mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 09:02:53 +00:00
Merge pull request #1371 from keleustes/nsvar
Add namespace to variable definition
This commit is contained in:
@@ -64,20 +64,14 @@ func (ra *ResAccumulator) GetTransformerConfig() *config.TransformerConfig {
|
|||||||
|
|
||||||
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
||||||
for _, v := range incoming {
|
for _, v := range incoming {
|
||||||
// Warning: Do not change GvknEquals to Equals until the
|
targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace)
|
||||||
// namespace is part of the variable declaration.
|
idMatcher := targetId.GvknEquals
|
||||||
// The namespace will eventually be added to the variable
|
if targetId.Namespace != "" || !targetId.IsNamespaceableKind() {
|
||||||
// declaration to solve the following issue:
|
// Preserve backward compatibility. An empty namespace means
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/1298
|
// wildcard search on the namespace hence we still use GvknEquals
|
||||||
// If two resources of the same kind with the same name but
|
idMatcher = targetId.Equals
|
||||||
// in different namespaces, any variable aiming to put at one
|
}
|
||||||
// of those resources will fail since it is not possible to
|
matched := ra.resMap.GetMatchingResourcesByOriginalId(idMatcher)
|
||||||
// specify the namespace and use Id.Equals instead of Id.GvknEquals
|
|
||||||
// The change will also need to be backward compatible, i.e.
|
|
||||||
// no namespace specified means wildcard namespace not "default"
|
|
||||||
// namespace.
|
|
||||||
matched := ra.resMap.GetMatchingResourcesByOriginalId(
|
|
||||||
resid.NewResId(v.ObjRef.GVK(), v.ObjRef.Name).GvknEquals)
|
|
||||||
if len(matched) > 1 {
|
if len(matched) > 1 {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"found %d resId matches for var %s "+
|
"found %d resId matches for var %s "+
|
||||||
|
|||||||
@@ -104,8 +104,8 @@ rules:
|
|||||||
// https://github.com/kubernetes-sigs/kustomize/issues/1298
|
// https://github.com/kubernetes-sigs/kustomize/issues/1298
|
||||||
const namespaceNeedInVarMyApp string = `
|
const namespaceNeedInVarMyApp string = `
|
||||||
resources:
|
resources:
|
||||||
- elasticsearch-test-service.yaml
|
|
||||||
- elasticsearch-dev-service.yaml
|
- elasticsearch-dev-service.yaml
|
||||||
|
- elasticsearch-test-service.yaml
|
||||||
vars:
|
vars:
|
||||||
- name: elasticsearch-test-service-name
|
- name: elasticsearch-test-service-name
|
||||||
objref:
|
objref:
|
||||||
@@ -256,11 +256,8 @@ spec:
|
|||||||
`
|
`
|
||||||
|
|
||||||
// TestVariablesAmbiguous demonstrates how two variables pointing at two different resources
|
// TestVariablesAmbiguous demonstrates how two variables pointing at two different resources
|
||||||
// using the same name in different namespaces are treated as ambiguous
|
// using the same name in different namespaces are treated as ambiguous if the namespace is
|
||||||
// The fundamental reason is that objRef field in the variable does not contain a namespace
|
// not specified
|
||||||
// qualifier.
|
|
||||||
// Once the namespace qualifer is added, as for the other resources lookup in the coder,
|
|
||||||
// the ResId.GvknEquals method usage will have to be phased out and replaced by ResId.Equals.
|
|
||||||
func TestVariablesAmbiguous(t *testing.T) {
|
func TestVariablesAmbiguous(t *testing.T) {
|
||||||
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/myapp")
|
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/myapp")
|
||||||
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyApp)
|
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyApp)
|
||||||
@@ -335,3 +332,56 @@ resources:
|
|||||||
}
|
}
|
||||||
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const namespaceNeedInVarMyAppWithNamespace string = `
|
||||||
|
resources:
|
||||||
|
- elasticsearch-dev-service.yaml
|
||||||
|
- elasticsearch-test-service.yaml
|
||||||
|
vars:
|
||||||
|
- name: elasticsearch-test-service-name
|
||||||
|
objref:
|
||||||
|
kind: Service
|
||||||
|
name: elasticsearch
|
||||||
|
namespace: test
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: metadata.name
|
||||||
|
- name: elasticsearch-test-protocol
|
||||||
|
objref:
|
||||||
|
kind: Service
|
||||||
|
name: elasticsearch
|
||||||
|
namespace: test
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: spec.ports[0].protocol
|
||||||
|
- name: elasticsearch-dev-service-name
|
||||||
|
objref:
|
||||||
|
kind: Service
|
||||||
|
name: elasticsearch
|
||||||
|
namespace: dev
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: metadata.name
|
||||||
|
- name: elasticsearch-dev-protocol
|
||||||
|
objref:
|
||||||
|
kind: Service
|
||||||
|
name: elasticsearch
|
||||||
|
namespace: dev
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: spec.ports[0].protocol
|
||||||
|
`
|
||||||
|
|
||||||
|
// TestVariablesDisambiguatedWithNamespace demonstrates that adding the namespace
|
||||||
|
// to the variable declarations allows to disambiguate the variables.
|
||||||
|
func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
|
||||||
|
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/myapp")
|
||||||
|
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyAppWithNamespace)
|
||||||
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
||||||
|
m, err := th.MakeKustTarget().MakeCustomizedResMap()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Err: %v", err)
|
||||||
|
}
|
||||||
|
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ type Target struct {
|
|||||||
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
|
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
|
||||||
gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||||
Name string `json:"name" yaml:"name"`
|
Name string `json:"name" yaml:"name"`
|
||||||
|
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FieldSelector contains the fieldPath to an object field.
|
// FieldSelector contains the fieldPath to an object field.
|
||||||
|
|||||||
Reference in New Issue
Block a user