From 8c53d77111e59d0451162f769fa282da7039209d Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Wed, 17 Jul 2019 14:16:33 -0700 Subject: [PATCH] update edit fix to convert the old patches to patchesStrategicMerge --- pkg/commands/kustfile/kustomizationfile.go | 5 +- .../kustfile/kustomizationfile_test.go | 83 ++++++++++++++ pkg/target/extendedpatch_test.go | 101 ++++++++++++++++++ pkg/target/kusttarget_configplugin.go | 2 +- pkg/types/fix.go | 55 ++++++++++ pkg/types/kustomization.go | 18 +--- plugin/builtin/PatchTransformer.go | 1 + .../patchtransformer/PatchTransformer.go | 1 + 8 files changed, 245 insertions(+), 21 deletions(-) create mode 100644 pkg/types/fix.go diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index 07451a044..2328b6417 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -61,6 +61,7 @@ func determineFieldOrder() []string { "CommonAnnotations", "PatchesStrategicMerge", "PatchesJson6902", + "Patches", "ConfigMapGenerator", "SecretGenerator", "GeneratorOptions", @@ -73,9 +74,7 @@ func determineFieldOrder() []string { } // Add deprecated fields here. - deprecated := map[string]bool{ - "Patches": true, - } + deprecated := map[string]bool{} // Account for the inlined TypeMeta fields. var result []string diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index 0e4d7a2d3..436fc4ea1 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -40,6 +40,7 @@ func TestFieldOrder(t *testing.T) { "CommonAnnotations", "PatchesStrategicMerge", "PatchesJson6902", + "Patches", "ConfigMapGenerator", "SecretGenerator", "GeneratorOptions", @@ -264,3 +265,85 @@ generatorOptions: string(expected), string(bytes)) } } + +func TestFixPatchesField(t *testing.T) { + kustomizationContentWithComments := []byte(` +patches: +- patch1.yaml +- patch2.yaml +`) + + expected := []byte(` +patchesStrategicMerge: +- patch1.yaml +- patch2.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +`) + fSys := fs.MakeFakeFS() + fSys.WriteTestKustomizationWith(kustomizationContentWithComments) + mf, err := NewKustomizationFile(fSys) + if err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + + kustomization, err := mf.Read() + if err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + if err = mf.Write(kustomization); err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + bytes, _ := fSys.ReadFile(mf.path) + + if string(expected) != string(bytes) { + t.Fatalf( + "expected =\n%s\n\nactual =\n%s\n", + string(expected), string(bytes)) + } +} + +func TestFixPatchesFieldForExtendedPatch(t *testing.T) { + kustomizationContentWithComments := []byte(` +patches: +- path: patch1.yaml + target: + kind: Deployment +- path: patch2.yaml + target: + kind: Service +`) + + expected := []byte(` +patches: +- path: patch1.yaml + target: + kind: Deployment +- path: patch2.yaml + target: + kind: Service +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +`) + fSys := fs.MakeFakeFS() + fSys.WriteTestKustomizationWith(kustomizationContentWithComments) + mf, err := NewKustomizationFile(fSys) + if err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + + kustomization, err := mf.Read() + if err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + if err = mf.Write(kustomization); err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + bytes, _ := fSys.ReadFile(mf.path) + + if string(expected) != string(bytes) { + t.Fatalf( + "expected =\n%s\n\nactual =\n%s\n", + string(expected), string(bytes)) + } +} diff --git a/pkg/target/extendedpatch_test.go b/pkg/target/extendedpatch_test.go index f24f209c9..ed9760b3d 100644 --- a/pkg/target/extendedpatch_test.go +++ b/pkg/target/extendedpatch_test.go @@ -923,6 +923,107 @@ spec: `) } +func TestExtendedPatchWithoutTarget(t *testing.T) { + th := kusttest_test.NewKustTestHarness(t, "/app/base") + makeCommonFileForExtendedPatchTest(th) + th.WriteK("/app/base", ` +resources: +- deployment.yaml +- service.yaml +patches: +- path: patch.yaml +`) + th.WriteF("/app/base/patch.yaml", ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: busybox + annotations: + new-key: new-value +`) + m, err := th.MakeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + th.AssertActualEqualsExpected(m, ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + labels: + app: nginx + name: nginx +spec: + template: + metadata: + labels: + app: nginx + spec: + containers: + - image: nginx + name: nginx + volumeMounts: + - mountPath: /tmp/ps + name: nginx-persistent-storage + volumes: + - emptyDir: {} + name: nginx-persistent-storage + - configMap: + name: configmap-in-base + name: configmap-in-base +--- +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + annotations: + new-key: new-value + labels: + app: busybox + name: busybox +spec: + template: + metadata: + labels: + app: busybox + spec: + containers: + - image: busybox + name: busybox + volumeMounts: + - mountPath: /tmp/ps + name: busybox-persistent-storage + volumes: + - emptyDir: {} + name: busybox-persistent-storage + - configMap: + name: configmap-in-base + name: configmap-in-base +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: nginx + name: nginx +spec: + ports: + - port: 80 + selector: + app: nginx +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: busybox + name: busybox +spec: + ports: + - port: 8080 + selector: + app: busybox +`) +} + func TestExtendedPatchNoMatchMultiplePatch(t *testing.T) { th := kusttest_test.NewKustTestHarness(t, "/app/base") makeCommonFileForExtendedPatchTest(th) diff --git a/pkg/target/kusttarget_configplugin.go b/pkg/target/kusttarget_configplugin.go index d621c2021..fc290ee94 100644 --- a/pkg/target/kusttarget_configplugin.go +++ b/pkg/target/kusttarget_configplugin.go @@ -200,7 +200,7 @@ func (kt *KustTarget) configureBuiltinPatchTransformer( Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"` } for _, patch := range kt.kustomization.Patches { - c.Target = &patch.Target + c.Target = patch.Target c.Patch = patch.Patch c.Path = patch.Path p := builtin.NewPatchTransformerPlugin() diff --git a/pkg/types/fix.go b/pkg/types/fix.go new file mode 100644 index 000000000..2b3e493ca --- /dev/null +++ b/pkg/types/fix.go @@ -0,0 +1,55 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +// Package types holds struct definitions that should find a better home. +package types + +import ( + "log" + "regexp" + + "sigs.k8s.io/yaml" +) + +// FixKustomizationPreUnmarshalling modies the raw data +// before marshalling - e.g. changes old field names to +// new field names. +func FixKustomizationPreUnmarshalling(data []byte) []byte { + deprecateFieldsMap := map[string]string{ + "imageTags:": "images:", + } + for oldname, newname := range deprecateFieldsMap { + pattern := regexp.MustCompile(oldname) + data = pattern.ReplaceAll(data, []byte(newname)) + } + + if useLegacyPatch(data) { + pattern := regexp.MustCompile("patches:") + data = pattern.ReplaceAll(data, []byte("patchesStrategicMerge:")) + } + + return data +} + +func useLegacyPatch(data []byte) bool { + found := false + + var object map[string]interface{} + err := yaml.Unmarshal(data, &object) + if err != nil { + log.Fatalf("invalid content from %s\n", string(data)) + } + if rawPatches, ok := object["patches"]; ok { + patches, ok := rawPatches.([]interface{}) + if !ok { + log.Fatalf("invalid patches from %v\n", rawPatches) + } + for _, p := range patches { + _, ok := p.(string) + if ok { + found = true + } + } + } + return found +} diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 0f1f43368..a56dd9bcb 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -5,8 +5,6 @@ package types import ( - "regexp" - "sigs.k8s.io/kustomize/v3/pkg/gvk" "sigs.k8s.io/kustomize/v3/pkg/image" ) @@ -195,20 +193,6 @@ func (k *Kustomization) EnforceFields() []string { return errs } -// FixKustomizationPreUnmarshalling modies the raw data -// before marshalling - e.g. changes old field names to -// new field names. -func FixKustomizationPreUnmarshalling(data []byte) []byte { - deprecateFieldsMap := map[string]string{ - "imageTags:": "images:", - } - for oldname, newname := range deprecateFieldsMap { - pattern := regexp.MustCompile(oldname) - data = pattern.ReplaceAll(data, []byte(newname)) - } - return data -} - // GeneratorArgs contains arguments common to generators. type GeneratorArgs struct { // Namespace for the configmap, optional @@ -385,7 +369,7 @@ type Patch struct { Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` // Target points to the resources that the patch is applied to - Target Selector `json:"target,omitempty" yaml:"target,omitempty"` + Target *Selector `json:"target,omitempty" yaml:"target,omitempty"` } // Selector specifies a set of resources. diff --git a/plugin/builtin/PatchTransformer.go b/plugin/builtin/PatchTransformer.go index f243a1560..2ab410ede 100644 --- a/plugin/builtin/PatchTransformer.go +++ b/plugin/builtin/PatchTransformer.go @@ -87,6 +87,7 @@ func (p *PatchTransformerPlugin) Transform(m resmap.ResMap) error { if err != nil { return err } + return nil } if p.Target == nil { diff --git a/plugin/builtin/patchtransformer/PatchTransformer.go b/plugin/builtin/patchtransformer/PatchTransformer.go index 16f82198c..f5d766039 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer.go +++ b/plugin/builtin/patchtransformer/PatchTransformer.go @@ -88,6 +88,7 @@ func (p *plugin) Transform(m resmap.ResMap) error { if err != nil { return err } + return nil } if p.Target == nil {