api/resource: retain disableNameSuffixHash on merge.

This commit is contained in:
Eli Shafer
2022-10-16 16:35:00 -07:00
parent 2db573b6a0
commit 487703ee8f
3 changed files with 57 additions and 4 deletions

View File

@@ -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:

View File

@@ -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
`)
}

View File

@@ -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)
}