Skip to content

Commit e95e854

Browse files
committed
fix: prune old duplicate keys from configmap/secret when overriding
1 parent 67fc288 commit e95e854

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

api/krusty/configmaps_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ metadata:
118118
`)
119119
}
120120

121-
func TestGeneratorOverrideDataWithBinaryDataInvalidAtKubeAPI(t *testing.T) {
122-
// the resulting ConfigMap will fail Kubernetes API validation:
123-
// The ConfigMap "test-configmap-b6h9d5bfmt" is invalid: data[CHANGING]: Invalid value: "CHANGING": duplicate of key present in binaryData
121+
func TestGeneratorOverrideDataWithBinaryData(t *testing.T) {
124122
th := kusttest_test.MakeHarness(t)
125123
th.WriteK("base", `
126124
configMapGenerator:
@@ -159,17 +157,14 @@ binaryData:
159157
hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
160158
data:
161159
BASE: red
162-
CHANGING: data-before
163160
OVERLAY: blue
164161
kind: ConfigMap
165162
metadata:
166-
name: test-configmap-b6h9d5bfmt
163+
name: test-configmap-7d7tc96hhk
167164
`)
168165
}
169166

170-
func TestGeneratorOverrideBinaryDataWithDataInvalidAtKubeAPI(t *testing.T) {
171-
// the resulting ConfigMap will fail Kubernetes API validation:
172-
// The ConfigMap "test-configmap-kt6d6mk694" is invalid: data[CHANGING]: Invalid value: "CHANGING": duplicate of key present in binaryData
167+
func TestGeneratorOverrideBinaryDataWithData(t *testing.T) {
173168
th := kusttest_test.MakeHarness(t)
174169
th.WriteK("base", `
175170
configMapGenerator:
@@ -200,19 +195,13 @@ OVERLAY=blue
200195
m := th.Run("overlay", th.MakeDefaultOptions())
201196
th.AssertActualEqualsExpected(m, `
202197
apiVersion: v1
203-
binaryData:
204-
CHANGING: |
205-
/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
206-
xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
207-
bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
208-
hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
209198
data:
210199
BASE: red
211200
CHANGING: data-after
212201
OVERLAY: blue
213202
kind: ConfigMap
214203
metadata:
215-
name: test-configmap-kt6d6mk694
204+
name: test-configmap-6kt4k5b8cf
216205
`)
217206
}
218207

api/krusty/secrets_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ type: Opaque
201201
`)
202202
}
203203

204-
func TestSecretGeneratorOverrideDataWithStringDataWorksAtKubeAPI(t *testing.T) {
205-
// The resulting Secret will have a duplicate key in both data and stringData
206-
// The stringData override will work, because the kube API considers stringData authoritative and write-only
204+
func TestSecretGeneratorOverrideDataWithStringData(t *testing.T) {
207205
th := kusttest_test.MakeHarness(t)
208206
th.WriteK("base", `
209207
secretGenerator:
@@ -235,20 +233,17 @@ OVERLAY=blue
235233
apiVersion: v1
236234
data:
237235
BASE: cmVk
238-
CHANGING: ZGF0YS1iZWZvcmU=
239236
kind: Secret
240237
metadata:
241-
name: test-secret-c4g4kdc558
238+
name: test-secret-27cgc8d5f9
242239
stringData:
243240
CHANGING: stringData-after
244241
OVERLAY: blue
245242
type: Opaque
246243
`)
247244
}
248245

249-
func TestSecretGeneratorOverrideStringDataWithDataSilentlyFailsAtKubeAPI(t *testing.T) {
250-
// The resulting Secret will have a duplicate key in both data and stringData
251-
// The data override will fail, because the kube API considers the older value in stringData authoritative and write-only
246+
func TestSecretGeneratorOverrideStringDataWithData(t *testing.T) {
252247
th := kusttest_test.MakeHarness(t)
253248
th.WriteK("base", `
254249
secretGenerator:
@@ -284,10 +279,9 @@ data:
284279
OVERLAY: Ymx1ZQ==
285280
kind: Secret
286281
metadata:
287-
name: test-secret-b65ktgckfh
282+
name: test-secret-dm4hm4t2hb
288283
stringData:
289284
BASE: red
290-
CHANGING: stringData-before
291285
type: Opaque
292286
`)
293287
}

api/resource/resource.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,24 @@ func (r *Resource) copyKustomizeSpecificFields(other *Resource) {
190190
}
191191

192192
func (r *Resource) MergeDataMapFrom(o *Resource) {
193-
r.SetDataMap(mergeStringMaps(o.GetDataMap(), r.GetDataMap()))
193+
r.SetDataMap(
194+
mergeStringMaps(
195+
pruneStringMap(o.GetDataMap(), r.GetBinaryDataMap(), r.GetStringDataMap()),
196+
r.GetDataMap()))
194197
}
195198

196199
func (r *Resource) MergeBinaryDataMapFrom(o *Resource) {
197-
r.SetBinaryDataMap(mergeStringMaps(o.GetBinaryDataMap(), r.GetBinaryDataMap()))
200+
r.SetBinaryDataMap(
201+
mergeStringMaps(
202+
pruneStringMap(o.GetBinaryDataMap(), r.GetDataMap()),
203+
r.GetBinaryDataMap()))
198204
}
199205

200206
func (r *Resource) MergeStringDataMapFrom(o *Resource) {
201-
r.SetStringDataMap(mergeStringMaps(o.GetStringDataMap(), r.GetStringDataMap()))
207+
r.SetStringDataMap(
208+
mergeStringMaps(
209+
pruneStringMap(o.GetStringDataMap(), r.GetDataMap()),
210+
r.GetStringDataMap()))
202211
}
203212

204213
func (r *Resource) ErrIfNotEquals(o *Resource) error {
@@ -549,3 +558,20 @@ func mergeStringMapsWithBuildAnnotations(maps ...map[string]string) map[string]s
549558
}
550559
return result
551560
}
561+
562+
// pruneStringMap returns a copy of orig with any shared keys from maps[] removed
563+
func pruneStringMap(orig map[string]string, maps ...map[string]string) map[string]string {
564+
if orig == nil {
565+
return nil
566+
}
567+
result := map[string]string{}
568+
for key, value := range orig {
569+
result[key] = value
570+
}
571+
for _, m := range maps {
572+
for key := range m {
573+
delete(result, key)
574+
}
575+
}
576+
return result
577+
}

0 commit comments

Comments
 (0)