Add separate handling for test operator with test

Signed-off-by: Anshul Ahuja <anshulahuja@microsoft.com>
This commit is contained in:
Anshul Ahuja
2023-07-13 10:26:28 +05:30
parent 16ec2db1f7
commit 7396e64409
2 changed files with 38 additions and 1 deletions
@@ -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 {
@@ -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{