mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
support for more helm template args (#4926)
* support for more helm template args * move templateArgs and unit tests to api/types * undo package name change * use our own simple helm chart instead of forking one * add argument to AsHelmArgs * code review * lint errors
This commit is contained in:
@@ -4,9 +4,12 @@
|
||||
package krusty_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
)
|
||||
|
||||
const expectedHelm = `
|
||||
@@ -233,3 +236,193 @@ spec:
|
||||
type: ClusterIP
|
||||
`)
|
||||
}
|
||||
|
||||
func TestHelmChartInflationGeneratorMultipleValuesFiles(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.WriteK(th.GetRoot(), `
|
||||
helmCharts:
|
||||
- name: test-chart
|
||||
releaseName: test-chart
|
||||
additionalValuesFiles:
|
||||
- charts/valuesFiles/file1.yaml
|
||||
- charts/valuesFiles/file2.yaml
|
||||
`)
|
||||
|
||||
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
|
||||
asYaml, err := m.AsYaml()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(asYaml), `apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
chart: test-1.0.0
|
||||
name: my-deploy
|
||||
namespace: file-2
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: test
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- image: test-image-file1:file1
|
||||
imagePullPolicy: Never
|
||||
---
|
||||
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()
|
||||
if err := th.ErrIfNoHelm(); err != nil {
|
||||
t.Skip("skipping: " + err.Error())
|
||||
}
|
||||
|
||||
copyValuesFilesTestChartsIntoHarness(t, th)
|
||||
|
||||
th.WriteK(th.GetRoot(), `
|
||||
helmCharts:
|
||||
- name: test-chart
|
||||
releaseName: test-chart
|
||||
apiVersions:
|
||||
- foo/v1
|
||||
`)
|
||||
|
||||
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
|
||||
asYaml, err := m.AsYaml()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(asYaml), `apiVersion: foo/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: foo/v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
annotations:
|
||||
helm.sh/hook: test
|
||||
name: test-chart
|
||||
`)
|
||||
}
|
||||
|
||||
func TestHelmChartInflationGeneratorSkipTests(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.WriteK(th.GetRoot(), `
|
||||
helmCharts:
|
||||
- name: test-chart
|
||||
releaseName: test-chart
|
||||
skipTests: true
|
||||
`)
|
||||
|
||||
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
|
||||
asYaml, err := m.AsYaml()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(asYaml), `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
|
||||
`)
|
||||
}
|
||||
|
||||
func TestHelmChartInflationGeneratorNameTemplate(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.WriteK(th.GetRoot(), `
|
||||
helmCharts:
|
||||
- name: test-chart
|
||||
nameTemplate: name-template
|
||||
`)
|
||||
|
||||
m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled())
|
||||
asYaml, err := m.AsYaml()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(asYaml), `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: name-template
|
||||
`)
|
||||
}
|
||||
|
||||
func copyValuesFilesTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced) {
|
||||
t.Helper()
|
||||
|
||||
thDir := filepath.Join(th.GetRoot(), "charts")
|
||||
chartDir := "testdata/helmcharts"
|
||||
|
||||
fs := th.GetFSys()
|
||||
require.NoError(t, fs.MkdirAll(filepath.Join(thDir, "templates")))
|
||||
require.NoError(t, copyutil.CopyDir(th.GetFSys(), chartDir, thDir))
|
||||
}
|
||||
|
||||
5
api/krusty/testdata/helmcharts/test-chart/Chart.yaml
vendored
Normal file
5
api/krusty/testdata/helmcharts/test-chart/Chart.yaml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
apiVersion: v1
|
||||
appVersion: "1.0"
|
||||
description: A simple test helm chart.
|
||||
name: test
|
||||
version: 1.0.0
|
||||
1
api/krusty/testdata/helmcharts/test-chart/README.md
vendored
Normal file
1
api/krusty/testdata/helmcharts/test-chart/README.md
vendored
Normal file
@@ -0,0 +1 @@
|
||||
This is a simple test chart.
|
||||
7
api/krusty/testdata/helmcharts/test-chart/templates/_helpers.tpl
vendored
Normal file
7
api/krusty/testdata/helmcharts/test-chart/templates/_helpers.tpl
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{{- define "apiversion" -}}
|
||||
{{- if .Capabilities.APIVersions.Has "foo/v1" -}}
|
||||
foo/v1
|
||||
{{- else -}}
|
||||
apps/v1
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
18
api/krusty/testdata/helmcharts/test-chart/templates/deployment.yaml
vendored
Normal file
18
api/krusty/testdata/helmcharts/test-chart/templates/deployment.yaml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
---
|
||||
apiVersion: {{ template "apiversion" . }}
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
|
||||
name: my-deploy
|
||||
namespace: {{ .Values.data.namespace }}
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Chart.Name }}
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- image: "{{ .Values.data.image.name }}:{{ .Values.data.image.tag }}"
|
||||
imagePullPolicy: {{ .Values.data.image.imagePullPolicy }}
|
||||
6
api/krusty/testdata/helmcharts/test-chart/templates/tests/test-pod.yaml
vendored
Normal file
6
api/krusty/testdata/helmcharts/test-chart/templates/tests/test-pod.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
apiVersion: {{ template "apiversion" . }}
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
annotations:
|
||||
"helm.sh/hook": test
|
||||
6
api/krusty/testdata/helmcharts/test-chart/values.yaml
vendored
Normal file
6
api/krusty/testdata/helmcharts/test-chart/values.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
data:
|
||||
namespace: default
|
||||
image:
|
||||
name: test-image
|
||||
tag: v1.0.0
|
||||
imagePullPolicy: Always
|
||||
5
api/krusty/testdata/helmcharts/valuesFiles/file1.yaml
vendored
Normal file
5
api/krusty/testdata/helmcharts/valuesFiles/file1.yaml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
data:
|
||||
image:
|
||||
name: test-image-file1
|
||||
tag: file1
|
||||
imagePullPolicy: Never
|
||||
2
api/krusty/testdata/helmcharts/valuesFiles/file2.yaml
vendored
Normal file
2
api/krusty/testdata/helmcharts/valuesFiles/file2.yaml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
data:
|
||||
namespace: file-2
|
||||
Reference in New Issue
Block a user