From 463b7764867c961da04e421dc73e5cdf28fecf32 Mon Sep 17 00:00:00 2001 From: msk- Date: Tue, 19 Jan 2021 17:41:44 +0000 Subject: [PATCH 1/6] Initial test commit --- api/krusty/diamondpatchref_test.go | 204 +++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 api/krusty/diamondpatchref_test.go diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go new file mode 100644 index 000000000..0d7b1ac41 --- /dev/null +++ b/api/krusty/diamondpatchref_test.go @@ -0,0 +1,204 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package krusty_test + +import ( + "testing" + + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" +) + +// Coverage for issue #2609 +func TestNamePrefixSuffixPatch(t *testing.T) { + th := kusttest_test.MakeHarness(t) + + th.WriteK("bottom/kustomization.yaml", `configMapGenerator: +- name: bottom + literals: + - KEY=value +`) + + th.WriteK("left-service/kustomization.yaml", `resources: +- deployment.yaml +`) + + th.WriteF("left-service/deployment.yaml", `apiVersion: apps/v1 +kind: Deployment +metadata: + name: left-deploy + labels: + app.kubernetes.io/name: left-deploy +spec: + selector: + matchLabels: + app.kubernetes.io/name: left-pod + template: + metadata: + labels: + app.kubernetes.io/name: left-pod + spec: + containers: + - image: left-image:v1.0 + name: service +`) + + th.WriteK("right-service/kustomization.yaml", `resources: +- deployment.yaml +`) + + th.WriteF("right-service/deployment.yaml", `apiVersion: apps/v1 +kind: Deployment +metadata: + name: right-deploy + labels: + app.kubernetes.io/name: right-deploy +spec: + selector: + matchLabels: + app.kubernetes.io/name: right-pod + template: + metadata: + labels: + app.kubernetes.io/name: right-pod + spec: + containers: + - image: right-image:v1.0 + name: service +`) + + th.WriteK("top/kustomization.yaml", `resources: +- ./left +- ./right +`) + + th.WriteK("top/left/kustomization.yaml", `resources: +- ../../left-service +- ./bottom + +patches: +- target: + kind: Deployment + name: left-deploy + patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: ignored-by-kustomize + spec: + template: + spec: + containers: + - name: service + env: + - name: MAPPED_CONFIG_ITEM + valueFrom: + configMapKeyRef: + name: left-bottom + key: KEY +`) + + th.WriteK("top/left/bottom/kustomization.yaml", `namePrefix: left- + +resources: + - ../../../bottom +`) + + th.WriteK("top/right/kustomization.yaml", `resources: +- ../../right-service +- ./bottom + +patches: +- target: + kind: Deployment + name: right-deploy + patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: ignored-by-kustomize + spec: + template: + spec: + containers: + - name: service + env: + - name: MAPPED_CONFIG_ITEM + valueFrom: + configMapKeyRef: + name: right-bottom + key: KEY +`) + + th.WriteK("top/right/bottom/kustomization.yaml", `namePrefix: right- + +resources: + - ../../../bottom +`) + + m := th.Run("top", th.MakeDefaultOptions()) + // Per #2609, the desired behavior is for configMapRef.name and configMapKeyRef.name to be "mysql-9792mdchtg" not "mysql" + th.AssertActualEqualsExpected(m, `apiVersion: v1 +data: + KEY: value +kind: ConfigMap +metadata: + name: left-bottom-9f2t6f5h6d +--- +apiVersion: v1 +data: + KEY: value +kind: ConfigMap +metadata: + name: right-bottom-9f2t6f5h6d +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app.kubernetes.io/name: left-deploy + name: left-deploy +spec: + selector: + matchLabels: + app.kubernetes.io/name: left-pod + template: + metadata: + labels: + app.kubernetes.io/name: left-pod + spec: + containers: + - env: + - name: MAPPED_CONFIG_ITEM + valueFrom: + configMapKeyRef: + key: KEY + name: left-bottom-9f2t6f5h6d + image: left-image:v1.0 + name: service +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app.kubernetes.io/name: right-deploy + name: right-deploy +spec: + selector: + matchLabels: + app.kubernetes.io/name: right-pod + template: + metadata: + labels: + app.kubernetes.io/name: right-pod + spec: + containers: + - env: + - name: MAPPED_CONFIG_ITEM + valueFrom: + configMapKeyRef: + key: KEY + name: right-bottom-9f2t6f5h6d + image: right-image:v1.0 + name: service`) +} From d8f2d2256d1283612fa3ef2650010f76e0a3391d Mon Sep 17 00:00:00 2001 From: msk- Date: Tue, 19 Jan 2021 17:57:42 +0000 Subject: [PATCH 2/6] Fixed test --- api/krusty/diamondpatchref_test.go | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go index 0d7b1ac41..5652562fc 100644 --- a/api/krusty/diamondpatchref_test.go +++ b/api/krusty/diamondpatchref_test.go @@ -9,17 +9,16 @@ import ( kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -// Coverage for issue #2609 func TestNamePrefixSuffixPatch(t *testing.T) { th := kusttest_test.MakeHarness(t) - th.WriteK("bottom/kustomization.yaml", `configMapGenerator: + th.WriteK("bottom", `configMapGenerator: - name: bottom literals: - KEY=value `) - th.WriteK("left-service/kustomization.yaml", `resources: + th.WriteK("left-service", `resources: - deployment.yaml `) @@ -43,7 +42,7 @@ spec: name: service `) - th.WriteK("right-service/kustomization.yaml", `resources: + th.WriteK("right-service", `resources: - deployment.yaml `) @@ -67,12 +66,12 @@ spec: name: service `) - th.WriteK("top/kustomization.yaml", `resources: + th.WriteK("top", `resources: - ./left - ./right `) - th.WriteK("top/left/kustomization.yaml", `resources: + th.WriteK("top/left", `resources: - ../../left-service - ./bottom @@ -98,13 +97,13 @@ patches: key: KEY `) - th.WriteK("top/left/bottom/kustomization.yaml", `namePrefix: left- + th.WriteK("top/left/bottom", `namePrefix: left- resources: - ../../../bottom `) - th.WriteK("top/right/kustomization.yaml", `resources: + th.WriteK("top/right", `resources: - ../../right-service - ./bottom @@ -130,7 +129,7 @@ patches: key: KEY `) - th.WriteK("top/right/bottom/kustomization.yaml", `namePrefix: right- + th.WriteK("top/right/bottom", `namePrefix: right- resources: - ../../../bottom @@ -138,21 +137,7 @@ resources: m := th.Run("top", th.MakeDefaultOptions()) // Per #2609, the desired behavior is for configMapRef.name and configMapKeyRef.name to be "mysql-9792mdchtg" not "mysql" - th.AssertActualEqualsExpected(m, `apiVersion: v1 -data: - KEY: value -kind: ConfigMap -metadata: - name: left-bottom-9f2t6f5h6d ---- -apiVersion: v1 -data: - KEY: value -kind: ConfigMap -metadata: - name: right-bottom-9f2t6f5h6d ---- -apiVersion: apps/v1 + th.AssertActualEqualsExpected(m, `apiVersion: apps/v1 kind: Deployment metadata: labels: @@ -173,10 +158,17 @@ spec: valueFrom: configMapKeyRef: key: KEY - name: left-bottom-9f2t6f5h6d + name: left-bottom image: left-image:v1.0 name: service --- +apiVersion: v1 +data: + KEY: value +kind: ConfigMap +metadata: + name: left-bottom-9f2t6f5h6d +--- apiVersion: apps/v1 kind: Deployment metadata: @@ -198,7 +190,15 @@ spec: valueFrom: configMapKeyRef: key: KEY - name: right-bottom-9f2t6f5h6d + name: right-bottom image: right-image:v1.0 - name: service`) + name: service +--- +apiVersion: v1 +data: + KEY: value +kind: ConfigMap +metadata: + name: right-bottom-9f2t6f5h6d +`) } From 7ac37867dc87e7cfddd760f2df9534a61b369b2d Mon Sep 17 00:00:00 2001 From: msk- Date: Tue, 19 Jan 2021 18:00:02 +0000 Subject: [PATCH 3/6] tidy-up, added desired behaviour --- api/krusty/diamondpatchref_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go index 5652562fc..76747ce76 100644 --- a/api/krusty/diamondpatchref_test.go +++ b/api/krusty/diamondpatchref_test.go @@ -100,7 +100,7 @@ patches: th.WriteK("top/left/bottom", `namePrefix: left- resources: - - ../../../bottom +- ../../../bottom `) th.WriteK("top/right", `resources: @@ -132,11 +132,11 @@ patches: th.WriteK("top/right/bottom", `namePrefix: right- resources: - - ../../../bottom +- ../../../bottom `) m := th.Run("top", th.MakeDefaultOptions()) - // Per #2609, the desired behavior is for configMapRef.name and configMapKeyRef.name to be "mysql-9792mdchtg" not "mysql" + // Desired behaviour: configMapKeyRef should reference ConfigMap names. th.AssertActualEqualsExpected(m, `apiVersion: apps/v1 kind: Deployment metadata: From eda827c317bd3372b58c0e414f41079f54a6d9d5 Mon Sep 17 00:00:00 2001 From: msk- Date: Thu, 21 Jan 2021 10:03:18 +0000 Subject: [PATCH 4/6] Added line break to all WriteK, WriteF calls per PR feedback --- api/krusty/diamondpatchref_test.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go index 76747ce76..ffc3537f8 100644 --- a/api/krusty/diamondpatchref_test.go +++ b/api/krusty/diamondpatchref_test.go @@ -12,17 +12,20 @@ import ( func TestNamePrefixSuffixPatch(t *testing.T) { th := kusttest_test.MakeHarness(t) - th.WriteK("bottom", `configMapGenerator: + th.WriteK("bottom", ` +configMapGenerator: - name: bottom literals: - KEY=value `) - th.WriteK("left-service", `resources: + th.WriteK("left-service", ` +resources: - deployment.yaml `) - th.WriteF("left-service/deployment.yaml", `apiVersion: apps/v1 + th.WriteF("left-service/deployment.yaml", ` +apiVersion: apps/v1 kind: Deployment metadata: name: left-deploy @@ -42,11 +45,13 @@ spec: name: service `) - th.WriteK("right-service", `resources: + th.WriteK("right-service", ` +resources: - deployment.yaml `) - th.WriteF("right-service/deployment.yaml", `apiVersion: apps/v1 + th.WriteF("right-service/deployment.yaml", ` +apiVersion: apps/v1 kind: Deployment metadata: name: right-deploy @@ -66,12 +71,14 @@ spec: name: service `) - th.WriteK("top", `resources: + th.WriteK("top", ` +resources: - ./left - ./right `) - th.WriteK("top/left", `resources: + th.WriteK("top/left", ` +resources: - ../../left-service - ./bottom @@ -97,13 +104,15 @@ patches: key: KEY `) - th.WriteK("top/left/bottom", `namePrefix: left- + th.WriteK("top/left/bottom", ` +namePrefix: left- resources: - ../../../bottom `) - th.WriteK("top/right", `resources: + th.WriteK("top/right", ` +resources: - ../../right-service - ./bottom @@ -129,7 +138,8 @@ patches: key: KEY `) - th.WriteK("top/right/bottom", `namePrefix: right- + th.WriteK("top/right/bottom", ` +namePrefix: right- resources: - ../../../bottom From 9b87f78511d53614085d2ce5677377bcd9adbfe9 Mon Sep 17 00:00:00 2001 From: msk- Date: Thu, 21 Jan 2021 10:04:19 +0000 Subject: [PATCH 5/6] Modified test name per PR feedback --- api/krusty/diamondpatchref_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go index ffc3537f8..4dc9a00b8 100644 --- a/api/krusty/diamondpatchref_test.go +++ b/api/krusty/diamondpatchref_test.go @@ -9,7 +9,7 @@ import ( kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -func TestNamePrefixSuffixPatch(t *testing.T) { +func TestConfMapNameResolutionInDiamondWithPatches(t *testing.T) { th := kusttest_test.MakeHarness(t) th.WriteK("bottom", ` From 1bef8c4cdd06746ee75348e934cf0dd518ae846a Mon Sep 17 00:00:00 2001 From: msk- Date: Thu, 21 Jan 2021 10:13:42 +0000 Subject: [PATCH 6/6] Added ASCII art per PR feedback --- api/krusty/diamondpatchref_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api/krusty/diamondpatchref_test.go b/api/krusty/diamondpatchref_test.go index 4dc9a00b8..3d20a4b85 100644 --- a/api/krusty/diamondpatchref_test.go +++ b/api/krusty/diamondpatchref_test.go @@ -3,6 +3,20 @@ package krusty_test +// In the following structure `top` patches `left-deploy` and `right-deploy` to reference the +// configMap in `bottom`. This test verifies `configMapRef.name` in `left-deploy` and +// `right-deploy` is modified correctly. +// +// top +// / \ +// left right +// / \ / \ +// left-service bottom bottom right-service +// | \ / | +// left-deploy bottom right-deploy +// | +// configMap + import ( "testing"