for yaml-formatted JSON6902 patches, convert first to json, then use the same code as for json-formatted JSON6902 patches

This commit is contained in:
David Schweikert
2018-10-05 21:47:20 +02:00
parent 489f6e2e67
commit 3d4bf3abbf
4 changed files with 11 additions and 197 deletions

View File

@@ -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 {

View File

@@ -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{

View File

@@ -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
}

View File

@@ -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)
}
}