-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create new integration/source folder and factor common code into help…
…er.go Signed-off-by: Matthias Wessendorf <[email protected]>
- Loading branch information
Showing
11 changed files
with
345 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2024 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const prefix = "CAMEL_KAMELET_" | ||
|
||
func GenerateEnvVarsFromStruct(prefix string, s interface{}) []corev1.EnvVar { | ||
var envVars []corev1.EnvVar | ||
|
||
// Use reflection to inspect the struct fields | ||
v := reflect.ValueOf(s) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
|
||
t := v.Type() | ||
|
||
for i := 0; i < v.NumField(); i++ { | ||
field := v.Field(i) | ||
fieldType := t.Field(i) | ||
|
||
// Skip unexported fields | ||
if !field.CanInterface() { | ||
continue | ||
} | ||
|
||
// Handle embedded/anonymous structs recursively | ||
if fieldType.Anonymous && field.Kind() == reflect.Struct { | ||
// Recursively handle embedded structs with the same prefix | ||
envVars = append(envVars, GenerateEnvVarsFromStruct(prefix, field.Interface())...) | ||
continue | ||
} | ||
|
||
// First, check for the custom 'camel' tag | ||
envVarName := fieldType.Tag.Get("camel") | ||
if envVarName == "" { | ||
// If 'camel' tag is not present, fall back to the 'json' tag or Go field name | ||
jsonTag := fieldType.Tag.Get("json") | ||
tagName := strings.Split(jsonTag, ",")[0] | ||
if tagName == "" || tagName == "-" { | ||
tagName = fieldType.Name | ||
} | ||
envVarName = fmt.Sprintf("%s_%s", prefix, strings.ToUpper(tagName)) | ||
} | ||
|
||
if field.Kind() == reflect.Ptr { | ||
if field.IsNil() { | ||
continue | ||
} | ||
field = field.Elem() | ||
} | ||
|
||
var value string | ||
switch field.Kind() { | ||
case reflect.Int, reflect.Int32, reflect.Int64: | ||
value = strconv.FormatInt(field.Int(), 10) | ||
case reflect.Bool: | ||
value = strconv.FormatBool(field.Bool()) | ||
case reflect.String: | ||
value = field.String() | ||
default: | ||
// Skip unsupported types | ||
continue | ||
} | ||
|
||
// Skip zero/empty values | ||
if value == "" { | ||
continue | ||
} | ||
|
||
envVars = append(envVars, corev1.EnvVar{ | ||
Name: envVarName, | ||
Value: value, | ||
}) | ||
} | ||
|
||
return envVars | ||
} | ||
|
||
func MakeSecretEnvVar(name, key, secretName string) corev1.EnvVar { | ||
return corev1.EnvVar{ | ||
Name: name, | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
Key: key, | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secretName, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func MakeSSLEnvVar() []corev1.EnvVar { | ||
return []corev1.EnvVar{ | ||
{ | ||
Name: "CAMEL_KNATIVE_CLIENT_SSL_ENABLED", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "CAMEL_KNATIVE_CLIENT_SSL_CERT_PATH", | ||
Value: "/knative-custom-certs/knative-eventing-bundle.pem", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2024 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package integration | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestGenerateEnvVarsFromStruct(t *testing.T) { | ||
type TestStruct struct { | ||
Field1 int `json:"field1"` | ||
Field2 bool `json:"field2"` | ||
Field3 string `json:"field3"` | ||
} | ||
|
||
prefix := "TEST_PREFIX" | ||
input := &TestStruct{ | ||
Field1: 123, | ||
Field2: true, | ||
Field3: "hello", | ||
} | ||
|
||
// Expected environment variables including SSL settings | ||
want := []corev1.EnvVar{ | ||
{Name: "TEST_PREFIX_FIELD1", Value: "123"}, | ||
{Name: "TEST_PREFIX_FIELD2", Value: "true"}, | ||
{Name: "TEST_PREFIX_FIELD3", Value: "hello"}, | ||
} | ||
|
||
got := GenerateEnvVarsFromStruct(prefix, input) | ||
|
||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("generateEnvVarsFromStruct() mismatch (-want +got):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestGenerateEnvVarsFromStruct_S3WithCamelTag(t *testing.T) { | ||
type AWSS3 struct { | ||
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN"` | ||
Region string `json:"region,omitempty"` | ||
} | ||
|
||
prefix := "CAMEL_KAMELET_AWS_S3_SOURCE" | ||
input := AWSS3{ | ||
Arn: "arn:aws:s3:::example-bucket", | ||
Region: "us-west-2", | ||
} | ||
|
||
// Expected environment variables including SSL settings and camel tag for Arn | ||
want := []corev1.EnvVar{ | ||
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN", Value: "arn:aws:s3:::example-bucket"}, | ||
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_REGION", Value: "us-west-2"}, | ||
} | ||
|
||
got := GenerateEnvVarsFromStruct(prefix, input) | ||
|
||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("generateEnvVarsFromStruct_S3WithCamelTag() mismatch (-want +got):\n%s", diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.