Skip to content

Commit

Permalink
Merge pull request #5133 from timja/issue-5072-non-core-api-version-n…
Browse files Browse the repository at this point in the history
…amespace

Only override name of core api version
  • Loading branch information
k8s-ci-robot committed Apr 18, 2023
2 parents 315ed56 + 75fa235 commit 2ce1c7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
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

0 comments on commit 2ce1c7c

Please sign in to comment.