You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2021/06/04 10:27:16 UTC

[camel-k] branch main updated: Related to #2165: account for annotations when checking if integration is changed

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

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


The following commit(s) were added to refs/heads/main by this push:
     new b448abf  Related to #2165: account for annotations when checking if integration is changed
b448abf is described below

commit b448abf08ef59245fcb7ddb4060f427af47d6403
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Thu Jun 3 11:00:14 2021 +0200

    Related to #2165: account for annotations when checking if integration is changed
---
 pkg/util/digest/digest.go      | 19 +++++++++++++++++
 pkg/util/digest/digest_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)

diff --git a/pkg/util/digest/digest.go b/pkg/util/digest/digest.go
index c63259c..e3baad4 100644
--- a/pkg/util/digest/digest.go
+++ b/pkg/util/digest/digest.go
@@ -29,6 +29,7 @@ import (
 	"path"
 	"sort"
 	"strconv"
+	"strings"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/util"
@@ -120,6 +121,13 @@ func ComputeForIntegration(integration *v1.Integration) (string, error) {
 			return "", err
 		}
 	}
+	// Integration traits as annotations
+	for _, k := range sortedTraitAnnotationsKeys(integration) {
+		v := integration.Annotations[k]
+		if _, err := hash.Write([]byte(fmt.Sprintf("%s=%v,", k, v))); err != nil {
+			return "", err
+		}
+	}
 
 	// Add a letter at the beginning and use URL safe encoding
 	digest := "v" + base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
@@ -241,6 +249,17 @@ func sortedTraitSpecMapKeys(m map[string]v1.TraitSpec) []string {
 	return res
 }
 
+func sortedTraitAnnotationsKeys(it *v1.Integration) []string {
+	res := make([]string, 0, len(it.Annotations))
+	for k := range it.Annotations {
+		if strings.HasPrefix(k, v1.TraitAnnotationPrefix) {
+			res = append(res, k)
+		}
+	}
+	sort.Strings(res)
+	return res
+}
+
 func ComputeSHA1(elem ...string) (string, error) {
 	file := path.Join(elem...)
 
diff --git a/pkg/util/digest/digest_test.go b/pkg/util/digest/digest_test.go
new file mode 100644
index 0000000..096146f
--- /dev/null
+++ b/pkg/util/digest/digest_test.go
@@ -0,0 +1,46 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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 digest
+
+import (
+	"testing"
+
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/stretchr/testify/assert"
+)
+
+func TestDigestUsesAnnotations(t *testing.T) {
+	it := v1.Integration{}
+	digest1, err := ComputeForIntegration(&it)
+	assert.NoError(t, err)
+
+	it.Annotations = map[string]string{
+		"another.annotation": "hello",
+	}
+	digest2, err := ComputeForIntegration(&it)
+	assert.NoError(t, err)
+	assert.Equal(t, digest1, digest2)
+
+	it.Annotations = map[string]string{
+		"another.annotation":                   "hello",
+		"trait.camel.apache.org/cron.fallback": "true",
+	}
+	digest3, err := ComputeForIntegration(&it)
+	assert.NoError(t, err)
+	assert.NotEqual(t, digest1, digest3)
+}