Skip to content

Commit

Permalink
chore(cmd): use bind trait option instead of annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jun 17, 2024
1 parent b3e2d60 commit ae34dd3
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 29 deletions.
2 changes: 1 addition & 1 deletion e2e/common/misc/pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestPipe(t *testing.T) {
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink",
"-p", "source.message=hello from test",
"-p", "sink.loggerName=integrationLogger",
"--annotation", "trait.camel.apache.org/camel.properties=[\"camel.prop1=a\",\"camel.prop2=b\"]",
"--trait", "camel.properties=[\"camel.prop1=a\",\"camel.prop2=b\"]",
"--name", "kb-with-traits").Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, "kb-with-traits"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Expand Down
10 changes: 8 additions & 2 deletions e2e/common/misc/pipe_with_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func TestPipeWithImage(t *testing.T) {
t.Run("run with initial image", func(t *testing.T) {
expectedImage := "quay.io/fuse_qe/echo-server:0.3.2"

g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink", "--annotation", "trait.camel.apache.org/container.image="+expectedImage, "--annotation", "trait.camel.apache.org/jvm.enabled=false", "--annotation", "trait.camel.apache.org/kamelets.enabled=false", "--annotation", "trait.camel.apache.org/dependencies.enabled=false", "--annotation", "test=1", "--name", bindingID).Execute()).To(Succeed())
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink",
"--trait", "container.image="+expectedImage, "--trait", "jvm.enabled=false",
"--trait", "kamelets.enabled=false", "--trait", "dependencies.enabled=false",
"--annotation", "test=1", "--name", bindingID).Execute()).To(Succeed())

g.Eventually(IntegrationGeneration(t, ctx, ns, bindingID)).
Should(gstruct.PointTo(BeNumerically("==", 1)))
Expand All @@ -69,7 +72,10 @@ func TestPipeWithImage(t *testing.T) {
t.Run("run with new image", func(t *testing.T) {
expectedImage := "quay.io/fuse_qe/echo-server:0.3.3"

g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink", "--annotation", "trait.camel.apache.org/container.image="+expectedImage, "--annotation", "trait.camel.apache.org/jvm.enabled=false", "--annotation", "trait.camel.apache.org/kamelets.enabled=false", "--annotation", "trait.camel.apache.org/dependencies.enabled=false", "--annotation", "test=2", "--name", bindingID).Execute()).To(Succeed())
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink",
"--trait", "container.image="+expectedImage, "--trait", "jvm.enabled=false",
"--trait", "kamelets.enabled=false", "--trait", "dependencies.enabled=false",
"--annotation", "test=2", "--name", bindingID).Execute()).To(Succeed())
g.Eventually(IntegrationGeneration(t, ctx, ns, bindingID)).
Should(gstruct.PointTo(BeNumerically("==", 1)))
g.Eventually(Integration(t, ctx, ns, bindingID)).Should(WithTransform(Annotations,
Expand Down
8 changes: 4 additions & 4 deletions e2e/common/traits/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ func TestHealthTrait(t *testing.T) {

g.Expect(KamelBindWithID(t, ctx, operatorID, ns, source, sink, "-p",
"source.message=Magicstring!", "-p", "sink.loggerName=binding",
"--annotation", "trait.camel.apache.org/health.enabled=true",
"--annotation", "trait.camel.apache.org/jolokia.enabled=true",
"--annotation", "trait.camel.apache.org/jolokia.use-ssl-client-authentication=false",
"--annotation", "trait.camel.apache.org/jolokia.protocol=http", "--name", name).Execute()).To(Succeed())
"--trait", "health.enabled=true",
"--trait", "jolokia.enabled=true",
"--trait", "jolokia.use-ssl-client-authentication=false",
"--trait", "jolokia.protocol=http", "--name", name).Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Expand Down
6 changes: 4 additions & 2 deletions e2e/knative/kamelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestKameletChange(t *testing.T) {
g.Eventually(IntegrationPodPhase(t, ctx, ns, "test-kamelet-display")).Should(Equal(corev1.PodRunning))

// Create the Pipe
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, timerSource, knChannelConf, "-p", "source.message=HelloKnative!", "--annotation", "trait.camel.apache.org/health.enabled=true", "--annotation", "trait.camel.apache.org/health.readiness-initial-delay=10", "--name", timerPipe).Execute()).To(Succeed())
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, timerSource, knChannelConf, "-p", "source.message=HelloKnative!",
"--trait", "health.enabled=true", "--trait", "health.readiness-initial-delay=10", "--name", timerPipe).Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, timerPipe)).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, timerPipe, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
// Consume the message
Expand All @@ -65,7 +66,8 @@ func TestKameletChange(t *testing.T) {
))

