fix: handle passing namespace downstream more elegant

This commit is contained in:
Niklas Wagner
2025-08-25 11:01:45 +02:00
parent c7612d1dba
commit 976a7cf2aa
2 changed files with 25 additions and 16 deletions

View File

@@ -29,13 +29,14 @@ import (
// KustTarget encapsulates the entirety of a kustomization build. // KustTarget encapsulates the entirety of a kustomization build.
type KustTarget struct { type KustTarget struct {
kustomization *types.Kustomization kustomization *types.Kustomization
kustFileName string kustFileName string
ldr ifc.Loader ldr ifc.Loader
validator ifc.Validator validator ifc.Validator
rFactory *resmap.Factory rFactory *resmap.Factory
pLdr *loader.Loader pLdr *loader.Loader
origin *resource.Origin origin *resource.Origin
inheritedNamespace string // For helm chart namespace inheritance without general propagation
} }
// NewKustTarget returns a new instance of KustTarget. // 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) { 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 var origin *resource.Origin
if len(kt.kustomization.BuildMetadata) != 0 || len(kt.kustomization.HelmCharts) > 0 { if len(kt.kustomization.BuildMetadata) != 0 {
origin = &resource.Origin{} origin = &resource.Origin{}
} }
kt.origin = origin kt.origin = origin
@@ -498,10 +497,13 @@ func (kt *KustTarget) accumulateDirectory(
} }
subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata
subKt.origin = kt.origin subKt.origin = kt.origin
// Propagate namespace to child kustomization if child doesn't have one // Pass down namespace context for helm chart inheritance, but don't set it on the kustomization
// This ensures Helm charts in base kustomizations inherit namespace from overlays // This avoids the performance penalty of general namespace propagation while still supporting
if subKt.kustomization.Namespace == "" && kt.kustomization.Namespace != "" { // helm chart namespace inheritance from parent overlays
subKt.kustomization.Namespace = kt.kustomization.Namespace if kt.kustomization.Namespace != "" {
subKt.inheritedNamespace = kt.kustomization.Namespace
} else if kt.inheritedNamespace != "" {
subKt.inheritedNamespace = kt.inheritedNamespace
} }
var bytes []byte var bytes []byte
if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists { if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists {

View File

@@ -168,8 +168,14 @@ var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func(
c.HelmChart = chart c.HelmChart = chart
// Pass kustomize namespace to helm // Pass kustomize namespace to helm
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/5566 // Fixes https://github.com/kubernetes-sigs/kustomize/issues/5566
if c.HelmChart.Namespace == "" && kt.kustomization.Namespace != "" { // Also check for inherited namespace context for nested kustomizations
c.HelmChart.Namespace = kt.kustomization.Namespace 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() p := f()
if err = kt.configureBuiltinPlugin(p, c, bpt); err != nil { 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") return nil, fmt.Errorf("valueadd keyword not yet defined")
}, },
} }