Skip to content

Commit 2ce1c7c

Browse files
authored
Merge pull request #5133 from timja/issue-5072-non-core-api-version-namespace
Only override name of core api version
2 parents 315ed56 + 75fa235 commit 2ce1c7c

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

api/filters/namespace/namespace.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ func (ns Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
5656

5757
// Run runs the filter on a single node rather than a slice
5858
func (ns Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
59-
// Special handling for metadata.namespace -- :(
59+
// Special handling for metadata.namespace and metadata.name -- :(
6060
// never let SetEntry handle metadata.namespace--it will incorrectly include cluster-scoped resources
61-
ns.FsSlice = ns.removeMetaNamespaceFieldSpecs(ns.FsSlice)
61+
// only update metadata.name if api version is expected one--so-as it leaves other resources of kind namespace alone
62+
apiVersion := node.GetApiVersion()
63+
ns.FsSlice = ns.removeUnneededMetaFieldSpecs(apiVersion, ns.FsSlice)
6264
gvk := resid.GvkFromNode(node)
6365
if err := ns.metaNamespaceHack(node, gvk); err != nil {
6466
return nil, err
@@ -186,12 +188,15 @@ func (ns Filter) removeRoleBindingSubjectFieldSpecs(fs types.FsSlice) types.FsSl
186188
return val
187189
}
188190

189-
func (ns Filter) removeMetaNamespaceFieldSpecs(fs types.FsSlice) types.FsSlice {
191+
func (ns Filter) removeUnneededMetaFieldSpecs(apiVersion string, fs types.FsSlice) types.FsSlice {
190192
var val types.FsSlice
191193
for i := range fs {
192194
if fs[i].Path == types.MetadataNamespacePath {
193195
continue
194196
}
197+
if apiVersion != types.MetadataNamespaceApiVersion && fs[i].Path == types.MetadataNamePath {
198+
continue
199+
}
195200
val = append(val, fs[i])
196201
}
197202
return val

api/krusty/namespaces_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,31 @@ metadata:
801801
namespace: iter8-monitoring
802802
`)
803803
}
804+
805+
// Demonstrates that metadata.name is only overridden for a kind: Namespace with apiVersion: v1
806+
// Test for issue #5072
807+
func TestNameNotOveriddenForNonCoreApiVersionOnANamespaceKind(t *testing.T) {
808+
th := kusttest_test.MakeHarness(t)
809+
810+
th.WriteF("azure-servicebus.yaml", `
811+
apiVersion: servicebus.azure.com/v1beta20210101preview
812+
kind: Namespace
813+
metadata:
814+
name: core-sb-99
815+
namespace: without-podinfo
816+
`)
817+
th.WriteK(".", `
818+
namespace: podinfo
819+
resources:
820+
- azure-servicebus.yaml
821+
`)
822+
823+
m := th.Run(".", th.MakeDefaultOptions())
824+
th.AssertActualEqualsExpected(m, `
825+
apiVersion: servicebus.azure.com/v1beta20210101preview
826+
kind: Namespace
827+
metadata:
828+
name: core-sb-99
829+
namespace: podinfo
830+
`)
831+
}

api/types/kustomization.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import (
1414
)
1515

1616
const (
17-
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
18-
KustomizationKind = "Kustomization"
19-
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
20-
ComponentKind = "Component"
21-
MetadataNamespacePath = "metadata/namespace"
17+
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
18+
KustomizationKind = "Kustomization"
19+
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
20+
ComponentKind = "Component"
21+
MetadataNamespacePath = "metadata/namespace"
22+
MetadataNamespaceApiVersion = "v1"
23+
MetadataNamePath = "metadata/name"
2224

2325
OriginAnnotations = "originAnnotations"
2426
TransformerAnnotations = "transformerAnnotations"

0 commit comments

Comments
 (0)