Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions api/filters/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func (ns Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {

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

func (ns Filter) removeMetaNamespaceFieldSpecs(fs types.FsSlice) types.FsSlice {
func (ns Filter) removeUnneededMetaFieldSpecs(apiVersion string, fs types.FsSlice) types.FsSlice {
var val types.FsSlice
for i := range fs {
if fs[i].Path == types.MetadataNamespacePath {
continue
}
if apiVersion != types.MetadataNamespaceApiVersion && fs[i].Path == types.MetadataNamePath {
continue
}
val = append(val, fs[i])
}
return val
Expand Down
28 changes: 28 additions & 0 deletions api/krusty/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,31 @@ metadata:
namespace: iter8-monitoring
`)
}

// Demonstrates that metadata.name is only overridden for a kind: Namespace with apiVersion: v1
// Test for issue #5072
func TestNameNotOveriddenForNonCoreApiVersionOnANamespaceKind(t *testing.T) {
th := kusttest_test.MakeHarness(t)

th.WriteF("azure-servicebus.yaml", `
apiVersion: servicebus.azure.com/v1beta20210101preview
kind: Namespace
metadata:
name: core-sb-99
namespace: without-podinfo
`)
th.WriteK(".", `
namespace: podinfo
resources:
- azure-servicebus.yaml
`)

m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: servicebus.azure.com/v1beta20210101preview
kind: Namespace
metadata:
name: core-sb-99
namespace: podinfo
`)
}
12 changes: 7 additions & 5 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
)

const (
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
KustomizationKind = "Kustomization"
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
ComponentKind = "Component"
MetadataNamespacePath = "metadata/namespace"
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
KustomizationKind = "Kustomization"
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
ComponentKind = "Component"
MetadataNamespacePath = "metadata/namespace"
MetadataNamespaceApiVersion = "v1"
MetadataNamePath = "metadata/name"

OriginAnnotations = "originAnnotations"
TransformerAnnotations = "transformerAnnotations"
Expand Down