Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend HelmChart to support helm --set-file #5384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -436,6 +436,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 @@ -430,6 +430,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: bar
`)
}

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 @@
This is a simple test chart.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.foo }}
{{- if .Values.config }}
data:
# file-like keys
game.properties: {{ .Values.config.game | toYaml | indent 2 }}
user-interface.yaml: {{ .Values.config.ui | toYaml | indent 2 }}
{{- end }}
Loading