mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
* 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
This commit is contained in:
@@ -284,9 +284,9 @@ func (f Filter) roleRefFilter() sieveFunc {
|
|||||||
return previousIdSelectedByGvk(roleRefGvk)
|
return previousIdSelectedByGvk(roleRefGvk)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prefixSuffixEquals(other resource.ResCtx) sieveFunc {
|
func prefixSuffixEquals(other resource.ResCtx, allowEmpty bool) sieveFunc {
|
||||||
return func(r *resource.Resource) bool {
|
return func(r *resource.Resource) bool {
|
||||||
return r.PrefixesSuffixesEquals(other)
|
return r.PrefixesSuffixesEquals(other, allowEmpty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,7 +325,10 @@ func (f Filter) selectReferral(
|
|||||||
if len(candidates) == 1 {
|
if len(candidates) == 1 {
|
||||||
return candidates[0], nil
|
return candidates[0], nil
|
||||||
}
|
}
|
||||||
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer))
|
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, true))
|
||||||
|
if len(candidates) > 1 {
|
||||||
|
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, false))
|
||||||
|
}
|
||||||
if len(candidates) == 1 {
|
if len(candidates) == 1 {
|
||||||
return candidates[0], nil
|
return candidates[0], nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -591,3 +591,184 @@ metadata:
|
|||||||
name: test-m8t7bmb6g2
|
name: test-m8t7bmb6g2
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
|
||||||
|
func TestPrefixSuffix(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteF("kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("a/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- ../common
|
||||||
|
|
||||||
|
namePrefix: a
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("b/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- ../common
|
||||||
|
|
||||||
|
namePrefix: b
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("common/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- service
|
||||||
|
|
||||||
|
configMapGenerator:
|
||||||
|
- name: "-example-configmap"
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("common/service/deployment.yaml", `
|
||||||
|
kind: Deployment
|
||||||
|
apiVersion: apps/v1
|
||||||
|
|
||||||
|
metadata:
|
||||||
|
name: "-"
|
||||||
|
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: app
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: "-example-configmap"
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("common/service/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
|
||||||
|
nameSuffix: api
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: a-api
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: a-example-configmap-6ct58987ht
|
||||||
|
name: app
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: a-example-configmap-6ct58987ht
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: b-api
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: b-example-configmap-6ct58987ht
|
||||||
|
name: app
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: b-example-configmap-6ct58987ht
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
|
||||||
|
func TestPrefixSuffix2(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteF("kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("a/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- ../common
|
||||||
|
|
||||||
|
namePrefix: a
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("b/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- ../common
|
||||||
|
|
||||||
|
namePrefix: b
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("common/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: "-example"
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: app
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: "-example-configmap"
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("common/kustomization.yaml", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
|
||||||
|
configMapGenerator:
|
||||||
|
- name: "-example-configmap"
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: a-example
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: a-example-configmap-6ct58987ht
|
||||||
|
name: app
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: a-example-configmap-6ct58987ht
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: b-example
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: b-example-configmap-6ct58987ht
|
||||||
|
name: app
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: b-example-configmap-6ct58987ht
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|||||||
@@ -287,13 +287,26 @@ func (r *Resource) getCsvAnnotation(name string) []string {
|
|||||||
return strings.Split(annotations[name], ",")
|
return strings.Split(annotations[name], ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrefixesSuffixesEquals is conceptually doing the same task
|
// PrefixesSuffixesEquals is conceptually doing the same task as
|
||||||
// as OutermostPrefixSuffix but performs a deeper comparison
|
// OutermostPrefixSuffix but performs a deeper comparison of the suffix and
|
||||||
// of the suffix and prefix slices.
|
// prefix slices.
|
||||||
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
|
// The allowEmpty flag determines whether an empty prefix/suffix
|
||||||
|
// should be considered a match on anything.
|
||||||
|
// This is used when filtering, starting with a coarser pass allowing empty
|
||||||
|
// matches, before requiring exact matches if there are multiple
|
||||||
|
// remaining candidates.
|
||||||
|
func (r *Resource) PrefixesSuffixesEquals(o ResCtx, allowEmpty bool) bool {
|
||||||
|
if allowEmpty {
|
||||||
|
eitherPrefixEmpty := len(r.GetNamePrefixes()) == 0 || len(o.GetNamePrefixes()) == 0
|
||||||
|
eitherSuffixEmpty := len(r.GetNameSuffixes()) == 0 || len(o.GetNameSuffixes()) == 0
|
||||||
|
|
||||||
|
return (eitherPrefixEmpty || utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes())) &&
|
||||||
|
(eitherSuffixEmpty || utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes()))
|
||||||
|
} else {
|
||||||
return utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes()) &&
|
return utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes()) &&
|
||||||
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
|
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveBuildAnnotations removes annotations created by the build process.
|
// RemoveBuildAnnotations removes annotations created by the build process.
|
||||||
// These are internal-only to kustomize, added to the data pipeline to
|
// These are internal-only to kustomize, added to the data pipeline to
|
||||||
|
|||||||
Reference in New Issue
Block a user