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.
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 {

View File

@@ -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")
},
}