Skip to content

Commit 82aa3c4

Browse files
committed
Extend HelmChart to support helm --set-file
1 parent f80d9c4 commit 82aa3c4

File tree

10 files changed

+186
-0
lines changed

10 files changed

+186
-0
lines changed

api/internal/builtins/HelmChartInflationGenerator.go

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

api/krusty/helmchartinflationgenerator_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,84 @@ metadata:
285285
`)
286286
}
287287

288+
func TestHelmChartInflationGeneratorSetFile(t *testing.T) {
289+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
290+
defer th.Reset()
291+
if err := th.ErrIfNoHelm(); err != nil {
292+
t.Skip("skipping: " + err.Error())
293+
}
294+
295+
copyValuesFilesTestChartsIntoHarness(t, th)
296+
297+
th.WriteF(filepath.Join(th.GetRoot(), "game.properties"), `
298+
enemy.types=aliens,monsters
299+
player.maximum-lives=5
300+
`)
301+
302+
th.WriteF(filepath.Join(th.GetRoot(), "user-interface.yaml"), `
303+
color:
304+
good: purple
305+
bad: yellow
306+
allow:
307+
textmode: true
308+
`)
309+
310+
th.WriteK(th.GetRoot(), `
311+
helmCharts:
312+
- name: test-chart
313+
releaseName: test-chart
314+
setFile:
315+
config.game: game.properties
316+
config.ui: user-interface.yaml
317+
`)
318+
319+
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
320+
asYaml, err := m.AsYaml()
321+
require.NoError(t, err)
322+
require.Equal(t, string(asYaml), `apiVersion: v1
323+
data:
324+
game.properties: |2
325+
326+
enemy.types=aliens,monsters
327+
player.maximum-lives=5
328+
user-interface.yaml: |2
329+
330+
color:
331+
good: purple
332+
bad: yellow
333+
allow:
334+
textmode: true
335+
kind: ConfigMap
336+
metadata:
337+
name: game-demo
338+
---
339+
apiVersion: apps/v1
340+
kind: Deployment
341+
metadata:
342+
labels:
343+
chart: test-1.0.0
344+
name: my-deploy
345+
namespace: default
346+
spec:
347+
replicas: 1
348+
selector:
349+
matchLabels:
350+
app: test
351+
template:
352+
spec:
353+
containers:
354+
- image: test-image:v1.0.0
355+
imagePullPolicy: Always
356+
---
357+
apiVersion: apps/v1
358+
kind: Pod
359+
metadata:
360+
annotations:
361+
helm.sh/hook: test
362+
name: test-chart
363+
`)
364+
}
365+
288366
func TestHelmChartInflationGeneratorApiVersions(t *testing.T) {
289367
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
290368
defer th.Reset()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
{{- if .Values.config }}
3+
apiVersion: v1
4+
kind: ConfigMap
5+
metadata:
6+
name: game-demo
7+
data:
8+
# file-like keys
9+
game.properties: {{ .Values.config.game | toYaml | indent 2 }}
10+
user-interface.yaml: {{ .Values.config.ui | toYaml | indent 2 }}
11+
{{- end }}

api/types/helmchartargs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ type HelmChart struct {
6363
// addition to either the default values file or the values specified in ValuesFile.
6464
AdditionalValuesFiles []string `json:"additionalValuesFiles,omitempty" yaml:"additionalValuesFiles,omitempty"`
6565

66+
// SetFile holds value mappings where the value is specified in a file.
67+
// This allows setting values from respective files. The file's content
68+
// will become the value's content.
69+
SetFile map[string]string `json:"setFile,omitempty" yaml:"setFile,omitempty"`
70+
6671
// ValuesFile is a local file path to a values file to use _instead of_
6772
// the default values that accompanied the chart.
6873
// The default values are in '{ChartHome}/{Name}/values.yaml'.
@@ -168,6 +173,9 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string {
168173
for _, valuesFile := range h.AdditionalValuesFiles {
169174
args = append(args, "-f", valuesFile)
170175
}
176+
for key, file := range h.SetFile {
177+
args = append(args, "--set-file", key+"="+file)
178+
}
171179

172180
for _, apiVer := range h.ApiVersions {
173181
args = append(args, "--api-versions", apiVer)

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ func (p *plugin) validateArgs() (err error) {
107107
// the additional values filepaths must be relative to the kust root
108108
p.AdditionalValuesFiles[i] = filepath.Join(p.h.Loader().Root(), file)
109109
}
110+
for key, file := range p.SetFile {
111+
// use Load() to enforce root restrictions
112+
if _, err := p.h.Loader().Load(file); err != nil {
113+
return errors.WrapPrefixf(err, "could not load file '%s' specified in SetFile", file)
114+
}
115+
// the filepaths must be relative to the kust root
116+
p.SetFile[key] = filepath.Join(p.h.Loader().Root(), file)
117+
}
110118

111119
if err = p.errIfIllegalValuesMerge(); err != nil {
112120
return err

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,62 @@ valuesFile: myValues.yaml
429429
))
430430
}
431431

432+
func TestHelmChartInflationGeneratorSetFile(t *testing.T) {
433+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
434+
PrepBuiltin("HelmChartInflationGenerator")
435+
defer th.Reset()
436+
if err := th.ErrIfNoHelm(); err != nil {
437+
t.Skip("skipping: " + err.Error())
438+
}
439+
440+
copyTestChartsIntoHarness(t, th)
441+
442+
th.WriteF(filepath.Join(th.GetRoot(), "game.properties"), `
443+
enemy.types=aliens,monsters
444+
player.maximum-lives=5
445+
`)
446+
447+
th.WriteF(filepath.Join(th.GetRoot(), "user-interface.yaml"), `
448+
color:
449+
good: purple
450+
bad: yellow
451+
allow:
452+
textmode: true
453+
`)
454+
455+
rm := th.LoadAndRunGenerator(`
456+
apiVersion: builtin
457+
kind: HelmChartInflationGenerator
458+
metadata:
459+
name: test-chart
460+
name: test-chart
461+
releaseName: test-chart
462+
chartHome: ./charts
463+
setFile:
464+
config.game: game.properties
465+
config.ui: user-interface.yaml
466+
`)
467+
468+
th.AssertActualEqualsExpected(rm, `
469+
apiVersion: v1
470+
data:
471+
game.properties: |2
472+
473+
enemy.types=aliens,monsters
474+
player.maximum-lives=5
475+
user-interface.yaml: |2
476+
477+
color:
478+
good: purple
479+
bad: yellow
480+
allow:
481+
textmode: true
482+
kind: ConfigMap
483+
metadata:
484+
name: game-demo
485+
`)
486+
}
487+
432488
func TestHelmChartInflationGeneratorWithInLineReplace(t *testing.T) {
433489
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
434490
PrepBuiltin("HelmChartInflationGenerator")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
appVersion: "1.0"
3+
description: A simple test helm chart.
4+
name: test
5+
version: 1.0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a simple test chart.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
{{- if .Values.config }}
3+
apiVersion: v1
4+
kind: ConfigMap
5+
metadata:
6+
name: game-demo
7+
data:
8+
# file-like keys
9+
game.properties: {{ .Values.config.game | toYaml | indent 2 }}
10+
user-interface.yaml: {{ .Values.config.ui | toYaml | indent 2 }}
11+
{{- end }}

plugin/builtin/helmchartinflationgenerator/testdata/charts/test-chart/values.yaml

Whitespace-only changes.

0 commit comments

Comments
 (0)