From a656a8b3eb369cabe4f2151fd3edd597fd15f8a8 Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Wed, 17 Feb 2021 11:54:52 +0100 Subject: [PATCH] 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 cc7853700c..062b91d7e7 100644 --- a/pkg/trait/deployer.go +++ b/pkg/trait/deployer.go @@ -95,9 +95,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 67c2813cbc..e2c54b1e47 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 {