Skip to content

Commit 40877d7

Browse files
committed
create new integration/source folder and factor common code into helper.go
Signed-off-by: Matthias Wessendorf <[email protected]>
1 parent f21370a commit 40877d7

File tree

11 files changed

+345
-297
lines changed

11 files changed

+345
-297
lines changed

cmd/controller/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2222

2323
"errors"
24+
"knative.dev/eventing/pkg/reconciler/integration/source"
2425
"log"
2526
"net/http"
2627
"os"
@@ -41,7 +42,6 @@ import (
4142
"knative.dev/eventing/pkg/reconciler/channel"
4243
"knative.dev/eventing/pkg/reconciler/containersource"
4344
"knative.dev/eventing/pkg/reconciler/eventtype"
44-
"knative.dev/eventing/pkg/reconciler/integrationsource"
4545
"knative.dev/eventing/pkg/reconciler/parallel"
4646
"knative.dev/eventing/pkg/reconciler/pingsource"
4747
"knative.dev/eventing/pkg/reconciler/sequence"
@@ -105,7 +105,7 @@ func main() {
105105
apiserversource.NewController,
106106
pingsource.NewController,
107107
containersource.NewController,
108-
integrationsource.NewController,
108+
source.NewController,
109109

110110
// Sources CRD
111111
sourcecrd.NewController,

pkg/reconciler/integration/helper.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2024 The Knative Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"strconv"
23+
"strings"
24+
25+
corev1 "k8s.io/api/core/v1"
26+
)
27+
28+
const prefix = "CAMEL_KAMELET_"
29+
30+
func GenerateEnvVarsFromStruct(prefix string, s interface{}) []corev1.EnvVar {
31+
var envVars []corev1.EnvVar
32+
33+
// Use reflection to inspect the struct fields
34+
v := reflect.ValueOf(s)
35+
if v.Kind() == reflect.Ptr {
36+
v = v.Elem()
37+
}
38+
39+
t := v.Type()
40+
41+
for i := 0; i < v.NumField(); i++ {
42+
field := v.Field(i)
43+
fieldType := t.Field(i)
44+
45+
// Skip unexported fields
46+
if !field.CanInterface() {
47+
continue
48+
}
49+
50+
// Handle embedded/anonymous structs recursively
51+
if fieldType.Anonymous && field.Kind() == reflect.Struct {
52+
// Recursively handle embedded structs with the same prefix
53+
envVars = append(envVars, GenerateEnvVarsFromStruct(prefix, field.Interface())...)
54+
continue
55+
}
56+
57+
// First, check for the custom 'camel' tag
58+
envVarName := fieldType.Tag.Get("camel")
59+
if envVarName == "" {
60+
// If 'camel' tag is not present, fall back to the 'json' tag or Go field name
61+
jsonTag := fieldType.Tag.Get("json")
62+
tagName := strings.Split(jsonTag, ",")[0]
63+
if tagName == "" || tagName == "-" {
64+
tagName = fieldType.Name
65+
}
66+
envVarName = fmt.Sprintf("%s_%s", prefix, strings.ToUpper(tagName))
67+
}
68+
69+
if field.Kind() == reflect.Ptr {
70+
if field.IsNil() {
71+
continue
72+
}
73+
field = field.Elem()
74+
}
75+
76+
var value string
77+
switch field.Kind() {
78+
case reflect.Int, reflect.Int32, reflect.Int64:
79+
value = strconv.FormatInt(field.Int(), 10)
80+
case reflect.Bool:
81+
value = strconv.FormatBool(field.Bool())
82+
case reflect.String:
83+
value = field.String()
84+
default:
85+
// Skip unsupported types
86+
continue
87+
}
88+
89+
// Skip zero/empty values
90+
if value == "" {
91+
continue
92+
}
93+
94+
envVars = append(envVars, corev1.EnvVar{
95+
Name: envVarName,
96+
Value: value,
97+
})
98+
}
99+
100+
return envVars
101+
}
102+
103+
func MakeSecretEnvVar(name, key, secretName string) corev1.EnvVar {
104+
return corev1.EnvVar{
105+
Name: name,
106+
ValueFrom: &corev1.EnvVarSource{
107+
SecretKeyRef: &corev1.SecretKeySelector{
108+
Key: key,
109+
LocalObjectReference: corev1.LocalObjectReference{
110+
Name: secretName,
111+
},
112+
},
113+
},
114+
}
115+
}
116+
117+
func MakeSSLEnvVar() []corev1.EnvVar {
118+
return []corev1.EnvVar{
119+
{
120+
Name: "CAMEL_KNATIVE_CLIENT_SSL_ENABLED",
121+
Value: "true",
122+
},
123+
{
124+
Name: "CAMEL_KNATIVE_CLIENT_SSL_CERT_PATH",
125+
Value: "/knative-custom-certs/knative-eventing-bundle.pem",
126+
},
127+
}
128+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2024 The Knative Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
corev1 "k8s.io/api/core/v1"
24+
)
25+
26+
func TestGenerateEnvVarsFromStruct(t *testing.T) {
27+
type TestStruct struct {
28+
Field1 int `json:"field1"`
29+
Field2 bool `json:"field2"`
30+
Field3 string `json:"field3"`
31+
}
32+
33+
prefix := "TEST_PREFIX"
34+
input := &TestStruct{
35+
Field1: 123,
36+
Field2: true,
37+
Field3: "hello",
38+
}
39+
40+
// Expected environment variables including SSL settings
41+
want := []corev1.EnvVar{
42+
{Name: "TEST_PREFIX_FIELD1", Value: "123"},
43+
{Name: "TEST_PREFIX_FIELD2", Value: "true"},
44+
{Name: "TEST_PREFIX_FIELD3", Value: "hello"},
45+
}
46+
47+
got := GenerateEnvVarsFromStruct(prefix, input)
48+
49+
if diff := cmp.Diff(want, got); diff != "" {
50+
t.Errorf("generateEnvVarsFromStruct() mismatch (-want +got):\n%s", diff)
51+
}
52+
}
53+
54+
func TestGenerateEnvVarsFromStruct_S3WithCamelTag(t *testing.T) {
55+
type AWSS3 struct {
56+
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN"`
57+
Region string `json:"region,omitempty"`
58+
}
59+
60+
prefix := "CAMEL_KAMELET_AWS_S3_SOURCE"
61+
input := AWSS3{
62+
Arn: "arn:aws:s3:::example-bucket",
63+
Region: "us-west-2",
64+
}
65+
66+
// Expected environment variables including SSL settings and camel tag for Arn
67+
want := []corev1.EnvVar{
68+
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN", Value: "arn:aws:s3:::example-bucket"},
69+
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_REGION", Value: "us-west-2"},
70+
}
71+
72+
got := GenerateEnvVarsFromStruct(prefix, input)
73+
74+
if diff := cmp.Diff(want, got); diff != "" {
75+
t.Errorf("generateEnvVarsFromStruct_S3WithCamelTag() mismatch (-want +got):\n%s", diff)
76+
}
77+
}

pkg/reconciler/integrationsource/controller.go renamed to pkg/reconciler/integration/source/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package integrationsource
17+
package source
1818

1919
import (
2020
"context"

pkg/reconciler/integrationsource/controller_test.go renamed to pkg/reconciler/integration/source/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package integrationsource
17+
package source
1818

1919
import (
2020
"context"

pkg/reconciler/integrationsource/integrationsource.go renamed to pkg/reconciler/integration/source/integrationsource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package integrationsource
17+
package source
1818

1919
import (
2020
"context"
2121
"fmt"
22+
"knative.dev/eventing/pkg/reconciler/integration/source/resources"
2223

2324
"go.uber.org/zap"
2425
corev1 "k8s.io/api/core/v1"
@@ -32,7 +33,6 @@ import (
3233
"knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha1/integrationsource"
3334
v1listers "knative.dev/eventing/pkg/client/listers/sources/v1"
3435
listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha1"
35-
"knative.dev/eventing/pkg/reconciler/integrationsource/resources"
3636
"knative.dev/pkg/controller"
3737
"knative.dev/pkg/logging"
3838
pkgreconciler "knative.dev/pkg/reconciler"

pkg/reconciler/integrationsource/integrationsource_test.go renamed to pkg/reconciler/integration/source/integrationsource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package integrationsource
17+
package source
1818

1919
import (
2020
"fmt"

0 commit comments

Comments
 (0)