From a85f297f3147ef0aeaae32b15de54221d8410af6 Mon Sep 17 00:00:00 2001 From: jingfangliu Date: Mon, 15 Jul 2019 15:17:33 -0700 Subject: [PATCH] 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 {