You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2022/08/08 09:48:26 UTC

[camel-k] 01/03: fix(trait): camel to use a single properties file

This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 735f0161f05e2324776527ef7c0562208bfecf07
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Wed Jul 27 11:59:38 2022 +0200

    fix(trait): camel to use a single properties file
    
    Closes #3458
---
 e2e/global/common/kamelet_binding_test.go | 151 ++++++++++++++++++++----------
 pkg/trait/camel.go                        |  50 ++--------
 2 files changed, 111 insertions(+), 90 deletions(-)

diff --git a/e2e/global/common/kamelet_binding_test.go b/e2e/global/common/kamelet_binding_test.go
index 2c88d4e2c..ab290295b 100644
--- a/e2e/global/common/kamelet_binding_test.go
+++ b/e2e/global/common/kamelet_binding_test.go
@@ -33,62 +33,91 @@ import (
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 )
 
-func TestErrorHandler(t *testing.T) {
+func TestKameletBinding(t *testing.T) {
 	WithNewTestNamespace(t, func(ns string) {
-		operatorID := "camel-k-kamelet-errorhandler"
+		operatorID := "camel-k-kameletbinding"
 		Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed())
 
-		Expect(createErrorProducerKamelet(ns, "my-own-error-producer-source")()).To(Succeed())
-		Expect(createLogKamelet(ns, "my-own-log-sink")()).To(Succeed())
-		from := corev1.ObjectReference{
-			Kind:       "Kamelet",
-			Name:       "my-own-error-producer-source",
-			APIVersion: v1alpha1.SchemeGroupVersion.String(),
-		}
-
-		to := corev1.ObjectReference{
-			Kind:       "Kamelet",
-			Name:       "my-own-log-sink",
-			APIVersion: v1alpha1.SchemeGroupVersion.String(),
-		}
-
-		errorHandler := map[string]interface{}{
-			"sink": map[string]interface{}{
-				"endpoint": map[string]interface{}{
-					"ref": map[string]string{
-						"kind":       "Kamelet",
-						"apiVersion": v1alpha1.SchemeGroupVersion.String(),
-						"name":       "my-own-log-sink",
-					},
-					"properties": map[string]string{
-						"loggerName": "kameletErrorHandler",
-					},
-				}}}
-
-		t.Run("throw error test", func(t *testing.T) {
+		// Error Handler testing
+		t.Run("test error handler", func(t *testing.T) {
 			RegisterTestingT(t)
-
-			Expect(BindKameletToWithErrorHandler(ns, "throw-error-binding", map[string]string{}, from, to, map[string]string{"message": "throw Error"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())
-
-			Eventually(IntegrationPodPhase(ns, "throw-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
-			Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).Should(ContainSubstring("kameletErrorHandler"))
-			Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("integrationLogger"))
-
+			Expect(createErrorProducerKamelet(ns, "my-own-error-producer-source")()).To(Succeed())
+			Expect(createLogKamelet(ns, "my-own-log-sink")()).To(Succeed())
+			from := corev1.ObjectReference{
+				Kind:       "Kamelet",
+				Name:       "my-own-error-producer-source",
+				APIVersion: v1alpha1.SchemeGroupVersion.String(),
+			}
+
+			to := corev1.ObjectReference{
+				Kind:       "Kamelet",
+				Name:       "my-own-log-sink",
+				APIVersion: v1alpha1.SchemeGroupVersion.String(),
+			}
+
+			errorHandler := map[string]interface{}{
+				"sink": map[string]interface{}{
+					"endpoint": map[string]interface{}{
+						"ref": map[string]string{
+							"kind":       "Kamelet",
+							"apiVersion": v1alpha1.SchemeGroupVersion.String(),
+							"name":       "my-own-log-sink",
+						},
+						"properties": map[string]string{
+							"loggerName": "kameletErrorHandler",
+						},
+					}}}
+
+			t.Run("throw error test", func(t *testing.T) {
+				RegisterTestingT(t)
+
+				Expect(BindKameletToWithErrorHandler(ns, "throw-error-binding", map[string]string{}, from, to, map[string]string{"message": "throw Error"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())
+
+				Eventually(IntegrationPodPhase(ns, "throw-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
+				Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).Should(ContainSubstring("kameletErrorHandler"))
+				Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("integrationLogger"))
+
+			})
+
+			t.Run("don't throw error test", func(t *testing.T) {
+				RegisterTestingT(t)
+
+				Expect(BindKameletToWithErrorHandler(ns, "no-error-binding", map[string]string{}, from, to, map[string]string{"message": "true"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())
+
+				Eventually(IntegrationPodPhase(ns, "no-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
+				Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("kameletErrorHandler"))
+				Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))
+
+			})
+
+			// Cleanup
+			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 
-		t.Run("don't throw error test", func(t *testing.T) {
+		// Kamelet binding with traits testing
+		t.Run("test kamelet binding with trait", func(t *testing.T) {
 			RegisterTestingT(t)
-
-			Expect(BindKameletToWithErrorHandler(ns, "no-error-binding", map[string]string{}, from, to, map[string]string{"message": "true"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())
-
-			Eventually(IntegrationPodPhase(ns, "no-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
-			Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("kameletErrorHandler"))
-			Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))
-
+			Expect(createTimerKamelet(ns, "my-own-timer-source")()).To(Succeed())
+			// Log sink kamelet exists from previous test
+
+			from := corev1.ObjectReference{
+				Kind:       "Kamelet",
+				Name:       "my-own-timer-source",
+				APIVersion: v1alpha1.SchemeGroupVersion.String(),
+			}
+
+			to := corev1.ObjectReference{
+				Kind:       "Kamelet",
+				Name:       "my-own-log-sink",
+				APIVersion: v1alpha1.SchemeGroupVersion.String(),
+			}
+
+			Expect(BindKameletTo(ns, "kb-with-traits", map[string]string{"trait.camel.apache.org/camel.properties": "[\"camel.prop1=a\",\"camel.prop2=b\"]"}, from, to, map[string]string{"message": "hello from test"}, map[string]string{"loggerName": "integrationLogger"})()).To(Succeed())
+
+			Eventually(IntegrationPodPhase(ns, "kb-with-traits"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
+			Eventually(IntegrationLogs(ns, "kb-with-traits"), TestTimeoutShort).Should(ContainSubstring("hello from test"))
+			Eventually(IntegrationLogs(ns, "kb-with-traits"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))
 		})
-
-		// Cleanup
-		Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 	})
 }
 
@@ -143,3 +172,29 @@ func createErrorProducerKamelet(ns string, name string) func() error {
 
 	return CreateKamelet(ns, name, flow, props, nil)
 }
+
+func createTimerKamelet(ns string, name string) func() error {
+	props := map[string]v1alpha1.JSONSchemaProp{
+		"message": {
+			Type: "string",
+		},
+	}
+
+	flow := map[string]interface{}{
+		"from": map[string]interface{}{
+			"uri": "timer:tick",
+			"steps": []map[string]interface{}{
+				{
+					"set-body": map[string]interface{}{
+						"constant": "{{message}}",
+					},
+				},
+				{
+					"to": "kamelet:sink",
+				},
+			},
+		},
+	}
+
+	return CreateKamelet(ns, name, flow, props, nil)
+}
diff --git a/pkg/trait/camel.go b/pkg/trait/camel.go
index 671cee6ba..17f847560 100644
--- a/pkg/trait/camel.go
+++ b/pkg/trait/camel.go
@@ -79,10 +79,6 @@ func (t *camelTrait) Apply(e *Environment) error {
 	if e.IntegrationKitInPhase(v1.IntegrationKitPhaseReady) && e.IntegrationInRunningPhases() {
 		// Get all resources
 		maps := t.computeConfigMaps(e)
-		if t.Properties != nil {
-			// Only user.properties
-			maps = append(maps, t.computeUserProperties(e)...)
-		}
 		e.Resources.AddAll(maps)
 	}
 
@@ -179,6 +175,14 @@ func (t *camelTrait) computeConfigMaps(e *Environment) []ctrl.Object {
 		userProperties += fmt.Sprintf("%s=%s\n", prop.Name, prop.Value)
 	}
 
+	if t.Properties != nil {
+		// Merge with properties set in the trait
+		for _, prop := range t.Properties {
+			k, v := property.SplitPropertyFileEntry(prop)
+			userProperties += fmt.Sprintf("%s=%s\n", k, v)
+		}
+	}
+
 	if userProperties != "" {
 		maps = append(
 			maps,
@@ -283,41 +287,3 @@ func (t *camelTrait) computeConfigMaps(e *Environment) []ctrl.Object {
 
 	return maps
 }
-
-func (t *camelTrait) computeUserProperties(e *Environment) []ctrl.Object {
-	maps := make([]ctrl.Object, 0)
-
-	// combine properties of integration with kit, integration
-	// properties have the priority
-	userProperties := ""
-
-	for _, prop := range t.Properties {
-		k, v := property.SplitPropertyFileEntry(prop)
-		userProperties += fmt.Sprintf("%s=%s\n", k, v)
-	}
-
-	if userProperties != "" {
-		maps = append(
-			maps,
-			&corev1.ConfigMap{
-				TypeMeta: metav1.TypeMeta{
-					Kind:       "ConfigMap",
-					APIVersion: "v1",
-				},
-				ObjectMeta: metav1.ObjectMeta{
-					Name:      e.Integration.Name + "-user-properties",
-					Namespace: e.Integration.Namespace,
-					Labels: map[string]string{
-						v1.IntegrationLabel:                e.Integration.Name,
-						"camel.apache.org/properties.type": "user",
-					},
-				},
-				Data: map[string]string{
-					"application.properties": userProperties,
-				},
-			},
-		)
-	}
-
-	return maps
-}