diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 606578b0b..64fac33f5 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -29,14 +29,13 @@ 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 - inheritedNamespace string // For helm chart namespace inheritance without general propagation + kustomization *types.Kustomization + kustFileName string + ldr ifc.Loader + validator ifc.Validator + rFactory *resmap.Factory + pLdr *loader.Loader + origin *resource.Origin } // NewKustTarget returns a new instance of KustTarget. @@ -128,8 +127,10 @@ 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 { + if len(kt.kustomization.BuildMetadata) != 0 || len(kt.kustomization.HelmCharts) > 0 { origin = &resource.Origin{} } kt.origin = origin @@ -497,13 +498,10 @@ func (kt *KustTarget) accumulateDirectory( } subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata subKt.origin = kt.origin - // 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 + // 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 } 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 78659df1c..c41d6c84f 100644 --- a/api/internal/target/kusttarget_configplugin.go +++ b/api/internal/target/kusttarget_configplugin.go @@ -168,14 +168,8 @@ var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func( c.HelmChart = chart // Pass kustomize namespace to helm // Fixes https://github.com/kubernetes-sigs/kustomize/issues/5566 - // 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 - } + if c.HelmChart.Namespace == "" && kt.kustomization.Namespace != "" { + c.HelmChart.Namespace = kt.kustomization.Namespace } p := f() if err = kt.configureBuiltinPlugin(p, c, bpt); err != nil { @@ -466,4 +460,3 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func( return nil, fmt.Errorf("valueadd keyword not yet defined") }, } -