Skip to content

Commit

Permalink
fix: diffing should not fail if resource fail schema validation (#19714
Browse files Browse the repository at this point in the history
…) (#19729)

Signed-off-by: Alexander Matyushentsev <[email protected]>
Co-authored-by: Alexander Matyushentsev <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and alexmt authored Aug 29, 2024
1 parent 02b8336 commit d56ef76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions util/argo/managedfields/managed_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions util/argo/managedfields/managed_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d56ef76

Please sign in to comment.