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/02/18 09:35:15 UTC

[camel-k] branch release-1.3.x updated: fix: Consider ObjectMeta field for deep derivative comparison before patching

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

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


The following commit(s) were added to refs/heads/release-1.3.x by this push:
     new 77a0956  fix: Consider ObjectMeta field for deep derivative comparison before patching
77a0956 is described below

commit 77a095600b512e82a6361ece67faa615157e893f
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Wed Feb 17 11:54:52 2021 +0100

    fix: Consider ObjectMeta field for deep derivative comparison before patching
---
 pkg/trait/deployer.go   |  7 ++++---
 pkg/util/patch/patch.go | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/pkg/trait/deployer.go b/pkg/trait/deployer.go
index 81aaee8..e21b0bd 100644
--- a/pkg/trait/deployer.go
+++ b/pkg/trait/deployer.go
@@ -94,9 +94,10 @@ func (t *deployerTrait) Apply(e *Environment) error {
 					return err
 				}
 
-				if !patch.SpecEqualDeepDerivative(object, resource) {
-					// If both objects have a "Spec" field and it contains all expected fields
-					// (plus optional others), then avoid patching
+				// If both objects have "ObjectMeta" and "Spec" fields and they contain all the expected fields
+				// (plus optional others), then avoid patching.
+				if !patch.ObjectMetaEqualDeepDerivative(object, resource) ||
+					!patch.SpecEqualDeepDerivative(object, resource) {
 
 					p, err := patch.PositiveMergePatch(object, resource)
 					if err != nil {
diff --git a/pkg/util/patch/patch.go b/pkg/util/patch/patch.go
index 67c2813..e2c54b1 100644
--- a/pkg/util/patch/patch.go
+++ b/pkg/util/patch/patch.go
@@ -91,6 +91,25 @@ func removeNilValues(v reflect.Value, parent reflect.Value) {
 	}
 }
 
+func ObjectMetaEqualDeepDerivative(object runtime.Object, expected runtime.Object) (res bool) {
+	defer func() {
+		if r := recover(); r != nil {
+			res = false
+		}
+	}()
+
+	if expected == nil {
+		return true
+	} else if object == nil {
+		return false
+	}
+
+	objectMeta := reflect.ValueOf(object).Elem().FieldByName("ObjectMeta").Interface()
+	expectedMeta := reflect.ValueOf(expected).Elem().FieldByName("ObjectMeta").Interface()
+
+	return equality.Semantic.DeepDerivative(expectedMeta, objectMeta)
+}
+
 func SpecEqualDeepDerivative(object runtime.Object, expected runtime.Object) (res bool) {
 	defer func() {
 		if r := recover(); r != nil {