diff --git a/api/krusty/intermediateresourceid_test.go b/api/krusty/intermediateresourceid_test.go index 6e9ac1ed8..781f56a5b 100644 --- a/api/krusty/intermediateresourceid_test.go +++ b/api/krusty/intermediateresourceid_test.go @@ -3,6 +3,7 @@ package krusty_test import ( "testing" + "github.com/stretchr/testify/assert" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) @@ -67,6 +68,147 @@ spec: `) } +// Tests that if resources in different layers (containing name +// transformations) have the same name, there is no conflict +func TestIntermediateNameSameNameDifferentLayer(t *testing.T) { + th := kusttest_test.MakeHarness(t) + th.WriteK("/app/gcp", ` +namePrefix: gcp- +resources: +- ../emea +patchesStrategicMerge: +- depPatch.yaml +`) + th.WriteF("/app/gcp/depPatch.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: prod-foo +spec: + replicas: 999 +`) + th.WriteK("/app/emea", ` +namePrefix: emea- +resources: +- ../prod +- deployment.yaml +`) + th.WriteF("/app/emea/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - image: whatever +`) + th.WriteK("/app/prod", ` +namePrefix: prod- +resources: +- ../base +`) + th.WriteK("/app/base", ` +resources: +- deployment.yaml +`) + th.WriteF("/app/base/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - image: whatever +`) + m := th.Run("/app/gcp", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: gcp-emea-prod-foo +spec: + replicas: 999 + template: + spec: + containers: + - image: whatever +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: gcp-emea-foo +spec: + template: + spec: + containers: + - image: whatever +`) +} + +// Same as above test but tries to refer to the name foo +// instead of prod-foo +func TestIntermediateNameAmbiguous(t *testing.T) { + th := kusttest_test.MakeHarness(t) + th.WriteK("/app/gcp", ` +namePrefix: gcp- +resources: +- ../emea +patchesStrategicMerge: +- depPatch.yaml +`) + th.WriteF("/app/gcp/depPatch.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + replicas: 999 +`) + th.WriteK("/app/emea", ` +namePrefix: emea- +resources: +- ../prod +- deployment.yaml +`) + th.WriteF("/app/emea/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - image: whatever +`) + th.WriteK("/app/prod", ` +namePrefix: prod- +resources: +- ../base +`) + th.WriteK("/app/base", ` +resources: +- deployment.yaml +`) + th.WriteF("/app/base/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - image: whatever +`) + err := th.RunWithErr("/app/gcp", th.MakeDefaultOptions()) + assert.Error(t, err) +} + // Test for issue #3228 // References to resources by their intermediate names after multiple // name transformations should be supported diff --git a/api/resmap/reswrangler.go b/api/resmap/reswrangler.go index efd37fc99..dceba29ad 100644 --- a/api/resmap/reswrangler.go +++ b/api/resmap/reswrangler.go @@ -216,7 +216,7 @@ func demandOneMatch( return r[0], nil } if len(r) > 1 { - return nil, fmt.Errorf("multiple matches for %sId %s", s, id) + return nil, fmt.Errorf("multiple matches for %s %s", s, id) } return nil, fmt.Errorf("no matches for %sId %s", s, id) }