diff --git a/api/krusty/generatormergeandreplace_test.go b/api/krusty/generatormergeandreplace_test.go index 821dcc918..0dff5ae9b 100644 --- a/api/krusty/generatormergeandreplace_test.go +++ b/api/krusty/generatormergeandreplace_test.go @@ -453,6 +453,190 @@ metadata: `) } +func TestMergeAndReplaceDisableNameSuffixHashGenerators(t *testing.T) { + th := kusttest_test.MakeHarness(t) + 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 +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: + - foo=override-bar + options: + disableNameSuffixHash: true +secretGenerator: +- name: secret-in-base + behavior: merge + literals: + - proxy=haproxy + options: + disableNameSuffixHash: true +`) + 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 + name: configmap-in-overlay + - configMap: + name: staging-team-foo-configmap-in-base + name: configmap-in-base +--- +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 +--- +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 +type: Opaque +--- +apiVersion: v1 +data: + hello: world +kind: ConfigMap +metadata: + labels: + env: staging + team: override-foo + name: staging-configmap-in-overlay +`) +} + func TestGeneratingIntoNamespaces(t *testing.T) { th := kusttest_test.MakeHarness(t) th.WriteK("app", ` diff --git a/api/krusty/generatoroptions_test.go b/api/krusty/generatoroptions_test.go index 3a940b20e..2bb98ae7d 100644 --- a/api/krusty/generatoroptions_test.go +++ b/api/krusty/generatoroptions_test.go @@ -90,3 +90,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 a88ce31d2..ae1a98be0 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) }