Skip to content

Commit b3d1df2

Browse files
Fix name in a configMapRef missing hash #5047 (#5236)
* Add regression tests * Update PrefixesSuffixesEquals function * Try empty prefix/suffix but fall back on duplicates * Run gofmt * Remove newline * Revert unnecessary gofmt change * Add comment
1 parent ed2ca23 commit b3d1df2

File tree

3 files changed

+206
-9
lines changed

3 files changed

+206
-9
lines changed

api/filters/nameref/nameref.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ func (f Filter) roleRefFilter() sieveFunc {
284284
return previousIdSelectedByGvk(roleRefGvk)
285285
}
286286

287-
func prefixSuffixEquals(other resource.ResCtx) sieveFunc {
287+
func prefixSuffixEquals(other resource.ResCtx, allowEmpty bool) sieveFunc {
288288
return func(r *resource.Resource) bool {
289-
return r.PrefixesSuffixesEquals(other)
289+
return r.PrefixesSuffixesEquals(other, allowEmpty)
290290
}
291291
}
292292

@@ -325,7 +325,10 @@ func (f Filter) selectReferral(
325325
if len(candidates) == 1 {
326326
return candidates[0], nil
327327
}
328-
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer))
328+
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, true))
329+
if len(candidates) > 1 {
330+
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, false))
331+
}
329332
if len(candidates) == 1 {
330333
return candidates[0], nil
331334
}

api/krusty/configmaps_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,184 @@ metadata:
591591
name: test-m8t7bmb6g2
592592
`)
593593
}
594+
595+
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
596+
func TestPrefixSuffix(t *testing.T) {
597+
th := kusttest_test.MakeHarness(t)
598+
th.WriteF("kustomization.yaml", `
599+
resources:
600+
- a
601+
- b
602+
`)
603+
604+
th.WriteF("a/kustomization.yaml", `
605+
resources:
606+
- ../common
607+
608+
namePrefix: a
609+
`)
610+
611+
th.WriteF("b/kustomization.yaml", `
612+
resources:
613+
- ../common
614+
615+
namePrefix: b
616+
`)
617+
618+
th.WriteF("common/kustomization.yaml", `
619+
resources:
620+
- service
621+
622+
configMapGenerator:
623+
- name: "-example-configmap"
624+
`)
625+
626+
th.WriteF("common/service/deployment.yaml", `
627+
kind: Deployment
628+
apiVersion: apps/v1
629+
630+
metadata:
631+
name: "-"
632+
633+
spec:
634+
template:
635+
spec:
636+
containers:
637+
- name: app
638+
envFrom:
639+
- configMapRef:
640+
name: "-example-configmap"
641+
`)
642+
643+
th.WriteF("common/service/kustomization.yaml", `
644+
resources:
645+
- deployment.yaml
646+
647+
nameSuffix: api
648+
`)
649+
650+
m := th.Run(".", th.MakeDefaultOptions())
651+
th.AssertActualEqualsExpected(m, `
652+
apiVersion: apps/v1
653+
kind: Deployment
654+
metadata:
655+
name: a-api
656+
spec:
657+
template:
658+
spec:
659+
containers:
660+
- envFrom:
661+
- configMapRef:
662+
name: a-example-configmap-6ct58987ht
663+
name: app
664+
---
665+
apiVersion: v1
666+
kind: ConfigMap
667+
metadata:
668+
name: a-example-configmap-6ct58987ht
669+
---
670+
apiVersion: apps/v1
671+
kind: Deployment
672+
metadata:
673+
name: b-api
674+
spec:
675+
template:
676+
spec:
677+
containers:
678+
- envFrom:
679+
- configMapRef:
680+
name: b-example-configmap-6ct58987ht
681+
name: app
682+
---
683+
apiVersion: v1
684+
kind: ConfigMap
685+
metadata:
686+
name: b-example-configmap-6ct58987ht
687+
`)
688+
}
689+
690+
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
691+
func TestPrefixSuffix2(t *testing.T) {
692+
th := kusttest_test.MakeHarness(t)
693+
th.WriteF("kustomization.yaml", `
694+
resources:
695+
- a
696+
- b
697+
`)
698+
699+
th.WriteF("a/kustomization.yaml", `
700+
resources:
701+
- ../common
702+
703+
namePrefix: a
704+
`)
705+
706+
th.WriteF("b/kustomization.yaml", `
707+
resources:
708+
- ../common
709+
710+
namePrefix: b
711+
`)
712+
713+
th.WriteF("common/deployment.yaml", `
714+
apiVersion: apps/v1
715+
kind: Deployment
716+
metadata:
717+
name: "-example"
718+
spec:
719+
template:
720+
spec:
721+
containers:
722+
- name: app
723+
envFrom:
724+
- configMapRef:
725+
name: "-example-configmap"
726+
`)
727+
728+
th.WriteF("common/kustomization.yaml", `
729+
resources:
730+
- deployment.yaml
731+
732+
configMapGenerator:
733+
- name: "-example-configmap"
734+
`)
735+
736+
m := th.Run(".", th.MakeDefaultOptions())
737+
th.AssertActualEqualsExpected(m, `
738+
apiVersion: apps/v1
739+
kind: Deployment
740+
metadata:
741+
name: a-example
742+
spec:
743+
template:
744+
spec:
745+
containers:
746+
- envFrom:
747+
- configMapRef:
748+
name: a-example-configmap-6ct58987ht
749+
name: app
750+
---
751+
apiVersion: v1
752+
kind: ConfigMap
753+
metadata:
754+
name: a-example-configmap-6ct58987ht
755+
---
756+
apiVersion: apps/v1
757+
kind: Deployment
758+
metadata:
759+
name: b-example
760+
spec:
761+
template:
762+
spec:
763+
containers:
764+
- envFrom:
765+
- configMapRef:
766+
name: b-example-configmap-6ct58987ht
767+
name: app
768+
---
769+
apiVersion: v1
770+
kind: ConfigMap
771+
metadata:
772+
name: b-example-configmap-6ct58987ht
773+
`)
774+
}

api/resource/resource.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,25 @@ func (r *Resource) getCsvAnnotation(name string) []string {
287287
return strings.Split(annotations[name], ",")
288288
}
289289

290-
// PrefixesSuffixesEquals is conceptually doing the same task
291-
// as OutermostPrefixSuffix but performs a deeper comparison
292-
// of the suffix and prefix slices.
293-
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
294-
return utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes()) &&
295-
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
290+
// PrefixesSuffixesEquals is conceptually doing the same task as
291+
// OutermostPrefixSuffix but performs a deeper comparison of the suffix and
292+
// prefix slices.
293+
// The allowEmpty flag determines whether an empty prefix/suffix
294+
// should be considered a match on anything.
295+
// This is used when filtering, starting with a coarser pass allowing empty
296+
// matches, before requiring exact matches if there are multiple
297+
// remaining candidates.
298+
func (r *Resource) PrefixesSuffixesEquals(o ResCtx, allowEmpty bool) bool {
299+
if allowEmpty {
300+
eitherPrefixEmpty := len(r.GetNamePrefixes()) == 0 || len(o.GetNamePrefixes()) == 0
301+
eitherSuffixEmpty := len(r.GetNameSuffixes()) == 0 || len(o.GetNameSuffixes()) == 0
302+
303+
return (eitherPrefixEmpty || utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes())) &&
304+
(eitherSuffixEmpty || utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes()))
305+
} else {
306+
return utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes()) &&
307+
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
308+
}
296309
}
297310

298311
// RemoveBuildAnnotations removes annotations created by the build process.

0 commit comments

Comments
 (0)