From 300aa76d8d55a04bf776aaaa4936352873d03c37 Mon Sep 17 00:00:00 2001 From: Bhargav Nookala Date: Fri, 1 Mar 2019 17:31:18 -0800 Subject: [PATCH] Allow customization of API Versions when using Helm Template with --api-versions or -a Signed-off-by: Bhargav Nookala --- cmd/helm/template.go | 3 ++ cmd/helm/template_test.go | 7 +++++ docs/helm/helm_template.md | 29 ++++++++++--------- .../charts/subchart1/templates/service.yaml | 3 ++ pkg/renderutil/render.go | 5 ++++ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index bc9fba9c1..24fe93b64 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -75,6 +75,7 @@ type templateCmd struct { releaseIsUpgrade bool renderFiles []string kubeVersion string + apiVersions []string outputDir string } @@ -104,6 +105,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command { f.StringArrayVar(&t.fileValues, "set-file", []string{}, "Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)") f.StringVar(&t.nameTemplate, "name-template", "", "Specify template used to name the release") f.StringVar(&t.kubeVersion, "kube-version", defaultKubeVersion, "Kubernetes version used as Capabilities.KubeVersion.Major/Minor") + f.StringArrayVarP(&t.apiVersions, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") f.StringVar(&t.outputDir, "output-dir", "", "Writes the executed templates to files in output-dir instead of stdout") return cmd @@ -167,6 +169,7 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { Namespace: t.namespace, }, KubeVersion: t.kubeVersion, + APIVersions: t.apiVersions, } renderedTemplates, err := renderutil.Render(c, config, renderOpts) diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index 10c836a0d..4821491a6 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -175,6 +175,13 @@ func TestTemplateCmd(t *testing.T) { expectKey: "subchart1/templates/service.yaml", expectValue: "kube-version/major: \"1\"\n kube-version/minor: \"6\"\n kube-version/gitversion: \"v1.6.0\"", }, + { + name: "check_kube_api_versions", + desc: "verify --api-versions overrides kubernetes api versions", + args: []string{subchart1ChartPath, "--api-versions", "helm.k8s.io/test"}, + expectKey: "subchart1/templates/service.yaml", + expectValue: "kube-api-version/test: v1", + }, } for _, tt := range tests { diff --git a/docs/helm/helm_template.md b/docs/helm/helm_template.md index 8f890e27d..504c54355 100644 --- a/docs/helm/helm_template.md +++ b/docs/helm/helm_template.md @@ -24,19 +24,20 @@ helm template [flags] CHART ### Options ``` - -x, --execute stringArray Only execute the given templates - -h, --help help for template - --is-upgrade Set .Release.IsUpgrade instead of .Release.IsInstall - --kube-version string Kubernetes version used as Capabilities.KubeVersion.Major/Minor (default "1.14") - -n, --name string Release name (default "release-name") - --name-template string Specify template used to name the release - --namespace string Namespace to install the release into - --notes Show the computed NOTES.txt file as well - --output-dir string Writes the executed templates to files in output-dir instead of stdout - --set stringArray Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) - --set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) - --set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) - -f, --values valueFiles Specify values in a YAML file (can specify multiple) (default []) + -a, --api-versions stringArray Kubernetes api versions used for Capabilities.APIVersions + -x, --execute stringArray Only execute the given templates + -h, --help help for template + --is-upgrade Set .Release.IsUpgrade instead of .Release.IsInstall + --kube-version string Kubernetes version used as Capabilities.KubeVersion.Major/Minor (default "1.14") + -n, --name string Release name (default "release-name") + --name-template string Specify template used to name the release + --namespace string Namespace to install the release into + --notes Show the computed NOTES.txt file as well + --output-dir string Writes the executed templates to files in output-dir instead of stdout + --set stringArray Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) + --set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) + --set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) + -f, --values valueFiles Specify values in a YAML file (can specify multiple) (default []) ``` ### Options inherited from parent commands @@ -55,4 +56,4 @@ helm template [flags] CHART * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 17-Jun-2019 +###### Auto generated by spf13/cobra on 3-Oct-2019 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml index e06d19b90..a67dc5b52 100644 --- a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml @@ -11,6 +11,9 @@ metadata: kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" kube-version/gitversion: "v{{ .Capabilities.KubeVersion.Major }}.{{ .Capabilities.KubeVersion.Minor }}.0" +{{ if .Capabilities.APIVersions.Has "helm.k8s.io/test" }} + kube-api-version/test: v1 +{{ end }} spec: type: {{ .Values.service.type }} ports: diff --git a/pkg/renderutil/render.go b/pkg/renderutil/render.go index 1996e1dc2..5be40992b 100644 --- a/pkg/renderutil/render.go +++ b/pkg/renderutil/render.go @@ -31,6 +31,7 @@ import ( type Options struct { ReleaseOptions chartutil.ReleaseOptions KubeVersion string + APIVersions []string } // Render chart templates locally and display the output. @@ -79,6 +80,10 @@ func Render(c *chart.Chart, config *chart.Config, opts Options) (map[string]stri caps.KubeVersion.GitVersion = fmt.Sprintf("v%d.%d.0", kv.Major(), kv.Minor()) } + if len(opts.APIVersions) > 0 { + caps.APIVersions = chartutil.NewVersionSet(append(opts.APIVersions, "v1")...) + } + vals, err := chartutil.ToRenderValuesCaps(c, config, opts.ReleaseOptions, caps) if err != nil { return nil, err