fix: apply namespace propagation for helmCharts only

This commit is contained in:
koba1t
2026-01-13 08:09:14 +09:00
parent a397f5b491
commit 4e75138d24
3 changed files with 23 additions and 13 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
parentNamespace string // namespace inherited from parent kustomization for HelmCharts
}
// NewKustTarget returns a new instance of KustTarget.
@@ -496,10 +497,15 @@ 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
// Propagate namespace to child kustomization's parentNamespace for HelmCharts
// This ensures Helm charts in base kustomizations inherit namespace from overlays
if subKt.kustomization.Namespace == "" && kt.kustomization.Namespace != "" {
subKt.kustomization.Namespace = kt.kustomization.Namespace
// without affecting other transformers like patches
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/6031
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/6027
if kt.kustomization.Namespace != "" {
subKt.parentNamespace = kt.kustomization.Namespace
} else if kt.parentNamespace != "" {
subKt.parentNamespace = kt.parentNamespace
}
var bytes []byte
if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists {

View File

@@ -168,8 +168,13 @@ 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 propagate parent namespace for multi-level kustomization hierarchies
if c.HelmChart.Namespace == "" {
if kt.kustomization.Namespace != "" {
c.HelmChart.Namespace = kt.kustomization.Namespace
} else if kt.parentNamespace != "" {
c.HelmChart.Namespace = kt.parentNamespace
}
}
p := f()
if err = kt.configureBuiltinPlugin(p, c, bpt); err != nil {

View File

@@ -149,7 +149,6 @@ OVERLAY_ENV=overlay
apiVersion: v1
data:
BASE_LAYER_ENV: base
COMMON_ENV: common
OVERLAY_ENV: overlay
kind: ConfigMap
metadata: