Skip to content

Commit b4c59bb

Browse files
[release-1.17] Fixing the way we render custom camel tags on go structs (#8529)
hammer: Fixing the way we render custom camel tags on go structs. Remove the incorrect alue of the tag and update the function that assembles the actual ENV_VARs Signed-off-by: Matthias Wessendorf <[email protected]> Co-authored-by: Matthias Wessendorf <[email protected]>
1 parent 85ca388 commit b4c59bb

File tree

3 files changed

+93
-39
lines changed

3 files changed

+93
-39
lines changed

pkg/apis/common/integration/v1alpha1/aws.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,33 @@ type AWSCommon struct {
3333

3434
type AWSS3 struct {
3535
AWSCommon `json:",inline"` // Embeds AWSCommon to inherit its fields in JSON
36-
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN"` // S3 ARN
37-
DeleteAfterRead bool `json:"deleteAfterRead" default:"true"` // Auto-delete objects after reading
38-
MoveAfterRead bool `json:"moveAfterRead" default:"false"` // Move objects after reading
39-
DestinationBucket string `json:"destinationBucket,omitempty"` // Destination bucket for moved objects
40-
DestinationBucketPrefix string `json:"destinationBucketPrefix,omitempty"` // Prefix for moved objects
41-
DestinationBucketSuffix string `json:"destinationBucketSuffix,omitempty"` // Suffix for moved objects
42-
AutoCreateBucket bool `json:"autoCreateBucket" default:"false"` // Auto-create S3 bucket
43-
Prefix string `json:"prefix,omitempty"` // S3 bucket prefix for search
44-
IgnoreBody bool `json:"ignoreBody" default:"false"` // Ignore object body
45-
ForcePathStyle bool `json:"forcePathStyle" default:"false"` // Force path style for bucket access
46-
Delay int `json:"delay" default:"500"` // Delay between polls in milliseconds
47-
MaxMessagesPerPoll int `json:"maxMessagesPerPoll" default:"10"` // Max messages to poll per request
36+
Arn string `json:"arn,omitempty" camel:"BUCKETNAMEORARN"` // S3 ARN
37+
DeleteAfterRead bool `json:"deleteAfterRead" default:"true"` // Auto-delete objects after reading
38+
MoveAfterRead bool `json:"moveAfterRead" default:"false"` // Move objects after reading
39+
DestinationBucket string `json:"destinationBucket,omitempty"` // Destination bucket for moved objects
40+
DestinationBucketPrefix string `json:"destinationBucketPrefix,omitempty"` // Prefix for moved objects
41+
DestinationBucketSuffix string `json:"destinationBucketSuffix,omitempty"` // Suffix for moved objects
42+
AutoCreateBucket bool `json:"autoCreateBucket" default:"false"` // Auto-create S3 bucket
43+
Prefix string `json:"prefix,omitempty"` // S3 bucket prefix for search
44+
IgnoreBody bool `json:"ignoreBody" default:"false"` // Ignore object body
45+
ForcePathStyle bool `json:"forcePathStyle" default:"false"` // Force path style for bucket access
46+
Delay int `json:"delay" default:"500"` // Delay between polls in milliseconds
47+
MaxMessagesPerPoll int `json:"maxMessagesPerPoll" default:"10"` // Max messages to poll per request
4848
}
4949

5050
type AWSSQS struct {
5151
AWSCommon `json:",inline"` // Embeds AWSCommon to inherit its fields in JSON
52-
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_SQS_SOURCE_QUEUENAMEORARN"` // SQS ARN
53-
DeleteAfterRead bool `json:"deleteAfterRead" default:"true"` // Auto-delete messages after reading
54-
AutoCreateQueue bool `json:"autoCreateQueue" default:"false"` // Auto-create SQS queue
55-
Host string `json:"host" camel:"CAMEL_KAMELET_AWS_SQS_SOURCE_AMAZONAWSHOST" default:"amazonaws.com"` // AWS host
56-
Protocol string `json:"protocol" default:"https"` // Communication protocol (http/https)
57-
QueueURL string `json:"queueURL,omitempty"` // Full SQS queue URL
58-
Greedy bool `json:"greedy" default:"false"` // Greedy scheduler
59-
Delay int `json:"delay" default:"500"` // Delay between polls in milliseconds
60-
MaxMessagesPerPoll int `json:"maxMessagesPerPoll" default:"1"` // Max messages to return (1-10)
61-
WaitTimeSeconds int `json:"waitTimeSeconds,omitempty"` // Wait time for messages
62-
VisibilityTimeout int `json:"visibilityTimeout,omitempty"` // Visibility timeout in seconds
52+
Arn string `json:"arn,omitempty" camel:"QUEUENAMEORARN"` // SQS ARN
53+
DeleteAfterRead bool `json:"deleteAfterRead" default:"true"` // Auto-delete messages after reading
54+
AutoCreateQueue bool `json:"autoCreateQueue" default:"false"` // Auto-create SQS queue
55+
Host string `json:"host" camel:"AMAZONAWSHOST" default:"amazonaws.com"` // AWS host
56+
Protocol string `json:"protocol" default:"https"` // Communication protocol (http/https)
57+
QueueURL string `json:"queueURL,omitempty"` // Full SQS queue URL
58+
Greedy bool `json:"greedy" default:"false"` // Greedy scheduler
59+
Delay int `json:"delay" default:"500"` // Delay between polls in milliseconds
60+
MaxMessagesPerPoll int `json:"maxMessagesPerPoll" default:"1"` // Max messages to return (1-10)
61+
WaitTimeSeconds int `json:"waitTimeSeconds,omitempty"` // Wait time for messages
62+
VisibilityTimeout int `json:"visibilityTimeout,omitempty"` // Visibility timeout in seconds
6363
}
6464

6565
type AWSDDBStreams struct {
@@ -71,6 +71,6 @@ type AWSDDBStreams struct {
7171

7272
type AWSSNS struct {
7373
AWSCommon `json:",inline"` // Embeds AWSCommon to inherit its fields in JSON
74-
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_SNS_SINK_TOPICNAMEORARN"` // SNS ARN
75-
AutoCreateTopic bool `json:"autoCreateTopic" default:"false"` // Auto-create SNS topic
74+
Arn string `json:"arn,omitempty" camel:"TOPICNAMEORARN"` // SNS ARN
75+
AutoCreateTopic bool `json:"autoCreateTopic" default:"false"` // Auto-create SNS topic
7676
}

pkg/reconciler/integration/helper.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func GenerateEnvVarsFromStruct(prefix string, s interface{}) []corev1.EnvVar {
5454

5555
// First, check for the custom 'camel' tag
5656
envVarName := fieldType.Tag.Get("camel")
57-
if envVarName == "" {
57+
if envVarName != "" {
58+
envVarName = fmt.Sprintf("%s_%s", prefix, envVarName)
59+
} else {
5860
// If 'camel' tag is not present, fall back to the 'json' tag or Go field name
5961
jsonTag := fieldType.Tag.Get("json")
6062
tagName := strings.Split(jsonTag, ",")[0]

pkg/reconciler/integration/helper_test.go

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,77 @@ func TestGenerateEnvVarsFromStruct(t *testing.T) {
5353

5454
func TestGenerateEnvVarsFromStruct_S3WithCamelTag(t *testing.T) {
5555
type AWSS3 struct {
56-
Arn string `json:"arn,omitempty" camel:"CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN"`
56+
Arn string `json:"arn,omitempty" camel:"BUCKETNAMEORARN"`
5757
Region string `json:"region,omitempty"`
5858
}
5959

60-
prefix := "CAMEL_KAMELET_AWS_S3_SOURCE"
61-
input := AWSS3{
62-
Arn: "arn:aws:s3:::example-bucket",
63-
Region: "us-west-2",
60+
type AWSSQS struct {
61+
Arn string `json:"arn,omitempty" camel:"QUEUENAMEORARN"`
62+
Region string `json:"region,omitempty"`
6463
}
6564

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"},
65+
tests := []struct {
66+
name string
67+
prefix string
68+
input interface{}
69+
want []corev1.EnvVar
70+
}{
71+
{
72+
name: "S3 Source with Camel Tag",
73+
prefix: "CAMEL_KAMELET_AWS_S3_SOURCE",
74+
input: AWSS3{
75+
Arn: "arn:aws:s3:::example-bucket",
76+
Region: "us-west-2",
77+
},
78+
want: []corev1.EnvVar{
79+
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN", Value: "arn:aws:s3:::example-bucket"},
80+
{Name: "CAMEL_KAMELET_AWS_S3_SOURCE_REGION", Value: "us-west-2"},
81+
},
82+
},
83+
{
84+
name: "S3 Sink with Camel Tag",
85+
prefix: "CAMEL_KAMELET_AWS_S3_SINK",
86+
input: AWSS3{
87+
Arn: "arn:aws:s3:::another-bucket",
88+
Region: "eu-central-1",
89+
},
90+
want: []corev1.EnvVar{
91+
{Name: "CAMEL_KAMELET_AWS_S3_SINK_BUCKETNAMEORARN", Value: "arn:aws:s3:::another-bucket"},
92+
{Name: "CAMEL_KAMELET_AWS_S3_SINK_REGION", Value: "eu-central-1"},
93+
},
94+
},
95+
{
96+
name: "SQS Source with Camel Tag",
97+
prefix: "CAMEL_KAMELET_AWS_SQS_SOURCE",
98+
input: AWSSQS{
99+
Arn: "arn:aws:sqs:::example-queue",
100+
Region: "us-east-1",
101+
},
102+
want: []corev1.EnvVar{
103+
{Name: "CAMEL_KAMELET_AWS_SQS_SOURCE_QUEUENAMEORARN", Value: "arn:aws:sqs:::example-queue"},
104+
{Name: "CAMEL_KAMELET_AWS_SQS_SOURCE_REGION", Value: "us-east-1"},
105+
},
106+
},
107+
{
108+
name: "SQS Sink with Camel Tag",
109+
prefix: "CAMEL_KAMELET_AWS_SQS_SINK",
110+
input: AWSSQS{
111+
Arn: "arn:aws:sqs:::another-queue",
112+
Region: "ap-southeast-1",
113+
},
114+
want: []corev1.EnvVar{
115+
{Name: "CAMEL_KAMELET_AWS_SQS_SINK_QUEUENAMEORARN", Value: "arn:aws:sqs:::another-queue"},
116+
{Name: "CAMEL_KAMELET_AWS_SQS_SINK_REGION", Value: "ap-southeast-1"},
117+
},
118+
},
70119
}
71120

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)
121+
for _, tt := range tests {
122+
t.Run(tt.name, func(t *testing.T) {
123+
got := GenerateEnvVarsFromStruct(tt.prefix, tt.input)
124+
if diff := cmp.Diff(tt.want, got); diff != "" {
125+
t.Errorf("GenerateEnvVarsFromStruct() mismatch (-want +got):\n%s", diff)
126+
}
127+
})
76128
}
77129
}

0 commit comments

Comments
 (0)