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 2024/02/28 13:42:32 UTC

(camel-k) branch main updated: fix(ctrl): address changes during phase transition

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


The following commit(s) were added to refs/heads/main by this push:
     new 47f719a12 fix(ctrl): address changes during phase transition
47f719a12 is described below

commit 47f719a12b7ce4a1d007d3e749e9236f362a997f
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Mon Feb 26 14:57:57 2024 +0100

    fix(ctrl): address changes during phase transition
---
 pkg/util/digest/digest.go      | 47 +++++++++++++++++++------------
 pkg/util/digest/digest_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 18 deletions(-)

diff --git a/pkg/util/digest/digest.go b/pkg/util/digest/digest.go
index 8e34d097b..1aed64a1f 100644
--- a/pkg/util/digest/digest.go
+++ b/pkg/util/digest/digest.go
@@ -116,27 +116,13 @@ func ComputeForIntegration(integration *v1.Integration, configmaps []*corev1.Con
 	// Calculation logic prior to 1.10.0 (the new Traits API schema) is maintained
 	// in order to keep consistency in the digest calculated from the same set of
 	// Trait configurations for backward compatibility.
-	traitsMap, err := toMap(integration.Spec.Traits)
-	if err != nil {
+	if err := computeForTraits(hash, integration.Spec.Traits); err != nil {
 		return "", err
 	}
-	for _, name := range sortedTraitsMapKeys(traitsMap) {
-		if name != "addons" {
-			if err := computeForTrait(hash, name, traitsMap[name]); err != nil {
-				return "", err
-			}
-		} else {
-			// Addons
-			addons := traitsMap["addons"]
-			for _, name := range util.SortedMapKeys(addons) {
-				if addon, ok := addons[name].(map[string]interface{}); ok {
-					if err := computeForTrait(hash, name, addon); err != nil {
-						return "", err
-					}
-				}
-			}
-		}
+	if err := computeForTraits(hash, integration.Status.Traits); err != nil {
+		return "", err
 	}
+
 	// Integration traits as annotations
 	for _, k := range sortedTraitAnnotationsKeys(integration) {
 		v := integration.Annotations[k]
@@ -212,6 +198,31 @@ func ComputeForIntegration(integration *v1.Integration, configmaps []*corev1.Con
 	return digest, nil
 }
 
+func computeForTraits(hash hash.Hash, traits v1.Traits) error {
+	specTraitsMap, err := toMap(traits)
+	if err != nil {
+		return err
+	}
+	for _, name := range sortedTraitsMapKeys(specTraitsMap) {
+		if name != "addons" {
+			if err := computeForTrait(hash, name, specTraitsMap[name]); err != nil {
+				return err
+			}
+		} else {
+			// Addons
+			addons := specTraitsMap["addons"]
+			for _, name := range util.SortedMapKeys(addons) {
+				if addon, ok := addons[name].(map[string]interface{}); ok {
+					if err := computeForTrait(hash, name, addon); err != nil {
+						return err
+					}
+				}
+			}
+		}
+	}
+	return nil
+}
+
 func computeForTrait(hash hash.Hash, name string, trait map[string]interface{}) error {
 	if _, err := hash.Write([]byte(name + "[")); err != nil {
 		return err
diff --git a/pkg/util/digest/digest_test.go b/pkg/util/digest/digest_test.go
index a40a174a9..a08036bb5 100644
--- a/pkg/util/digest/digest_test.go
+++ b/pkg/util/digest/digest_test.go
@@ -138,3 +138,66 @@ func TestDigestUsesSecret(t *testing.T) {
 	require.NoError(t, err)
 	assert.NotEqual(t, digest2, digest3)
 }
+
+func TestDigestMatchingTraitsUpdated(t *testing.T) {
+	it := v1.Integration{
+		Spec: v1.IntegrationSpec{
+			Traits: v1.Traits{
+				Camel: &trait.CamelTrait{
+					Properties: []string{"hello=world"},
+				},
+			},
+		},
+	}
+
+	itSpecOnlyTraitUpdated := v1.Integration{
+		Spec: v1.IntegrationSpec{
+			Traits: v1.Traits{
+				Camel: &trait.CamelTrait{
+					Properties: []string{"hello=world2"},
+				},
+			},
+		},
+	}
+
+	itStatusOnlyTraitUpdated := v1.Integration{
+		Spec: v1.IntegrationSpec{},
+		Status: v1.IntegrationStatus{
+			Traits: v1.Traits{
+				Camel: &trait.CamelTrait{
+					Properties: []string{"hello=world2"},
+				},
+			},
+		},
+	}
+
+	itDigest, err := ComputeForIntegration(&it, nil, nil)
+	assert.Nil(t, err)
+	itSpecOnlyTraitUpdatedDigest, err := ComputeForIntegration(&itSpecOnlyTraitUpdated, nil, nil)
+	assert.Nil(t, err)
+	itStatusOnlyTraitUpdatedDigest, err := ComputeForIntegration(&itStatusOnlyTraitUpdated, nil, nil)
+	assert.Nil(t, err)
+
+	assert.NotEqual(t, itSpecOnlyTraitUpdatedDigest, itDigest, "Digests must not be equal")
+	assert.NotEqual(t, itStatusOnlyTraitUpdatedDigest, itDigest, "Digests must not be equal")
+	assert.Equal(t, itSpecOnlyTraitUpdatedDigest, itStatusOnlyTraitUpdatedDigest, "Digests must be equal")
+}
+
+func TestSpecStatusDrift(t *testing.T) {
+	it := v1.Integration{}
+	it.Spec.Traits.Camel = &trait.CamelTrait{}
+	it.Status.Traits.Camel = &trait.CamelTrait{}
+
+	it.Spec.Traits.Camel.Properties = []string{"hello=world1"}
+	d1, err := ComputeForIntegration(&it, nil, nil)
+	assert.Nil(t, err)
+	it.Status.Traits.Camel.Properties = []string{"hello=world2"}
+	d2, err := ComputeForIntegration(&it, nil, nil)
+	assert.Nil(t, err)
+	it.Spec.Traits.Camel.Properties = []string{"hello=world3"}
+	d3, err := ComputeForIntegration(&it, nil, nil)
+	assert.Nil(t, err)
+
+	assert.NotEqual(t, d2, d1, "d2 must not be equal to d1")
+	assert.NotEqual(t, d3, d2, "d3 must not be equal to d2")
+}