From 487703ee8f46ef95f1071c1dc36f7adbdf8bbb53 Mon Sep 17 00:00:00 2001 From: Eli Shafer Date: Sun, 16 Oct 2022 16:35:00 -0700 Subject: [PATCH 1/3] api/resource: retain disableNameSuffixHash on merge. --- api/krusty/generatormergeandreplace_test.go | 6 ++- api/krusty/generatoroptions_test.go | 44 +++++++++++++++++++++ api/resource/resource.go | 11 +++++- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/api/krusty/generatormergeandreplace_test.go b/api/krusty/generatormergeandreplace_test.go index fae8e745f7..953bb87131 100644 --- a/api/krusty/generatormergeandreplace_test.go +++ b/api/krusty/generatormergeandreplace_test.go @@ -339,6 +339,8 @@ configMapGenerator: behavior: replace literals: - foo=override-bar + options: + disableNameSuffixHash: true secretGenerator: - name: secret-in-base behavior: merge @@ -389,7 +391,7 @@ spec: name: staging-configmap-in-overlay-dc6fm46dhm name: configmap-in-overlay - configMap: - name: staging-team-foo-configmap-in-base-hc6g9dk6g9 + name: staging-team-foo-configmap-in-base name: configmap-in-base --- apiVersion: v1 @@ -424,7 +426,7 @@ metadata: env: staging org: example.com team: override-foo - name: staging-team-foo-configmap-in-base-hc6g9dk6g9 + name: staging-team-foo-configmap-in-base --- apiVersion: v1 data: diff --git a/api/krusty/generatoroptions_test.go b/api/krusty/generatoroptions_test.go index 8297f55603..3eee4f19a5 100644 --- a/api/krusty/generatoroptions_test.go +++ b/api/krusty/generatoroptions_test.go @@ -94,3 +94,47 @@ metadata: name: shouldHaveHash-c9867f8446 `) } + +func TestGeneratorOptionsOverlayDisableNameSuffixHash(t *testing.T) { + th := kusttest_test.MakeHarness(t) + th.WriteK("base", ` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +generatorOptions: + disableNameSuffixHash: false + labels: + foo: bar +configMapGenerator: +- name: baseShouldHaveHashButOverlayShouldNot + literals: + - foo=bar +`) + th.WriteK("overlay", ` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- ../base +generatorOptions: + disableNameSuffixHash: true + labels: + fruit: apple +configMapGenerator: +- name: baseShouldHaveHashButOverlayShouldNot + behavior: merge + literals: + - fruit=apple +`) + m := th.Run("overlay", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +data: + foo: bar + fruit: apple +kind: ConfigMap +metadata: + labels: + foo: bar + fruit: apple + name: baseShouldHaveHashButOverlayShouldNot +`) +} diff --git a/api/resource/resource.go b/api/resource/resource.go index a88ce31d22..ae1a98be0e 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -164,10 +164,17 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error { mergeStringMaps(other.GetLabels(), r.GetLabels())); err != nil { return fmt.Errorf("copyMerge cannot set labels - %w", err) } - if err := r.SetAnnotations( - mergeStringMapsWithBuildAnnotations(other.GetAnnotations(), r.GetAnnotations())); err != nil { + + ra := r.GetAnnotations() + _, enableNameSuffixHash := ra[utils.BuildAnnotationsGenAddHashSuffix] + merged := mergeStringMapsWithBuildAnnotations(other.GetAnnotations(), ra) + if !enableNameSuffixHash { + delete(merged, utils.BuildAnnotationsGenAddHashSuffix) + } + if err := r.SetAnnotations(merged); err != nil { return fmt.Errorf("copyMerge cannot set annotations - %w", err) } + if err := r.SetName(other.GetName()); err != nil { return fmt.Errorf("copyMerge cannot set name - %w", err) } From 24bf1d07e7b3bf4b12191641ca062a5c856b5a13 Mon Sep 17 00:00:00 2001 From: Eli Shafer Date: Sat, 5 Nov 2022 13:23:01 -0700 Subject: [PATCH 2/3] api/resource: add new test for inline options. --- api/krusty/generatormergeandreplace_test.go | 163 +++++++++++++++++++- 1 file changed, 160 insertions(+), 3 deletions(-) diff --git a/api/krusty/generatormergeandreplace_test.go b/api/krusty/generatormergeandreplace_test.go index 953bb87131..4c23eea63f 100644 --- a/api/krusty/generatormergeandreplace_test.go +++ b/api/krusty/generatormergeandreplace_test.go @@ -335,6 +335,161 @@ configMapGenerator: - name: configmap-in-overlay literals: - hello=world +- name: configmap-in-base + behavior: replace + literals: + - foo=override-bar +secretGenerator: +- name: secret-in-base + behavior: merge + literals: + - proxy=haproxy +`) + m := th.Run("overlay", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + note: This is a test annotation + labels: + app: mynginx + env: staging + org: example.com + team: override-foo + name: staging-team-foo-nginx +spec: + selector: + matchLabels: + app: mynginx + env: staging + org: example.com + team: override-foo + template: + metadata: + annotations: + note: This is a test annotation + labels: + app: mynginx + env: staging + org: example.com + team: override-foo + spec: + containers: + - image: nginx + name: nginx + volumeMounts: + - mountPath: /tmp/ps + name: nginx-persistent-storage + volumes: + - gcePersistentDisk: + pdName: nginx-persistent-storage + name: nginx-persistent-storage + - configMap: + name: staging-configmap-in-overlay-dc6fm46dhm + name: configmap-in-overlay + - configMap: + name: staging-team-foo-configmap-in-base-hc6g9dk6g9 + name: configmap-in-base +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + note: This is a test annotation + labels: + app: mynginx + env: staging + org: example.com + team: override-foo + name: staging-team-foo-nginx +spec: + ports: + - port: 80 + selector: + app: mynginx + env: staging + org: example.com + team: override-foo +--- +apiVersion: v1 +data: + foo: override-bar +kind: ConfigMap +metadata: + annotations: + note: This is a test annotation + labels: + app: mynginx + env: staging + org: example.com + team: override-foo + name: staging-team-foo-configmap-in-base-hc6g9dk6g9 +--- +apiVersion: v1 +data: + password: c29tZXB3 + proxy: aGFwcm94eQ== + username: YWRtaW4= +kind: Secret +metadata: + annotations: + note: This is a test annotation + labels: + app: mynginx + env: staging + org: example.com + team: override-foo + name: staging-team-foo-secret-in-base-k2k4692t9g +type: Opaque +--- +apiVersion: v1 +data: + hello: world +kind: ConfigMap +metadata: + labels: + env: staging + team: override-foo + name: staging-configmap-in-overlay-dc6fm46dhm +`) +} + +func TestMergeAndReplaceDisableNameSuffixHashGenerators(t *testing.T) { + th := kusttest_test.MakeHarness(t) + makeBaseWithGenerators(th) + th.WriteF("overlay/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx +spec: + template: + spec: + volumes: + - name: nginx-persistent-storage + emptyDir: null + gcePersistentDisk: + pdName: nginx-persistent-storage + - configMap: + name: configmap-in-overlay + name: configmap-in-overlay +`) + th.WriteK("overlay", ` +namePrefix: staging- +commonLabels: + env: staging + team: override-foo +patchesStrategicMerge: +- deployment.yaml +resources: +- ../app +configMapGenerator: +- name: configmap-in-overlay + literals: + - hello=world + options: + disableNameSuffixHash: true - name: configmap-in-base behavior: replace literals: @@ -346,6 +501,8 @@ secretGenerator: behavior: merge literals: - proxy=haproxy + options: + disableNameSuffixHash: true `) m := th.Run("overlay", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` @@ -388,7 +545,7 @@ spec: pdName: nginx-persistent-storage name: nginx-persistent-storage - configMap: - name: staging-configmap-in-overlay-dc6fm46dhm + name: staging-configmap-in-overlay name: configmap-in-overlay - configMap: name: staging-team-foo-configmap-in-base @@ -442,7 +599,7 @@ metadata: env: staging org: example.com team: override-foo - name: staging-team-foo-secret-in-base-k2k4692t9g + name: staging-team-foo-secret-in-base type: Opaque --- apiVersion: v1 @@ -453,7 +610,7 @@ metadata: labels: env: staging team: override-foo - name: staging-configmap-in-overlay-dc6fm46dhm + name: staging-configmap-in-overlay `) } From 2d854c46dd31c01d7dc57d2d162aa2323ea7dd69 Mon Sep 17 00:00:00 2001 From: Eli Shafer Date: Sun, 11 Dec 2022 22:58:41 -0800 Subject: [PATCH 3/3] resource/api: update test to use a more specific kustomization --- api/krusty/generatormergeandreplace_test.go | 67 ++++++++++++++------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/api/krusty/generatormergeandreplace_test.go b/api/krusty/generatormergeandreplace_test.go index 4c23eea63f..80b8846884 100644 --- a/api/krusty/generatormergeandreplace_test.go +++ b/api/krusty/generatormergeandreplace_test.go @@ -457,7 +457,52 @@ metadata: func TestMergeAndReplaceDisableNameSuffixHashGenerators(t *testing.T) { th := kusttest_test.MakeHarness(t) - makeBaseWithGenerators(th) + th.WriteK("app", ` +namePrefix: team-foo- +commonLabels: + app: mynginx + org: example.com + team: foo +commonAnnotations: + note: This is a test annotation +resources: + - deployment.yaml +configMapGenerator: +- name: configmap-in-base + literals: + - foo=bar +secretGenerator: +- name: secret-in-base + literals: + - username=admin + - password=somepw +`) + th.WriteF("app/deployment.yaml", ` +apiVersion: apps/v1 +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 +`) th.WriteF("overlay/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment @@ -552,26 +597,6 @@ spec: name: configmap-in-base --- apiVersion: v1 -kind: Service -metadata: - annotations: - note: This is a test annotation - labels: - app: mynginx - env: staging - org: example.com - team: override-foo - name: staging-team-foo-nginx -spec: - ports: - - port: 80 - selector: - app: mynginx - env: staging - org: example.com - team: override-foo ---- -apiVersion: v1 data: foo: override-bar kind: ConfigMap