From a00cf9ad2c413c121b9893e434f2480a33a000c3 Mon Sep 17 00:00:00 2001 From: Anshul Ahuja Date: Thu, 6 Jul 2023 10:31:05 +0530 Subject: [PATCH] refactor Signed-off-by: Anshul Ahuja --- internal/resourcemodifiers/resource_modifiers.go | 4 ++-- .../resourcemodifiers/resource_modifiers_test.go | 11 ++++++++--- .../resource_modifiers_validator_test.go | 14 +++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/internal/resourcemodifiers/resource_modifiers.go b/internal/resourcemodifiers/resource_modifiers.go index b2dbe6a99..37c41dd99 100644 --- a/internal/resourcemodifiers/resource_modifiers.go +++ b/internal/resourcemodifiers/resource_modifiers.go @@ -23,7 +23,7 @@ const ( type JsonPatch struct { Operation string `yaml:"operation"` Path string `yaml:"path"` - NewValue string `yaml:"newValue,omitempty"` + Value string `yaml:"value,omitempty"` } type Conditions struct { @@ -111,7 +111,7 @@ func (r *ResourceModifierRule) PatchArrayToByteArray() ([]byte, error) { } func (p *JsonPatch) ToString() string { - return fmt.Sprintf(`{"op": "%s", "path": "%s", "value": "%s"}`, p.Operation, p.Path, p.NewValue) + return fmt.Sprintf(`{"op": "%s", "path": "%s", "value": "%s"}`, p.Operation, p.Path, p.Value) } func ApplyPatch(patch []byte, obj *unstructured.Unstructured, log logrus.FieldLogger) error { diff --git a/internal/resourcemodifiers/resource_modifiers_test.go b/internal/resourcemodifiers/resource_modifiers_test.go index 8ce619c55..f18cbee86 100644 --- a/internal/resourcemodifiers/resource_modifiers_test.go +++ b/internal/resourcemodifiers/resource_modifiers_test.go @@ -18,7 +18,7 @@ func TestGetResourceModifiersFromConfig(t *testing.T) { Namespace: "test-namespace", }, Data: map[string]string{ - "sub.yml": "# kubectl delete cm jsonsub-configmap -n velero\n# kubectl create cm jsonsub-configmap --from-file /home/anshulahuja/workspace/fork-velero/tilt-resources/examples/sub.yml -nvelero\nversion: v1\nresourceModifierRules:\n- conditions:\n groupKind: persistentvolumeclaims.storage.k8s.io\n resourceNameRegex: \".*\"\n namespaces:\n - bar\n - foo\n patches:\n - operation: replace\n path: \"/spec/storageClassName\"\n newValue: \"premium\"\n - operation: remove\n path: \"/metadata/labels/test\"\n\n\n", + "sub.yml": "# kubectl delete cm jsonsub-configmap -n velero\n# kubectl create cm jsonsub-configmap --from-file /home/anshulahuja/workspace/fork-velero/tilt-resources/examples/sub.yml -nvelero\nversion: v1\nresourceModifierRules:\n- conditions:\n groupKind: persistentvolumeclaims.storage.k8s.io\n resourceNameRegex: \".*\"\n namespaces:\n - bar\n - foo\n patches:\n - operation: replace\n path: \"/spec/storageClassName\"\n Value: \"premium\"\n - operation: remove\n path: \"/metadata/labels/test\"\n\n\n", }, } @@ -44,7 +44,7 @@ func TestGetResourceModifiersFromConfig(t *testing.T) { { Operation: "replace", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, { Operation: "remove", @@ -115,10 +115,15 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) { Namespaces: []string{"foo"}, }, Patches: []JsonPatch{ + { + Operation: "test", + Path: "/spec/storageClassName", + Value: "standard", + }, { Operation: "replace", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, }, }, diff --git a/internal/resourcemodifiers/resource_modifiers_validator_test.go b/internal/resourcemodifiers/resource_modifiers_validator_test.go index 5eaa8b3ac..95cb6db60 100644 --- a/internal/resourcemodifiers/resource_modifiers_validator_test.go +++ b/internal/resourcemodifiers/resource_modifiers_validator_test.go @@ -29,7 +29,7 @@ func TestResourceModifiers_Validate(t *testing.T) { { Operation: "replace", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, }, }, @@ -52,7 +52,7 @@ func TestResourceModifiers_Validate(t *testing.T) { { Operation: "replace", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, }, }, @@ -87,7 +87,7 @@ func TestJsonPatch_Validate(t *testing.T) { type fields struct { Operation string Path string - NewValue string + Value string } tests := []struct { name string @@ -99,7 +99,7 @@ func TestJsonPatch_Validate(t *testing.T) { fields: fields{ Operation: "replace", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, wantErr: false, }, @@ -108,7 +108,7 @@ func TestJsonPatch_Validate(t *testing.T) { fields: fields{ Operation: "", Path: "/spec/storageClassName", - NewValue: "premium", + Value: "premium", }, wantErr: true, }, @@ -117,7 +117,7 @@ func TestJsonPatch_Validate(t *testing.T) { fields: fields{ Operation: "replace", Path: "", - NewValue: "premium", + Value: "premium", }, wantErr: true, }, @@ -127,7 +127,7 @@ func TestJsonPatch_Validate(t *testing.T) { p := &JsonPatch{ Operation: tt.fields.Operation, Path: tt.fields.Path, - NewValue: tt.fields.NewValue, + Value: tt.fields.Value, } if err := p.Validate(); (err != nil) != tt.wantErr { t.Errorf("JsonPatch.Validate() error = %v, wantErr %v", err, tt.wantErr)