...
1
2
3
4 package krusty_test
5
6 import (
7 "strings"
8 "testing"
9
10 "sigs.k8s.io/kustomize/api/resmap"
11 "sigs.k8s.io/kustomize/api/resource"
12 kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
13 )
14
15 func findSecret(m resmap.ResMap, prefix string) *resource.Resource {
16 for _, r := range m.Resources() {
17 if r.OrgId().Kind == "Secret" {
18 if prefix == "" || strings.HasPrefix(r.GetName(), prefix) {
19 return r
20 }
21 }
22 }
23 return nil
24 }
25
26 func TestDisableNameSuffixHash(t *testing.T) {
27 th := kusttest_test.MakeHarness(t)
28 const kustomizationContent = `
29 namePrefix: foo-
30 nameSuffix: -bar
31 namespace: ns1
32 commonLabels:
33 app: nginx
34 commonAnnotations:
35 note: This is a test annotation
36 resources:
37 - deployment.yaml
38 - namespace.yaml
39 generatorOptions:
40 disableNameSuffixHash: false
41 configMapGenerator:
42 - name: literalConfigMap
43 literals:
44 - DB_USERNAME=admin
45 - DB_PASSWORD=somepw
46 secretGenerator:
47 - name: secret
48 literals:
49 - DB_USERNAME=admin
50 - DB_PASSWORD=somepw
51 type: Opaque
52 patchesJson6902:
53 - target:
54 group: apps
55 version: v1
56 kind: Deployment
57 name: dply1
58 path: jsonpatch.json
59 `
60
61 th.WriteK("/whatever", kustomizationContent)
62 th.WriteF("/whatever/deployment.yaml", `
63 apiVersion: apps/v1
64 metadata:
65 name: dply1
66 kind: Deployment
67 `)
68 th.WriteF("/whatever/namespace.yaml", `
69 apiVersion: v1
70 kind: Namespace
71 metadata:
72 name: ns1
73 `)
74 th.WriteF("/whatever/jsonpatch.json", `[
75 {"op": "add", "path": "/spec/replica", "value": "3"}
76 ]`)
77
78 m := th.Run("/whatever", th.MakeDefaultOptions())
79
80 secret := findSecret(m, "")
81 if secret == nil {
82 t.Errorf("Expected to find a Secret")
83 } else if secret.GetName() != "foo-secret-bar-82c2g5f8f6" {
84 t.Errorf("unexpected secret resource name: %s", secret.GetName())
85 }
86
87 th.WriteK("/whatever",
88 strings.ReplaceAll(kustomizationContent,
89 "disableNameSuffixHash: false",
90 "disableNameSuffixHash: true"))
91 m = th.Run("/whatever", th.MakeDefaultOptions())
92 secret = findSecret(m, "")
93 if secret == nil {
94 t.Errorf("Expected to find a Secret")
95 } else if secret.GetName() != "foo-secret-bar" {
96 t.Errorf("unexpected secret resource name: %s", secret.GetName())
97 }
98 }
99 func TestDisableNameSuffixHashPerObject(t *testing.T) {
100 th := kusttest_test.MakeHarness(t)
101 const kustomizationContent = `
102 generatorOptions:
103 disableNameSuffixHash: false
104 secretGenerator:
105 - name: nohash
106 options:
107 disableNameSuffixHash: true
108 literals:
109 - DB_USERNAME=admin
110 - DB_PASSWORD=somepw
111 type: Opaque
112 - name: yeshash
113 options:
114 disableNameSuffixHash: false
115 literals:
116 - DB_USERNAME=admin
117 - DB_PASSWORD=somepw
118 type: Opaque
119 `
120
121 th.WriteK("/whatever", kustomizationContent)
122
123 m := th.Run("/whatever", th.MakeDefaultOptions())
124
125 secret := findSecret(m, "nohash")
126 if secret == nil {
127 t.Errorf("Expected to find a Secret")
128 } else if secret.GetName() != "nohash" {
129 t.Errorf("unexpected secret resource name: %s", secret.GetName())
130 }
131
132 secret = findSecret(m, "yeshash")
133 if secret == nil {
134 t.Errorf("Expected to find a Secret")
135 } else if secret.GetName() != "yeshash-82c2g5f8f6" {
136 t.Errorf("unexpected secret resource name: %s", secret.GetName())
137 }
138 }
139
View as plain text