Skip to content

Commit

Permalink
Extend HelmChart to support helm --set-file
Browse files Browse the repository at this point in the history
  • Loading branch information
dakr0013 committed Oct 21, 2023
1 parent f80d9c4 commit 82aa3c4
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/internal/builtins/HelmChartInflationGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions api/krusty/helmchartinflationgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,84 @@ metadata:
`)
}

func TestHelmChartInflationGeneratorSetFile(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}

copyValuesFilesTestChartsIntoHarness(t, th)

th.WriteF(filepath.Join(th.GetRoot(), "game.properties"), `
enemy.types=aliens,monsters
player.maximum-lives=5
`)

th.WriteF(filepath.Join(th.GetRoot(), "user-interface.yaml"), `
color:
good: purple
bad: yellow
allow:
textmode: true
`)

th.WriteK(th.GetRoot(), `
helmCharts:
- name: test-chart
releaseName: test-chart
setFile:
config.game: game.properties
config.ui: user-interface.yaml
`)

m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
asYaml, err := m.AsYaml()
require.NoError(t, err)
require.Equal(t, string(asYaml), `apiVersion: v1
data:
game.properties: |2
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.yaml: |2
color:
good: purple
bad: yellow
allow:
textmode: true
kind: ConfigMap
metadata:
name: game-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
chart: test-1.0.0
name: my-deploy
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: test
template:
spec:
containers:
- image: test-image:v1.0.0
imagePullPolicy: Always
---
apiVersion: apps/v1
kind: Pod
metadata:
annotations:
helm.sh/hook: test
name: test-chart
`)
}

func TestHelmChartInflationGeneratorApiVersions(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t)
defer th.Reset()
Expand Down
11 changes: 11 additions & 0 deletions api/krusty/testdata/helmcharts/test-chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
{{- if .Values.config }}
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# file-like keys
game.properties: {{ .Values.config.game | toYaml | indent 2 }}
user-interface.yaml: {{ .Values.config.ui | toYaml | indent 2 }}
{{- end }}
8 changes: 8 additions & 0 deletions api/types/helmchartargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type HelmChart struct {
// addition to either the default values file or the values specified in ValuesFile.
AdditionalValuesFiles []string `json:"additionalValuesFiles,omitempty" yaml:"additionalValuesFiles,omitempty"`

// SetFile holds value mappings where the value is specified in a file.
// This allows setting values from respective files. The file's content
// will become the value's content.
SetFile map[string]string `json:"setFile,omitempty" yaml:"setFile,omitempty"`

// ValuesFile is a local file path to a values file to use _instead of_
// the default values that accompanied the chart.
// The default values are in '{ChartHome}/{Name}/values.yaml'.
Expand Down Expand Up @@ -168,6 +173,9 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string {
for _, valuesFile := range h.AdditionalValuesFiles {
args = append(args, "-f", valuesFile)
}
for key, file := range h.SetFile {
args = append(args, "--set-file", key+"="+file)
}

for _, apiVer := range h.ApiVersions {
args = append(args, "--api-versions", apiVer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (p *plugin) validateArgs() (err error) {
// the additional values filepaths must be relative to the kust root
p.AdditionalValuesFiles[i] = filepath.Join(p.h.Loader().Root(), file)
}
for key, file := range p.SetFile {
// use Load() to enforce root restrictions
if _, err := p.h.Loader().Load(file); err != nil {
return errors.WrapPrefixf(err, "could not load file '%s' specified in SetFile", file)
}
// the filepaths must be relative to the kust root
p.SetFile[key] = filepath.Join(p.h.Loader().Root(), file)
}

if err = p.errIfIllegalValuesMerge(); err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,62 @@ valuesFile: myValues.yaml
))
}

func TestHelmChartInflationGeneratorSetFile(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}

copyTestChartsIntoHarness(t, th)

th.WriteF(filepath.Join(th.GetRoot(), "game.properties"), `
enemy.types=aliens,monsters
player.maximum-lives=5
`)

th.WriteF(filepath.Join(th.GetRoot(), "user-interface.yaml"), `
color:
good: purple
bad: yellow
allow:
textmode: true
`)

rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: test-chart
name: test-chart
releaseName: test-chart
chartHome: ./charts
setFile:
config.game: game.properties
config.ui: user-interface.yaml
`)

th.AssertActualEqualsExpected(rm, `
apiVersion: v1
data:
game.properties: |2
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.yaml: |2
color:
good: purple
bad: yellow
allow:
textmode: true
kind: ConfigMap
metadata:
name: game-demo
`)
}

func TestHelmChartInflationGeneratorWithInLineReplace(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A simple test helm chart.
name: test
version: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a simple test chart.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
{{- if .Values.config }}
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# file-like keys
game.properties: {{ .Values.config.game | toYaml | indent 2 }}
user-interface.yaml: {{ .Values.config.ui | toYaml | indent 2 }}
{{- end }}
Empty file.

0 comments on commit 82aa3c4

Please sign in to comment.