// Update the Pipe
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, timerSource, knChannelConf, "-p", "source.message=message is Hi", "--annotation", "trait.camel.apache.org/health.enabled=true", "--annotation", "trait.camel.apache.org/health.readiness-initial-delay=10", "--name", timerPipe).Execute()).To(Succeed())
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, timerSource, knChannelConf, "-p", "source.message=message is Hi",
"--trait", "health.enabled=true", "--trait", "health.readiness-initial-delay=10", "--name", timerPipe).Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, timerPipe), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, timerPipe, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
Expand Down
4 changes: 3 additions & 1 deletion e2e/native/native_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func TestNativeBinding(t *testing.T) {
message := "Magicstring!"
t.Run("binding with native build", func(t *testing.T) {
bindingName := "native-binding"
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "timer-source", "log-sink", "-p", "source.message="+message, "--annotation", "trait.camel.apache.org/quarkus.build-mode=native", "--annotation", "trait.camel.apache.org/builder.tasks-limit-memory=quarkus-native:6.5Gi", "--name", bindingName).Execute()).To(Succeed())
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "timer-source", "log-sink", "-p", "source.message="+message,
"--trait", "quarkus.build-mode=native", "--trait", "builder.tasks-limit-memory=quarkus-native:6.5Gi",
"--name", bindingName).Execute()).To(Succeed())

// ====================================
// !!! THE MOST TIME-CONSUMING PART !!!
Expand Down
23 changes: 21 additions & 2 deletions pkg/cmd/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error {
if binding.Annotations == nil {
binding.Annotations = make(map[string]string)
}

for _, t := range o.Traits {
kv := strings.Split(t, "=")
kv := strings.SplitN(t, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("could not parse trait configuration %s, expected format 'trait.property=value'", t)
}
binding.Annotations[v1.TraitAnnotationPrefix+kv[0]] = kv[1]
value := maybeBuildArrayNotation(binding.Annotations[v1.TraitAnnotationPrefix+kv[0]], kv[1])
binding.Annotations[v1.TraitAnnotationPrefix+kv[0]] = value
}
}

Expand Down Expand Up @@ -285,6 +287,23 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error {
return nil
}

// buildArrayNotation is used to build an array annotation to support traits array configuration
// for example, `-t camel.properties=a=1 -t camel.properties=b=2 would convert into annotation
// camel.properties=[a=1,b=2]
func maybeBuildArrayNotation(array, value string) string {
fmt.Println(array, value)
if array == "" {
return value
}
// append
if strings.HasPrefix(array, "[") && strings.HasSuffix(array, "]") {
content := array[1:len(array)-1] + "," + value
return "[" + content + "]"
}
// init the array notation
return "[" + array + "," + value + "]"
}

