Signed-off-by: Anshul Ahuja <anshulahuja@microsoft.com>
pull/6452/head
Anshul Ahuja 2023-07-06 10:31:05 +05:30
parent 6307a43004
commit a00cf9ad2c
3 changed files with 17 additions and 12 deletions

View File

@ -23,7 +23,7 @@ const (
type JsonPatch struct { type JsonPatch struct {
Operation string `yaml:"operation"` Operation string `yaml:"operation"`
Path string `yaml:"path"` Path string `yaml:"path"`
NewValue string `yaml:"newValue,omitempty"` Value string `yaml:"value,omitempty"`
} }
type Conditions struct { type Conditions struct {
@ -111,7 +111,7 @@ func (r *ResourceModifierRule) PatchArrayToByteArray() ([]byte, error) {
} }
func (p *JsonPatch) ToString() string { 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 { func ApplyPatch(patch []byte, obj *unstructured.Unstructured, log logrus.FieldLogger) error {

View File

@ -18,7 +18,7 @@ func TestGetResourceModifiersFromConfig(t *testing.T) {
Namespace: "test-namespace", Namespace: "test-namespace",
}, },
Data: map[string]string{ 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", Operation: "replace",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
{ {
Operation: "remove", Operation: "remove",
@ -115,10 +115,15 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) {
Namespaces: []string{"foo"}, Namespaces: []string{"foo"},
}, },
Patches: []JsonPatch{ Patches: []JsonPatch{
{
Operation: "test",
Path: "/spec/storageClassName",
Value: "standard",
},
{ {
Operation: "replace", Operation: "replace",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
}, },
}, },

View File

@ -29,7 +29,7 @@ func TestResourceModifiers_Validate(t *testing.T) {
{ {
Operation: "replace", Operation: "replace",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
}, },
}, },
@ -52,7 +52,7 @@ func TestResourceModifiers_Validate(t *testing.T) {
{ {
Operation: "replace", Operation: "replace",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
}, },
}, },
@ -87,7 +87,7 @@ func TestJsonPatch_Validate(t *testing.T) {
type fields struct { type fields struct {
Operation string Operation string
Path string Path string
NewValue string Value string
} }
tests := []struct { tests := []struct {
name string name string
@ -99,7 +99,7 @@ func TestJsonPatch_Validate(t *testing.T) {
fields: fields{ fields: fields{
Operation: "replace", Operation: "replace",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
wantErr: false, wantErr: false,
}, },
@ -108,7 +108,7 @@ func TestJsonPatch_Validate(t *testing.T) {
fields: fields{ fields: fields{
Operation: "", Operation: "",
Path: "/spec/storageClassName", Path: "/spec/storageClassName",
NewValue: "premium", Value: "premium",
}, },
wantErr: true, wantErr: true,
}, },
@ -117,7 +117,7 @@ func TestJsonPatch_Validate(t *testing.T) {
fields: fields{ fields: fields{
Operation: "replace", Operation: "replace",
Path: "", Path: "",
NewValue: "premium", Value: "premium",
}, },
wantErr: true, wantErr: true,
}, },
@ -127,7 +127,7 @@ func TestJsonPatch_Validate(t *testing.T) {
p := &JsonPatch{ p := &JsonPatch{
Operation: tt.fields.Operation, Operation: tt.fields.Operation,
Path: tt.fields.Path, Path: tt.fields.Path,
NewValue: tt.fields.NewValue, Value: tt.fields.Value,
} }
if err := p.Validate(); (err != nil) != tt.wantErr { if err := p.Validate(); (err != nil) != tt.wantErr {
t.Errorf("JsonPatch.Validate() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("JsonPatch.Validate() error = %v, wantErr %v", err, tt.wantErr)