From a85f297f3147ef0aeaae32b15de54221d8410af6 Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Mon, 15 Jul 2019 15:17:33 -0700 Subject: [PATCH 1/4] enable extended patch transformer and add tests --- pkg/target/extendedpatch_test.go | 924 ++++++++++++++++++++++++++ pkg/target/kusttarget_configplugin.go | 27 +- pkg/types/kustomization.go | 1 - 3 files changed, 950 insertions(+), 2 deletions(-) create mode 100644 pkg/target/extendedpatch_test.go diff --git a/pkg/target/extendedpatch_test.go b/pkg/target/extendedpatch_test.go new file mode 100644 index 000000000..dc647ad7d --- /dev/null +++ b/pkg/target/extendedpatch_test.go @@ -0,0 +1,924 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package target_test + +import ( + "testing" + + "sigs.k8s.io/kustomize/v3/pkg/kusttest" +) + +func makeCommonFileForExtendedPatchTest(th *kusttest_test.KustTestHarness) { + th.WriteF("/app/base/deployment.yaml", ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: nginx + labels: + app: nginx +spec: + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - name: nginx-persistent-storage + mountPath: /tmp/ps + volumes: + - name: nginx-persistent-storage + emptyDir: {} + - configMap: + name: configmap-in-base + name: configmap-in-base +--- +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: busybox + labels: + app: busybox +spec: + template: + metadata: + labels: + app: busybox + spec: + containers: + - name: busybox + image: busybox + volumeMounts: + - name: busybox-persistent-storage + mountPath: /tmp/ps + volumes: + - name: busybox-persistent-storage + emptyDir: {} + - configMap: + name: configmap-in-base + name: configmap-in-base +`) + th.WriteF("/app/base/service.yaml", ` +apiVersion: v1 +kind: Service +metadata: + name: nginx + labels: + app: nginx +spec: + ports: + - port: 80 + selector: + app: nginx +--- +apiVersion: v1 +kind: Service +metadata: + name: busybox + labels: + app: busybox +spec: + ports: + - port: 8080 + selector: + app: busybox +`) +} + +func TestExtendedPatchNameSelector(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 + target: + name: busybox +`) + 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: + annotations: + new-key: new-value + labels: + app: busybox + name: busybox +spec: + ports: + - port: 8080 + selector: + app: busybox +`) +} + +func TestExtendedPatchGvkSelector(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 + target: + kind: Deployment +`) + 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: + annotations: + new-key: new-value + 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 TestExtendedPatchLabelSelector(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 + target: + labelSelector: app=nginx +`) + th.WriteF("/app/base/patch.yaml", ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: nginx + 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: + annotations: + new-key: new-value + 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: + 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: + annotations: + new-key: new-value + 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 TestExtendedPatchNameGvkSelector(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 + target: + name: busybox + kind: Deployment +`) + 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 TestExtendedPatchNameLabelSelector(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 + target: + name: .* + labelSelector: app=busybox +`) + 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: + annotations: + new-key: new-value + labels: + app: busybox + name: busybox +spec: + ports: + - port: 8080 + selector: + app: busybox +`) +} + +func TestExtendedPatchGvkLabelSelector(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 + target: + kind: Deployment + labelSelector: app=busybox +`) + 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 TestExtendedPatchNameGvkLabelSelector(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 + target: + name: busybox + kind: Deployment + labelSelector: app=busybox +`) + 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 TestExtendedPatchNoMatch(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 + target: + name: no-match +`) + 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: + 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 +`) +} diff --git a/pkg/target/kusttarget_configplugin.go b/pkg/target/kusttarget_configplugin.go index e616f4334..d621c2021 100644 --- a/pkg/target/kusttarget_configplugin.go +++ b/pkg/target/kusttarget_configplugin.go @@ -64,6 +64,7 @@ func (kt *KustTarget) configureBuiltinTransformers( // - patch SMP configurators := []transformerConfigurator{ kt.configureBuiltinPatchStrategicMergeTransformer, + kt.configureBuiltinPatchTransformer, kt.configureBuiltinNamespaceTransformer, kt.configureBuiltinNameTransformer, kt.configureBuiltinLabelTransformer, @@ -170,7 +171,6 @@ func (kt *KustTarget) configureBuiltinPatchStrategicMergeTransformer( tConfig *config.TransformerConfig) ( result []transformers.Transformer, err error) { if len(kt.kustomization.PatchesStrategicMerge) == 0 { - result = append(result, transformers.NewNoOpTransformer()) return } var c struct { @@ -188,6 +188,31 @@ func (kt *KustTarget) configureBuiltinPatchStrategicMergeTransformer( return } +func (kt *KustTarget) configureBuiltinPatchTransformer( + tConfig *config.TransformerConfig) ( + result []transformers.Transformer, err error) { + if len(kt.kustomization.Patches) == 0 { + return + } + var c struct { + Path string `json:"path,omitempty" yaml:"path,omitempty"` + Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` + Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"` + } + for _, patch := range kt.kustomization.Patches { + c.Target = &patch.Target + c.Patch = patch.Patch + c.Path = patch.Path + p := builtin.NewPatchTransformerPlugin() + err = kt.configureBuiltinPlugin(p, c, "patch") + if err != nil { + return nil, err + } + result = append(result, p) + } + return +} + func (kt *KustTarget) configureBuiltinLabelTransformer( tConfig *config.TransformerConfig) ( result []transformers.Transformer, err error) { diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 0d46fd90d..0f1f43368 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -200,7 +200,6 @@ func (k *Kustomization) EnforceFields() []string { // new field names. func FixKustomizationPreUnmarshalling(data []byte) []byte { deprecateFieldsMap := map[string]string{ - "patches:": "patchesStrategicMerge:", "imageTags:": "images:", } for oldname, newname := range deprecateFieldsMap { From 8121467c1ed68076408374966d91b097b6815d00 Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Mon, 15 Jul 2019 16:01:23 -0700 Subject: [PATCH 2/4] fix the ci failure --- .../PatchStrategicMergeTransformer_test.go | 24 +++++++++---------- .../patchtransformer/PatchTransformer_test.go | 19 ++++++++------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go b/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go index 6dc740795..1d2b4389c 100644 --- a/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go +++ b/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go @@ -8,7 +8,7 @@ import ( "testing" "sigs.k8s.io/kustomize/v3/pkg/kusttest" - "sigs.k8s.io/kustomize/v3/pkg/plugins" + plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test" ) const ( @@ -58,7 +58,7 @@ spec: ) func TestPatchStrategicMergeTransformerMissingFile(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -83,7 +83,7 @@ paths: } func TestBadPatchStrategicMergeTransformer(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -108,7 +108,7 @@ patches: 'thisIsNotAPatch' } func TestBothEmptyPatchStrategicMergeTransformer(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -131,7 +131,7 @@ metadata: } func TestPatchStrategicMergeTransformerFromFiles(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -181,7 +181,7 @@ spec: } func TestPatchStrategicMergeTransformerWithInline(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -216,7 +216,7 @@ spec: } func TestPatchStrategicMergeTransformerMultiplePatches(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -293,7 +293,7 @@ spec: } func TestStrategicMergeTransformerMultiplePatchesWithConflicts(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -354,7 +354,7 @@ paths: } func TestStrategicMergeTransformerWrongNamespace(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -397,7 +397,7 @@ paths: } func TestStrategicMergeTransformerNoSchema(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -437,7 +437,7 @@ spec: } func TestStrategicMergeTransformerNoSchemaMultiPatches(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -493,7 +493,7 @@ spec: } func TestStrategicMergeTransformerNoSchemaMultiPatchesWithConflict(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( diff --git a/plugin/builtin/patchtransformer/PatchTransformer_test.go b/plugin/builtin/patchtransformer/PatchTransformer_test.go index 9e5787fb1..622df0132 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer_test.go +++ b/plugin/builtin/patchtransformer/PatchTransformer_test.go @@ -4,10 +4,11 @@ package main_test import ( - kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest" - "sigs.k8s.io/kustomize/v3/pkg/plugins" "strings" "testing" + + kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest" + plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test" ) const ( @@ -65,7 +66,7 @@ spec: ) func TestPatchTransformerMissingFile(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -89,7 +90,7 @@ path: patch.yaml } func TestPatchTransformerBadPatch(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -113,7 +114,7 @@ patch: "thisIsNotAPatch" } func TestPatchTransformerMissingSelector(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -137,7 +138,7 @@ patch: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "Cluste } func TestPatchTransformerBothEmptyPathAndPatch(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -160,7 +161,7 @@ metadata: } func TestPatchTransformerBothNonEmptyPathAndPatch(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -185,7 +186,7 @@ Patch: "something" } func TestPatchTransformerFromFiles(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -267,7 +268,7 @@ spec: } func TestPatchTransformerWithInline(t *testing.T) { - tc := plugins.NewEnvForTest(t).Set() + tc := plugins_test.NewEnvForTest(t).Set() defer tc.Reset() tc.BuildGoPlugin( From 6f7441962824d932a5160cddfb35e1b0082b162b Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Mon, 15 Jul 2019 16:34:13 -0700 Subject: [PATCH 3/4] fix local test failures --- pkg/target/chartinflatorplugin_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/target/chartinflatorplugin_test.go b/pkg/target/chartinflatorplugin_test.go index 73bfffb98..6349eeda9 100644 --- a/pkg/target/chartinflatorplugin_test.go +++ b/pkg/target/chartinflatorplugin_test.go @@ -8,10 +8,10 @@ package target_test import ( - "sigs.k8s.io/kustomize/v3/pkg/plugins" "testing" - "sigs.k8s.io/kustomize/v3/pkg/kusttest" + kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest" + plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test" ) // This is an example of using a helm chart as a base, From ed0cfc685b56318a55ec129356a61f318775ea88 Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Tue, 16 Jul 2019 15:16:00 -0700 Subject: [PATCH 4/4] add test for extended patch with overlapping patches --- pkg/target/extendedpatch_test.go | 223 ++++++++++++++++++ plugin/builtin/PatchTransformer.go | 9 +- .../patchtransformer/PatchTransformer.go | 9 +- 3 files changed, 233 insertions(+), 8 deletions(-) diff --git a/pkg/target/extendedpatch_test.go b/pkg/target/extendedpatch_test.go index dc647ad7d..f24f209c9 100644 --- a/pkg/target/extendedpatch_test.go +++ b/pkg/target/extendedpatch_test.go @@ -922,3 +922,226 @@ spec: app: busybox `) } + +func TestExtendedPatchNoMatchMultiplePatch(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 + target: + name: no-match +- path: patch.yaml + target: + name: busybox + kind: Job +`) + 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: + 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 TestExtendedPatchMultiplePatchOverlapping(t *testing.T) { + th := kusttest_test.NewKustTestHarness(t, "/app/base") + makeCommonFileForExtendedPatchTest(th) + th.WriteK("/app/base", ` +resources: +- deployment.yaml +- service.yaml +patches: +- path: patch1.yaml + target: + labelSelector: app=busybox +- path: patch2.yaml + target: + name: busybox + kind: Deployment +`) + th.WriteF("/app/base/patch1.yaml", ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: busybox + annotations: + new-key-from-patch1: new-value +`) + th.WriteF("/app/base/patch2.yaml", ` +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: busybox + annotations: + new-key-from-patch2: 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-from-patch1: new-value + new-key-from-patch2: 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: + annotations: + new-key-from-patch1: new-value + labels: + app: busybox + name: busybox +spec: + ports: + - port: 8080 + selector: + app: busybox +`) +} diff --git a/plugin/builtin/PatchTransformer.go b/plugin/builtin/PatchTransformer.go index 7eefd39c1..f243a1560 100644 --- a/plugin/builtin/PatchTransformer.go +++ b/plugin/builtin/PatchTransformer.go @@ -114,10 +114,11 @@ func (p *PatchTransformerPlugin) Transform(m resmap.ResMap) error { } } if p.loadedPatch != nil { - p.loadedPatch.SetName(resource.GetName()) - p.loadedPatch.SetNamespace(resource.GetNamespace()) - p.loadedPatch.SetGvk(resource.GetGvk()) - err = resource.Patch(p.loadedPatch.Kunstructured) + patchCopy := p.loadedPatch.DeepCopy() + patchCopy.SetName(resource.GetName()) + patchCopy.SetNamespace(resource.GetNamespace()) + patchCopy.SetGvk(resource.GetGvk()) + err = resource.Patch(patchCopy.Kunstructured) if err != nil { return err } diff --git a/plugin/builtin/patchtransformer/PatchTransformer.go b/plugin/builtin/patchtransformer/PatchTransformer.go index 1b02717ba..16f82198c 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer.go +++ b/plugin/builtin/patchtransformer/PatchTransformer.go @@ -115,10 +115,11 @@ func (p *plugin) Transform(m resmap.ResMap) error { } } if p.loadedPatch != nil { - p.loadedPatch.SetName(resource.GetName()) - p.loadedPatch.SetNamespace(resource.GetNamespace()) - p.loadedPatch.SetGvk(resource.GetGvk()) - err = resource.Patch(p.loadedPatch.Kunstructured) + patchCopy := p.loadedPatch.DeepCopy() + patchCopy.SetName(resource.GetName()) + patchCopy.SetNamespace(resource.GetNamespace()) + patchCopy.SetGvk(resource.GetGvk()) + err = resource.Patch(patchCopy.Kunstructured) if err != nil { return err }