Skip to content

Commit 798d339

Browse files
authored
Merge pull request #6044 from koba1t/fix/namespace_propagation_problem_at_v5.8.0
Fix namespace propagation problem at v5.8.0
2 parents 6c8c9cc + 7e45799 commit 798d339

File tree

4 files changed

+436
-12
lines changed

4 files changed

+436
-12
lines changed

api/internal/target/kusttarget.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ import (
2929

3030
// KustTarget encapsulates the entirety of a kustomization build.
3131
type KustTarget struct {
32-
kustomization *types.Kustomization
33-
kustFileName string
34-
ldr ifc.Loader
35-
validator ifc.Validator
36-
rFactory *resmap.Factory
37-
pLdr *loader.Loader
38-
origin *resource.Origin
32+
kustomization *types.Kustomization
33+
kustFileName string
34+
ldr ifc.Loader
35+
validator ifc.Validator
36+
rFactory *resmap.Factory
37+
pLdr *loader.Loader
38+
origin *resource.Origin
39+
helmRootNamespace string // namespace inherited from parent kustomization for HelmCharts
3940
}
4041

4142
// NewKustTarget returns a new instance of KustTarget.
@@ -496,10 +497,15 @@ func (kt *KustTarget) accumulateDirectory(
496497
}
497498
subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata
498499
subKt.origin = kt.origin
499-
// Propagate namespace to child kustomization if child doesn't have one
500+
// Propagate namespace to child kustomization's helmRootNamespace for HelmCharts
500501
// This ensures Helm charts in base kustomizations inherit namespace from overlays
501-
if subKt.kustomization.Namespace == "" && kt.kustomization.Namespace != "" {
502-
subKt.kustomization.Namespace = kt.kustomization.Namespace
502+
// without affecting other transformers like patches
503+
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/6031
504+
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/6027
505+
if kt.kustomization.Namespace != "" {
506+
subKt.helmRootNamespace = kt.kustomization.Namespace
507+
} else if kt.helmRootNamespace != "" {
508+
subKt.helmRootNamespace = kt.helmRootNamespace
503509
}
504510
var bytes []byte
505511
if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists {

api/internal/target/kusttarget_configplugin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,13 @@ var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func(
168168
c.HelmChart = chart
169169
// Pass kustomize namespace to helm
170170
// Fixes https://github.com/kubernetes-sigs/kustomize/issues/5566
171-
if c.HelmChart.Namespace == "" && kt.kustomization.Namespace != "" {
172-
c.HelmChart.Namespace = kt.kustomization.Namespace
171+
// Also propagate parent namespace for multi-level kustomization hierarchies
172+
if c.HelmChart.Namespace == "" {
173+
if kt.kustomization.Namespace != "" {
174+
c.HelmChart.Namespace = kt.kustomization.Namespace
175+
} else if kt.helmRootNamespace != "" {
176+
c.HelmChart.Namespace = kt.helmRootNamespace
177+
}
173178
}
174179
p := f()
175180
if err = kt.configureBuiltinPlugin(p, c, bpt); err != nil {

api/krusty/helmchartinflationgenerator_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,76 @@ metadata:
12271227
`)
12281228
}
12291229

1230+
// Regression test: verify that HelmCharts in base kustomizations
1231+
// still receive namespace from overlay after fixing namespace propagation issues.
1232+
// This test ensures the fix for https://github.com/kubernetes-sigs/kustomize/issues/6031
1233+
// and https://github.com/kubernetes-sigs/kustomize/issues/6027
1234+
// does not break the HelmChart namespace propagation feature from
1235+
// https://github.com/kubernetes-sigs/kustomize/issues/5566
1236+
func TestHelmChartNamespacePropagationViaResourcesThreeLevels(t *testing.T) {
1237+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
1238+
defer th.Reset()
1239+
if err := th.ErrIfNoHelm(); err != nil {
1240+
t.Skip("skipping: " + err.Error())
1241+
}
1242+
1243+
// Create base directory with helm chart (Level 1)
1244+
baseDir := th.MkDir("base")
1245+
chartDir := filepath.Join(baseDir, "charts", "service")
1246+
require.NoError(t, th.GetFSys().MkdirAll(filepath.Join(chartDir, "templates")))
1247+
th.WriteF(filepath.Join(chartDir, "Chart.yaml"), `
1248+
apiVersion: v2
1249+
name: service
1250+
type: application
1251+
version: 1.0.0
1252+
`)
1253+
th.WriteF(filepath.Join(chartDir, "values.yaml"), ``)
1254+
th.WriteF(filepath.Join(chartDir, "templates", "service.yaml"), `
1255+
apiVersion: v1
1256+
kind: Service
1257+
metadata:
1258+
name: test-service
1259+
namespace: {{ .Release.Namespace }}
1260+
annotations:
1261+
helm-namespace: {{ .Release.Namespace }}
1262+
`)
1263+
1264+
// Base kustomization with helmCharts (no namespace)
1265+
th.WriteK(baseDir, `
1266+
helmGlobals:
1267+
chartHome: ./charts
1268+
helmCharts:
1269+
- name: service
1270+
releaseName: service
1271+
`)
1272+
1273+
// Mid-layer that references base via resources (no namespace) (Level 2)
1274+
midDir := th.MkDir("mid")
1275+
th.WriteK(midDir, `
1276+
namePrefix: mid-
1277+
resources:
1278+
- ../base
1279+
`)
1280+
1281+
// Top overlay that references mid-layer and sets namespace (Level 3)
1282+
overlayDir := th.MkDir("overlay")
1283+
th.WriteK(overlayDir, `
1284+
namespace: production
1285+
resources:
1286+
- ../mid
1287+
`)
1288+
1289+
m := th.Run(overlayDir, th.MakeOptionsPluginsEnabled())
1290+
th.AssertActualEqualsExpected(m, `apiVersion: v1
1291+
kind: Service
1292+
metadata:
1293+
annotations:
1294+
helm-namespace: production
1295+
name: mid-test-service
1296+
namespace: production
1297+
`)
1298+
}
1299+
12301300
func copyValuesFilesTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced) {
12311301
t.Helper()
12321302

0 commit comments

Comments
 (0)