Skip to content

Commit 1dcdb51

Browse files
committed
feat(helm-namespace): consistent namespace handling for Helm charts and update related tests
1 parent 6605d1b commit 1dcdb51

File tree

13 files changed

+153
-16
lines changed

13 files changed

+153
-16
lines changed

api/internal/builtins/HelmChartInflationGenerator.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/internal/builtins/NamespaceTransformer.go

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/konfig/general.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ const (
4949

5050
// Annotation key for marking helm-generated resources to skip namespace transformation
5151
HelmGeneratedAnnotation = ConfigAnnoDomain + "/helm-generated"
52+
53+
// Annotation key for preserving the effective Helm chart namespace during build-time transforms.
54+
HelmChartNamespaceAnnotation = ConfigAnnoDomain + "/helm-chart-namespace"
5255
)

api/krusty/helmchartinflationgenerator_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,52 @@ metadata:
11791179
`)
11801180
}
11811181

1182+
func TestHelmChartDifferentNamespacesMissingResourceNamespace(t *testing.T) {
1183+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
1184+
defer th.Reset()
1185+
if err := th.ErrIfNoHelm(); err != nil {
1186+
t.Skip("skipping: " + err.Error())
1187+
}
1188+
1189+
chartDir := filepath.Join(th.GetRoot(), "charts", "service")
1190+
require.NoError(t, th.GetFSys().MkdirAll(filepath.Join(chartDir, "templates")))
1191+
th.WriteF(filepath.Join(chartDir, "Chart.yaml"), `
1192+
apiVersion: v2
1193+
name: service
1194+
type: application
1195+
version: 1.0.0
1196+
`)
1197+
th.WriteF(filepath.Join(chartDir, "values.yaml"), ``)
1198+
th.WriteF(filepath.Join(chartDir, "templates", "service.yaml"), `
1199+
apiVersion: v1
1200+
kind: Service
1201+
metadata:
1202+
name: test-service
1203+
annotations:
1204+
release-namespace: {{ .Release.Namespace }}
1205+
`)
1206+
1207+
th.WriteK(th.GetRoot(), `
1208+
helmGlobals:
1209+
chartHome: ./charts
1210+
namespace: transformer-ns
1211+
helmCharts:
1212+
- name: service
1213+
releaseName: service
1214+
namespace: helm-ns
1215+
`)
1216+
1217+
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
1218+
th.AssertActualEqualsExpected(m, `apiVersion: v1
1219+
kind: Service
1220+
metadata:
1221+
annotations:
1222+
release-namespace: helm-ns
1223+
name: test-service
1224+
namespace: helm-ns
1225+
`)
1226+
}
1227+
11821228
func TestHelmChartSameNamespace(t *testing.T) {
11831229
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
11841230
defer th.Reset()

api/resource/resource.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var BuildAnnotations = []string{
5050
kioutil.LegacyIdAnnotation,
5151

5252
konfig.HelmGeneratedAnnotation,
53+
konfig.HelmChartNamespaceAnnotation,
5354
}
5455

5556
func (r *Resource) ResetRNode(incoming *Resource) {
@@ -160,7 +161,9 @@ func (r *Resource) DeepCopy() *Resource {
160161
// the resource.
161162
// TODO: move to RNode, use GetMeta to improve performance.
162163
// TODO: make a version of mergeStringMaps that is build-annotation aware
163-
// to avoid repeatedly setting refby and genargs annotations
164+
//
165+
// to avoid repeatedly setting refby and genargs annotations
166+
//
164167
// Must remove the kustomize bit at the end.
165168
func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error {
166169
if err := r.SetLabels(

examples/helmNamespace/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ output=$(kustomize build \
3131
printf '%s\n' "$output"
3232
```
3333

34+
## Helm Chart with Namespace in `helmChart.namespace`
35+
3436
The Service is emitted by the chart without a namespace, so the example namespace should be applied:
3537

3638
<!-- @checkMissingNamespaceFilled @testHelm -->
3739
```sh
38-
printf '%s\n' "$output" | grep -A4 'name: chart-service' | grep 'namespace: top-level-ns'
40+
printf '%s\n' "$output" | grep -A4 'name: chart-service' | grep 'namespace: chart-ns'
3941
```
4042

4143
The ConfigMap is emitted by the chart with an explicit namespace, so that value should be preserved:
@@ -51,3 +53,26 @@ The Secret is emitted by the chart with an release namespace, so that value shou
5153
```sh
5254
printf '%s\n' "$output" | grep -A4 'name: chart-secret' | grep 'namespace: chart-ns'
5355
```
56+
57+
## Helm Chart without Namespace in `helmChart.namespace`
58+
59+
The Service is emitted by the chart without a namespace, so the example namespace should be applied:
60+
61+
<!-- @checkMissingNamespaceFilled @testHelm -->
62+
```sh
63+
printf '%s\n' "$output" | grep -A4 'name: chart-service' | grep 'namespace: top-level-ns'
64+
```
65+
66+
The ConfigMap is emitted by the chart with an explicit namespace, so that value should be preserved:
67+
68+
<!-- @checkExistingNamespacePreserved @testHelm -->
69+
```sh
70+
printf '%s\n' "$output" | grep -A4 'name: chart-config' | grep 'namespace: chart-owned-ns'
71+
```
72+
73+
The Secret is emitted by the chart with an release namespace, so that value should be preserved:
74+
75+
<!-- @checkExistingNamespacePreserved @testHelm -->
76+
```sh
77+
printf '%s\n' "$output" | grep -A4 'name: chart-secret' | grep 'namespace: top-level-ns'
78+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: v1
22
kind: ConfigMap
33
metadata:
4-
name: chart-config
4+
name: {{ .Release.Name }}-config
55
namespace: chart-owned-ns
66
data:
77
owner: helm-chart
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: v1
22
kind: Secret
33
metadata:
4-
name: chart-secret
4+
name: {{ .Release.Name }}-secret
55
namespace: {{ .Release.Namespace }}
66
data:
77
.secret-file: dmFsdWUtMg0KDQo=
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: chart-service
4+
name: {{ .Release.Name }}-service
55
spec:
66
selector:
7-
app: chart-service
7+
app: app-service
88
ports:
99
- port: 80
1010
targetPort: 8080

examples/helmNamespace/kustomization.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ helmGlobals:
77
chartHome: charts
88

99
helmCharts:
10+
# test a contains a namespace
1011
- name: namespace-fill
12+
releaseName: test-a
1113
namespace: chart-ns
14+
# test b inherits the namespace from top level
15+
- name: namespace-fill
16+
releaseName: test-b

0 commit comments

Comments
 (0)