You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2021/06/08 19:21:25 UTC

[camel-k] 03/03: kameletbinding: remove the need of allocation some intermediate slices

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

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

commit 621e5578996957ab9aa771c6eaf15e8dda56e687
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Tue Jun 8 17:07:01 2021 +0200

    kameletbinding: remove the need of allocation some intermediate slices
---
 pkg/controller/kameletbinding/common.go | 60 +++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/pkg/controller/kameletbinding/common.go b/pkg/controller/kameletbinding/common.go
index bd952ab..c68e312 100644
--- a/pkg/controller/kameletbinding/common.go
+++ b/pkg/controller/kameletbinding/common.go
@@ -125,32 +125,20 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
 		}
 	}
 
-	allBindings := make([]*bindings.Binding, 0, len(steps)+3)
-	allBindings = append(allBindings, from)
-	allBindings = append(allBindings, steps...)
-	allBindings = append(allBindings, to)
-	if errorHandler != nil {
-		allBindings = append(allBindings, errorHandler)
-	}
-
-	propList := make([]string, 0)
-	for _, b := range allBindings {
-		if it.Spec.Traits == nil {
-			it.Spec.Traits = make(map[string]v1.TraitSpec)
-		}
-		for k, v := range b.Traits {
-			it.Spec.Traits[k] = v
-		}
-		for k, v := range b.ApplicationProperties {
-			propList = append(propList, fmt.Sprintf("%s=%s", k, v))
-		}
-	}
-
-	sort.Strings(propList)
-	for _, p := range propList {
-		it.Spec.Configuration = append(it.Spec.Configuration, v1.ConfigurationSpec{
-			Type:  "property",
-			Value: p,
+	configureBinding(&it, from)
+	configureBinding(&it, steps...)
+	configureBinding(&it, to)
+	configureBinding(&it, errorHandler)
+
+	if it.Spec.Configuration != nil {
+		sort.SliceStable(it.Spec.Configuration, func(i, j int) bool {
+			mi, mj := it.Spec.Configuration[i], it.Spec.Configuration[j]
+			switch {
+			case mi.Type != mj.Type:
+				return mi.Type < mj.Type
+			default:
+				return mi.Value < mj.Value
+			}
 		})
 	}
 
@@ -190,6 +178,26 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
 	return &it, nil
 }
 
+func configureBinding(integration *v1.Integration, bindings ...*bindings.Binding) {
+	for _, b := range bindings {
+		if b == nil {
+			continue
+		}
+		if integration.Spec.Traits == nil {
+			integration.Spec.Traits = make(map[string]v1.TraitSpec)
+		}
+		for k, v := range b.Traits {
+			integration.Spec.Traits[k] = v
+		}
+		for k, v := range b.ApplicationProperties {
+			integration.Spec.Configuration = append(integration.Spec.Configuration, v1.ConfigurationSpec{
+				Type:  "property",
+				Value: fmt.Sprintf("%s=%s", k, v),
+			})
+		}
+	}
+}
+
 func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.KameletBinding) (v1.TraitProfile, error) {
 	if binding.Spec.Integration != nil && binding.Spec.Integration.Profile != "" {
 		return binding.Spec.Integration.Profile, nil