From 7396e6440972185153bbf0ec644fff30578235f8 Mon Sep 17 00:00:00 2001 From: Anshul Ahuja Date: Thu, 13 Jul 2023 10:26:28 +0530 Subject: [PATCH] Add separate handling for test operator with test Signed-off-by: Anshul Ahuja --- .../resourcemodifiers/resource_modifiers.go | 6 +++- .../resource_modifiers_test.go | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/internal/resourcemodifiers/resource_modifiers.go b/internal/resourcemodifiers/resource_modifiers.go index 77e15cde53..4d4d3ebca5 100644 --- a/internal/resourcemodifiers/resource_modifiers.go +++ b/internal/resourcemodifiers/resource_modifiers.go @@ -130,7 +130,11 @@ func ApplyPatch(patch []byte, obj *unstructured.Unstructured, log logrus.FieldLo } modifiedObjBytes, err := jsonPatch.Apply(objBytes) if err != nil { - return fmt.Errorf("error in applying JSON Patch, could be due to test operator failing %s", err.Error()) + if errors.Is(err, jsonpatch.ErrTestFailed) { + log.Infof("Test operation failed for JSON Patch %s", err.Error()) + return nil + } + return fmt.Errorf("error in applying JSON Patch %s", err.Error()) } err = obj.UnmarshalJSON(modifiedObjBytes) if err != nil { diff --git a/internal/resourcemodifiers/resource_modifiers_test.go b/internal/resourcemodifiers/resource_modifiers_test.go index 166aedeecf..ee2ebaca85 100644 --- a/internal/resourcemodifiers/resource_modifiers_test.go +++ b/internal/resourcemodifiers/resource_modifiers_test.go @@ -243,6 +243,39 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) { wantErr: false, wantObj: deployNginxTwoReplica.DeepCopy(), }, + { + name: "nginx deployment: test operator fails, skips substitution, no error", + fields: fields{ + Version: "v1", + ResourceModifierRules: []ResourceModifierRule{ + { + Conditions: Conditions{ + GroupKind: "deployments.apps", + ResourceNameRegex: "^test-.*$", + Namespaces: []string{"foo"}, + }, + Patches: []JSONPatch{ + { + Operation: "test", + Path: "/spec/replicas", + Value: "5", + }, + { + Operation: "replace", + Path: "/spec/replicas", + Value: "2", + }, + }, + }, + }, + }, + args: args{ + obj: deployNginxOneReplica.DeepCopy(), + groupResource: "deployments.apps", + }, + wantErr: false, + wantObj: deployNginxOneReplica.DeepCopy(), + }, { name: "nginx deployment: Empty Resource Regex", fields: fields{