func showPipeOutput(cmd *cobra.Command, binding *v1.Pipe, outputFormat string, scheme runtime.ObjectTyper) error {
printer := printers.NewTypeSetter(scheme)
printer.Delegate = &kubernetes.CLIPrinter{
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,30 @@ status: {}
`, output)
}

func TestBindTraitsArray(t *testing.T) {
buildCmdOptions, bindCmd, _ := initializeBindCmdOptions(t)
output, err := test.ExecuteCommand(bindCmd, cmdBind, "my:src", "my:dst", "-o", "yaml",
"-t", "camel.properties=a=1", "-t", "camel.properties=b=2")
assert.Equal(t, "yaml", buildCmdOptions.OutputFormat)

require.NoError(t, err)
assert.Equal(t, `apiVersion: camel.apache.org/v1
kind: Pipe
metadata:
annotations:
camel.apache.org/operator.id: camel-k
trait.camel.apache.org/camel.properties: '[a=1,b=2]'
creationTimestamp: null
name: my-to-my
spec:
sink:
uri: my:dst
source:
uri: my:src
status: {}
`, output)
}

func TestBindSteps(t *testing.T) {
buildCmdOptions, bindCmd, _ := initializeBindCmdOptions(t)
output, err := test.ExecuteCommand(bindCmd, cmdBind, "my:src", "my:dst", "-o", "yaml",
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pipe/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestPipeIntegrationPipeTraitAnnotations(t *testing.T) {
Namespace: "ns",
Name: "my-pipe",
Annotations: map[string]string{
"trait.camel.apache.org/camel.runtime-version": "1.2.3",
v1.TraitAnnotationPrefix + "camel.runtime-version": "1.2.3",
},
},
Spec: v1.PipeSpec{
Expand Down
26 changes: 13 additions & 13 deletions pkg/trait/trait_configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func TestTraitConfigurationFromAnnotations(t *testing.T) {
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/cron.concurrency-policy": "annotated-policy",
"trait.camel.apache.org/environment.container-meta": boolean.TrueString,
v1.TraitAnnotationPrefix + "cron.concurrency-policy": "annotated-policy",
v1.TraitAnnotationPrefix + "environment.container-meta": boolean.TrueString,
},
},
Spec: v1.IntegrationSpec{
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestFailOnWrongTraitAnnotations(t *testing.T) {
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/cron.missing-property": "the-value",
v1.TraitAnnotationPrefix + "cron.missing-property": "the-value",
},
},
Spec: v1.IntegrationSpec{
Expand All @@ -116,8 +116,8 @@ func TestTraitConfigurationOverrideRulesFromAnnotations(t *testing.T) {
Platform: &v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/cron.components": "cmp2",
"trait.camel.apache.org/cron.schedule": "schedule2",
v1.TraitAnnotationPrefix + "cron.components": "cmp2",
v1.TraitAnnotationPrefix + "cron.schedule": "schedule2",
},
},
Spec: v1.IntegrationPlatformSpec{
Expand All @@ -133,9 +133,9 @@ func TestTraitConfigurationOverrideRulesFromAnnotations(t *testing.T) {
IntegrationKit: &v1.IntegrationKit{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/cron.components": "cmp3",
"trait.camel.apache.org/cron.concurrency-policy": "policy2",
"trait.camel.apache.org/builder.verbose": boolean.TrueString,
v1.TraitAnnotationPrefix + "cron.components": "cmp3",
v1.TraitAnnotationPrefix + "cron.concurrency-policy": "policy2",
v1.TraitAnnotationPrefix + "builder.verbose": boolean.TrueString,
},
},
Spec: v1.IntegrationKitSpec{
Expand All @@ -149,8 +149,8 @@ func TestTraitConfigurationOverrideRulesFromAnnotations(t *testing.T) {
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/cron.components": "cmp4",
"trait.camel.apache.org/cron.concurrency-policy": "policy4",
v1.TraitAnnotationPrefix + "cron.components": "cmp4",
v1.TraitAnnotationPrefix + "cron.concurrency-policy": "policy4",
},
},
Spec: v1.IntegrationSpec{
Expand Down Expand Up @@ -178,8 +178,8 @@ func TestTraitListConfigurationFromAnnotations(t *testing.T) {
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/jolokia.options": `["opt1", "opt2"]`,
"trait.camel.apache.org/service-binding.services": `Binding:xxx`, // lenient
v1.TraitAnnotationPrefix + "jolokia.options": `["opt1", "opt2"]`,
v1.TraitAnnotationPrefix + "service-binding.services": `Binding:xxx`, // lenient
},
},
Spec: v1.IntegrationSpec{
Expand All @@ -200,7 +200,7 @@ func TestTraitSplitConfiguration(t *testing.T) {
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/owner.target-labels": "[\"opt1\", \"opt2\"]",
v1.TraitAnnotationPrefix + "owner.target-labels": "[\"opt1\", \"opt2\"]",
},
},
Spec: v1.IntegrationSpec{
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func TestIntegrationAndPipeSameTraits(t *testing.T) {
pipe := &v1.Pipe{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/camel.runtime-version": "1.2.3",
v1.TraitAnnotationPrefix + "camel.runtime-version": "1.2.3",
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestDigestUsesAnnotations(t *testing.T) {
assert.Equal(t, digest1, digest2)

it.Annotations = map[string]string{
"another.annotation": "hello",
"trait.camel.apache.org/cron.fallback": "true",
"another.annotation": "hello",
v1.TraitAnnotationPrefix + "cron.fallback": "true",
}
digest3, err := ComputeForIntegration(&it, nil, nil)
require.NoError(t, err)
Expand Down

0 comments on commit ae34dd3

Please sign in to comment.