From d56ef7641cfa40b88cf3a1151e093568631496c1 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Thu, 29 Aug 2024 06:14:49 -1000 Subject: [PATCH] fix: diffing should not fail if resource fail schema validation (#19714) (#19729) Signed-off-by: Alexander Matyushentsev Co-authored-by: Alexander Matyushentsev --- util/argo/managedfields/managed_fields.go | 8 ++++++-- util/argo/managedfields/managed_fields_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/util/argo/managedfields/managed_fields.go b/util/argo/managedfields/managed_fields.go index 4dbe8a1a36244..1ead8f452195a 100644 --- a/util/argo/managedfields/managed_fields.go +++ b/util/argo/managedfields/managed_fields.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + log "github.com/sirupsen/logrus" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/structured-merge-diff/v4/fieldpath" @@ -28,12 +29,15 @@ func Normalize(live, config *unstructured.Unstructured, trustedManagers []string liveCopy := live.DeepCopy() configCopy := config.DeepCopy() + normalized := false + results, err := newTypedResults(liveCopy, configCopy, pt) + // error might happen if the resources are not parsable and so cannot be normalized if err != nil { - return nil, nil, fmt.Errorf("error building typed results: %w", err) + log.Debugf("error building typed results: %v", err) + return liveCopy, configCopy, nil } - normalized := false for _, mf := range live.GetManagedFields() { if trustedManager(mf.Manager, trustedManagers) { err := normalize(mf, results) diff --git a/util/argo/managedfields/managed_fields_test.go b/util/argo/managedfields/managed_fields_test.go index e94f486a64b29..91352f33396bb 100644 --- a/util/argo/managedfields/managed_fields_test.go +++ b/util/argo/managedfields/managed_fields_test.go @@ -141,6 +141,16 @@ func TestNormalize(t *testing.T) { assert.Len(t, vwcConfig.Webhooks, 1) assert.Equal(t, "", string(vwcConfig.Webhooks[0].ClientConfig.CABundle)) }) + t.Run("does not fail if object fails validation schema", func(t *testing.T) { + desiredState := StrToUnstructured(testdata.DesiredDeploymentYaml) + require.NoError(t, unstructured.SetNestedField(desiredState.Object, "spec", "hello", "world")) + liveState := StrToUnstructured(testdata.LiveDeploymentWithManagedReplicaYaml) + + pt := parser.Type("io.k8s.api.apps.v1.Deployment") + + _, _, err := managedfields.Normalize(liveState, desiredState, []string{}, &pt) + require.NoError(t, err) + }) } func validateNestedFloat64(t *testing.T, expected float64, obj *unstructured.Unstructured, fields ...string) {