From 3d4bf3abbf30b606e0b118c97372c102ad214c76 Mon Sep 17 00:00:00 2001 From: David Schweikert Date: Fri, 5 Oct 2018 21:47:20 +0200 Subject: [PATCH] for yaml-formatted JSON6902 patches, convert first to json, then use the same code as for json-formatted JSON6902 patches --- pkg/patch/transformer/factory.go | 14 +- .../transformer/patchjson6902json_test.go | 3 + pkg/patch/transformer/patchjson6902yaml.go | 62 --------- .../transformer/patchjson6902yaml_test.go | 129 ------------------ 4 files changed, 11 insertions(+), 197 deletions(-) delete mode 100644 pkg/patch/transformer/patchjson6902yaml.go delete mode 100644 pkg/patch/transformer/patchjson6902yaml_test.go diff --git a/pkg/patch/transformer/factory.go b/pkg/patch/transformer/factory.go index 6f85e9744..b7eb25839 100644 --- a/pkg/patch/transformer/factory.go +++ b/pkg/patch/transformer/factory.go @@ -22,7 +22,7 @@ import ( "sigs.k8s.io/kustomize/pkg/resid" "github.com/evanphx/json-patch" - "github.com/krishicks/yaml-patch" + "github.com/ghodss/yaml" "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/transformers" @@ -76,18 +76,20 @@ func (f PatchJson6902Factory) makeOnePatchJson6902Transformer(p patch.Json6902) if err != nil { return nil, err } - if isJsonFormat(rawOp) { - decodedPatch, err := jsonpatch.DecodePatch(rawOp) + + if !isJsonFormat(rawOp) { + // if it isn't JSON, try to parse it as YAML + rawOp, err = yaml.YAMLToJSON(rawOp) if err != nil { return nil, err } - return newPatchJson6902JSONTransformer(targetId, decodedPatch) } - decodedPatch, err := yamlpatch.DecodePatch(rawOp) + + decodedPatch, err := jsonpatch.DecodePatch(rawOp) if err != nil { return nil, err } - return newPatchJson6902YAMLTransformer(targetId, decodedPatch) + return newPatchJson6902JSONTransformer(targetId, decodedPatch) } func isJsonFormat(data []byte) bool { diff --git a/pkg/patch/transformer/patchjson6902json_test.go b/pkg/patch/transformer/patchjson6902json_test.go index 180cc77ae..3d5e2d8ad 100644 --- a/pkg/patch/transformer/patchjson6902json_test.go +++ b/pkg/patch/transformer/patchjson6902json_test.go @@ -21,11 +21,14 @@ import ( "testing" "github.com/evanphx/json-patch" + "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" ) +var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"} + func TestJsonPatchJSONTransformer_Transform(t *testing.T) { id := resid.NewResId(deploy, "deploy1") base := resmap.ResMap{ diff --git a/pkg/patch/transformer/patchjson6902yaml.go b/pkg/patch/transformer/patchjson6902yaml.go deleted file mode 100644 index cafdf25d7..000000000 --- a/pkg/patch/transformer/patchjson6902yaml.go +++ /dev/null @@ -1,62 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package transformer - -import ( - "github.com/ghodss/yaml" - "github.com/krishicks/yaml-patch" - "sigs.k8s.io/kustomize/pkg/resid" - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/transformers" -) - -// patchJson6902YAMLTransformer applies patches. -type patchJson6902YAMLTransformer struct { - target resid.ResId - patch yamlpatch.Patch -} - -var _ transformers.Transformer = &patchJson6902YAMLTransformer{} - -// newPatchJson6902YAMLTransformer constructs a PatchJson6902 transformer. -func newPatchJson6902YAMLTransformer(t resid.ResId, p yamlpatch.Patch) (transformers.Transformer, error) { - if len(p) == 0 { - return transformers.NewNoOpTransformer(), nil - } - return &patchJson6902YAMLTransformer{target: t, patch: p}, nil -} - -// Transform apply the json patches on top of the base resources. -func (t *patchJson6902YAMLTransformer) Transform(baseResourceMap resmap.ResMap) error { - obj, err := findTargetObj(baseResourceMap, t.target) - if obj == nil { - return err - } - rawObj, err := yaml.Marshal(obj.Unstructured.Object) - if err != nil { - return err - } - modifiedObj, err := t.patch.Apply(rawObj) - if err != nil { - return err - } - err = yaml.Unmarshal(modifiedObj, &obj.Unstructured.Object) - if err != nil { - return err - } - return nil -} diff --git a/pkg/patch/transformer/patchjson6902yaml_test.go b/pkg/patch/transformer/patchjson6902yaml_test.go deleted file mode 100644 index 414a1f457..000000000 --- a/pkg/patch/transformer/patchjson6902yaml_test.go +++ /dev/null @@ -1,129 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package transformer - -import ( - "reflect" - "testing" - - "github.com/krishicks/yaml-patch" - "sigs.k8s.io/kustomize/pkg/gvk" - "sigs.k8s.io/kustomize/pkg/resid" - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/resource" -) - -var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"} - -func TestJsonPatchYAMLTransformer_Transform(t *testing.T) { - id := resid.NewResId(deploy, "deploy1") - base := resmap.ResMap{ - id: resource.NewResourceFromMap( - map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deploy1", - }, - "spec": map[string]interface{}{ - "template": map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "old-label": "old-value", - }, - }, - "spec": map[string]interface{}{ - "containers": []interface{}{ - map[string]interface{}{ - "name": "nginx", - "image": "nginx", - }, - }, - }, - }, - }, - }), - } - - var image, replica, command interface{} - image = "my-nginx" - replica = "3" - command = []string{"arg1", "arg2", "arg3"} - patch := yamlpatch.Patch{ - { - Op: "replace", - Path: "/spec/template/spec/containers/0/name", - Value: yamlpatch.NewNode(&image), - }, - { - Op: "add", - Path: "/spec/replica", - Value: yamlpatch.NewNode(&replica), - }, - { - Op: "add", - Path: "/spec/template/spec/containers/0/command", - Value: yamlpatch.NewNode(&command), - }, - } - - expected := resmap.ResMap{ - id: resource.NewResourceFromMap( - map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deploy1", - }, - "spec": map[string]interface{}{ - "replica": "3", - "template": map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "old-label": "old-value", - }, - }, - "spec": map[string]interface{}{ - "containers": []interface{}{ - map[string]interface{}{ - "image": "nginx", - "name": "my-nginx", - "command": []interface{}{ - "arg1", - "arg2", - "arg3", - }, - }, - }, - }, - }, - }, - }), - } - jpt, err := newPatchJson6902YAMLTransformer(id, patch) - if err != nil { - t.Fatalf("unexpected error : %v", err) - } - err = jpt.Transform(base) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if !reflect.DeepEqual(base, expected) { - err = expected.ErrorIfNotEqual(base) - t.Fatalf("actual doesn't match expected: %v", err) - } -}