Skip to content

Commit

Permalink
fix: diffing should not fail if resource fail schema validation (#19735)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
  • Loading branch information
alexmt committed Aug 29, 2024
1 parent 5638e70 commit 1383a1b
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: %s", 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 @@ -143,6 +143,16 @@ func TestNormalize(t *testing.T) {
assert.Equal(t, 1, len(vwcConfig.Webhooks))
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 1383a1b

Please # to comment.