From 976a7cf2aa28c77c7b3e6b21bce63cb5f68724cd Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Mon, 25 Aug 2025 11:01:45 +0200 Subject: [PATCH] fix: handle passing namespace downstream more elegant --- api/internal/target/kusttarget.go | 30 ++++++++++--------- .../target/kusttarget_configplugin.go | 11 +++++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 64fac33f5..606578b0b 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -29,13 +29,14 @@ import ( // KustTarget encapsulates the entirety of a kustomization build. type KustTarget struct { - kustomization *types.Kustomization - kustFileName string - ldr ifc.Loader - validator ifc.Validator - rFactory *resmap.Factory - pLdr *loader.Loader - origin *resource.Origin + kustomization *types.Kustomization + kustFileName string + ldr ifc.Loader + validator ifc.Validator + rFactory *resmap.Factory + pLdr *loader.Loader + origin *resource.Origin + inheritedNamespace string // For helm chart namespace inheritance without general propagation } // NewKustTarget returns a new instance of KustTarget. @@ -127,10 +128,8 @@ func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) { } func (kt *KustTarget) makeCustomizedResMap() (resmap.ResMap, error) { - // Only track origin if we have Helm charts or build metadata is requested - // This optimization avoids unnecessary overhead when origins aren't needed var origin *resource.Origin - if len(kt.kustomization.BuildMetadata) != 0 || len(kt.kustomization.HelmCharts) > 0 { + if len(kt.kustomization.BuildMetadata) != 0 { origin = &resource.Origin{} } kt.origin = origin @@ -498,10 +497,13 @@ func (kt *KustTarget) accumulateDirectory( } subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata subKt.origin = kt.origin - // Propagate namespace to child kustomization if child doesn't have one - // This ensures Helm charts in base kustomizations inherit namespace from overlays - if subKt.kustomization.Namespace == "" && kt.kustomization.Namespace != "" { - subKt.kustomization.Namespace = kt.kustomization.Namespace + // Pass down namespace context for helm chart inheritance, but don't set it on the kustomization + // This avoids the performance penalty of general namespace propagation while still supporting + // helm chart namespace inheritance from parent overlays + if kt.kustomization.Namespace != "" { + subKt.inheritedNamespace = kt.kustomization.Namespace + } else if kt.inheritedNamespace != "" { + subKt.inheritedNamespace = kt.inheritedNamespace } var bytes []byte if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists { diff --git a/api/internal/target/kusttarget_configplugin.go b/api/internal/target/kusttarget_configplugin.go index c41d6c84f..78659df1c 100644 --- a/api/internal/target/kusttarget_configplugin.go +++ b/api/internal/target/kusttarget_configplugin.go @@ -168,8 +168,14 @@ var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func( c.HelmChart = chart // Pass kustomize namespace to helm // Fixes https://github.com/kubernetes-sigs/kustomize/issues/5566 - if c.HelmChart.Namespace == "" && kt.kustomization.Namespace != "" { - c.HelmChart.Namespace = kt.kustomization.Namespace + // Also check for inherited namespace context for nested kustomizations + if c.HelmChart.Namespace == "" { + if kt.kustomization.Namespace != "" { + c.HelmChart.Namespace = kt.kustomization.Namespace + } else if kt.inheritedNamespace != "" { + // For nested kustomizations, use inherited namespace from parent + c.HelmChart.Namespace = kt.inheritedNamespace + } } p := f() if err = kt.configureBuiltinPlugin(p, c, bpt); err != nil { @@ -460,3 +466,4 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func( return nil, fmt.Errorf("valueadd keyword not yet defined") }, } +