Commit 300aa76d authored by Bhargav Nookala's avatar Bhargav Nookala

Allow customization of API Versions when using Helm Template with --api-versions or -a

Signed-off-by: 's avatarBhargav Nookala <nooknb@gmail.com>
parent 5437dd4d
...@@ -75,6 +75,7 @@ type templateCmd struct { ...@@ -75,6 +75,7 @@ type templateCmd struct {
releaseIsUpgrade bool releaseIsUpgrade bool
renderFiles []string renderFiles []string
kubeVersion string kubeVersion string
apiVersions []string
outputDir string outputDir string
} }
...@@ -104,6 +105,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command { ...@@ -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.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.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.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") f.StringVar(&t.outputDir, "output-dir", "", "Writes the executed templates to files in output-dir instead of stdout")
return cmd return cmd
...@@ -167,6 +169,7 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { ...@@ -167,6 +169,7 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
Namespace: t.namespace, Namespace: t.namespace,
}, },
KubeVersion: t.kubeVersion, KubeVersion: t.kubeVersion,
APIVersions: t.apiVersions,
} }
renderedTemplates, err := renderutil.Render(c, config, renderOpts) renderedTemplates, err := renderutil.Render(c, config, renderOpts)
......
...@@ -175,6 +175,13 @@ func TestTemplateCmd(t *testing.T) { ...@@ -175,6 +175,13 @@ func TestTemplateCmd(t *testing.T) {
expectKey: "subchart1/templates/service.yaml", expectKey: "subchart1/templates/service.yaml",
expectValue: "kube-version/major: \"1\"\n kube-version/minor: \"6\"\n kube-version/gitversion: \"v1.6.0\"", 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 { for _, tt := range tests {
......
...@@ -24,19 +24,20 @@ helm template [flags] CHART ...@@ -24,19 +24,20 @@ helm template [flags] CHART
### Options ### Options
``` ```
-x, --execute stringArray Only execute the given templates -a, --api-versions stringArray Kubernetes api versions used for Capabilities.APIVersions
-h, --help help for template -x, --execute stringArray Only execute the given templates
--is-upgrade Set .Release.IsUpgrade instead of .Release.IsInstall -h, --help help for template
--kube-version string Kubernetes version used as Capabilities.KubeVersion.Major/Minor (default "1.14") --is-upgrade Set .Release.IsUpgrade instead of .Release.IsInstall
-n, --name string Release name (default "release-name") --kube-version string Kubernetes version used as Capabilities.KubeVersion.Major/Minor (default "1.14")
--name-template string Specify template used to name the release -n, --name string Release name (default "release-name")
--namespace string Namespace to install the release into --name-template string Specify template used to name the release
--notes Show the computed NOTES.txt file as well --namespace string Namespace to install the release into
--output-dir string Writes the executed templates to files in output-dir instead of stdout --notes Show the computed NOTES.txt file as well
--set stringArray Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) --output-dir string Writes the executed templates to files in output-dir instead of stdout
--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 stringArray Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--set-string stringArray Set STRING 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)
-f, --values valueFiles Specify values in a YAML file (can specify multiple) (default []) --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 ### Options inherited from parent commands
...@@ -55,4 +56,4 @@ helm template [flags] CHART ...@@ -55,4 +56,4 @@ helm template [flags] CHART
* [helm](helm.md) - The Helm package manager for Kubernetes. * [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
...@@ -11,6 +11,9 @@ metadata: ...@@ -11,6 +11,9 @@ metadata:
kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" kube-version/major: "{{ .Capabilities.KubeVersion.Major }}"
kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}"
kube-version/gitversion: "v{{ .Capabilities.KubeVersion.Major }}.{{ .Capabilities.KubeVersion.Minor }}.0" 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: spec:
type: {{ .Values.service.type }} type: {{ .Values.service.type }}
ports: ports:
......
...@@ -31,6 +31,7 @@ import ( ...@@ -31,6 +31,7 @@ import (
type Options struct { type Options struct {
ReleaseOptions chartutil.ReleaseOptions ReleaseOptions chartutil.ReleaseOptions
KubeVersion string KubeVersion string
APIVersions []string
} }
// Render chart templates locally and display the output. // 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 ...@@ -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()) 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) vals, err := chartutil.ToRenderValuesCaps(c, config, opts.ReleaseOptions, caps)
if err != nil { if err != nil {
return nil, err return nil, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment