Skip to content

Commit

Permalink
Merge pull request #4834 from elisshafer/generators/retain_disable_su…
Browse files Browse the repository at this point in the history
…ffix_hash

api/resource: retain disableNameSuffixHash on merge and replace
  • Loading branch information
k8s-ci-robot authored Jan 24, 2023
2 parents 659c0ee + 2d854c4 commit 8f75682
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 2 deletions.
184 changes: 184 additions & 0 deletions api/krusty/generatormergeandreplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", `
Expand Down
44 changes: 44 additions & 0 deletions api/krusty/generatoroptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`)
}
11 changes: 9 additions & 2 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8f75682

Please sign in to comment.