Skip to content

Commit

Permalink
create new integration/source folder and factor common code into help…
Browse files Browse the repository at this point in the history
…er.go

Signed-off-by: Matthias Wessendorf <[email protected]>
  • Loading branch information
matzew committed Nov 13, 2024
1 parent 84c5911 commit acc2146
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 297 deletions.
4 changes: 2 additions & 2 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"

"errors"
"knative.dev/eventing/pkg/reconciler/integration/source"
"log"
"net/http"
"os"
Expand All @@ -41,7 +42,6 @@ import (
"knative.dev/eventing/pkg/reconciler/channel"
"knative.dev/eventing/pkg/reconciler/containersource"
"knative.dev/eventing/pkg/reconciler/eventtype"
"knative.dev/eventing/pkg/reconciler/integrationsource"
"knative.dev/eventing/pkg/reconciler/parallel"
"knative.dev/eventing/pkg/reconciler/pingsource"
"knative.dev/eventing/pkg/reconciler/sequence"
Expand Down Expand Up @@ -105,7 +105,7 @@ func main() {
apiserversource.NewController,
pingsource.NewController,
containersource.NewController,
integrationsource.NewController,
source.NewController,

// Sources CRD
sourcecrd.NewController,
Expand Down
128 changes: 128 additions & 0 deletions pkg/reconciler/integration/helper.go
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_"

Check failure on line 28 in pkg/reconciler/integration/helper.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

const `prefix` is unused (unused)

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",
},
}
}
77 changes: 77 additions & 0 deletions pkg/reconciler/integration/helper_test.go
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integrationsource
package source

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integrationsource
package source

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integrationsource
package source

import (
"context"
"fmt"
"knative.dev/eventing/pkg/reconciler/integration/source/resources"

"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,7 +33,6 @@ import (
"knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha1/integrationsource"
v1listers "knative.dev/eventing/pkg/client/listers/sources/v1"
listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha1"
"knative.dev/eventing/pkg/reconciler/integrationsource/resources"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integrationsource
package source

import (
"fmt"
Expand Down
Loading

0 comments on commit acc2146

Please sign in to comment.