diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 88ca92fc9..92f6ed738 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -26,6 +26,7 @@ import ( "strings" "time" + "github.com/Masterminds/semver" "github.com/spf13/cobra" "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/engine" @@ -35,6 +36,7 @@ import ( util "k8s.io/helm/pkg/releaseutil" "k8s.io/helm/pkg/tiller" "k8s.io/helm/pkg/timeconv" + tversion "k8s.io/helm/pkg/version" ) const templateDesc = ` @@ -61,6 +63,7 @@ type templateCmd struct { showNotes bool releaseName string renderFiles []string + kubeVersion string } func newTemplateCmd(out io.Writer) *cobra.Command { @@ -84,6 +87,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command { f.StringVar(&t.namespace, "namespace", "", "namespace to install the release into") f.StringArrayVar(&t.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") f.StringVar(&t.nameTemplate, "name-template", "", "specify template used to name the release") + f.StringVar(&t.kubeVersion, "kube-version", "", "override the Kubernetes version used as Capabilities.KubeVersion.Major/Minor (e.g. 1.7)") return cmd } @@ -171,7 +175,22 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { // Set up engine. renderer := engine.New() - vals, err := chartutil.ToRenderValues(c, config, options) + caps := &chartutil.Capabilities{ + APIVersions: chartutil.DefaultVersionSet, + KubeVersion: chartutil.DefaultKubeVersion, + TillerVersion: tversion.GetVersionProto(), + } + // If --kube-versionis set, try to parse it as SemVer, and override the + // kubernetes version + if t.kubeVersion != "" { + kv, err := semver.NewVersion(t.kubeVersion) + if err != nil { + return fmt.Errorf("could not parse a kubernetes version: %v", err) + } + caps.KubeVersion.Major = fmt.Sprint(kv.Major()) + caps.KubeVersion.Minor = fmt.Sprint(kv.Minor()) + } + vals, err := chartutil.ToRenderValuesCaps(c, config, options, caps) if err != nil { return err } diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index b1e080493..06c7edf9a 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -104,6 +104,13 @@ func TestTemplateCmd(t *testing.T) { expectKey: "subchart1/templates/service.yaml", expectValue: "release-name: \"foobar-YWJj-baz\"", }, + { + name: "check_kube_version", + desc: "verify --kube-version overrides the kubernetes version", + args: []string{chartPath, "--kube-version", "1.6"}, + expectKey: "subchart1/templates/service.yaml", + expectValue: "kube-version/major: \"1\"\n kube-version/minor: \"6\"", + }, } var buf bytes.Buffer diff --git a/docs/helm/helm_template.md b/docs/helm/helm_template.md index 80ecdec39..adfffa269 100644 --- a/docs/helm/helm_template.md +++ b/docs/helm/helm_template.md @@ -26,6 +26,7 @@ helm template [flags] CHART ``` -x, --execute stringArray only execute the given templates + --kube-version string override the Kubernetes version used as Capabilities.KubeVersion.Major/Minor (e.g. 1.7) -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 @@ -47,4 +48,4 @@ helm template [flags] CHART ### SEE ALSO * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 24-Aug-2017 +###### Auto generated by spf13/cobra on 11-Sep-2017 diff --git a/pkg/chartutil/capabilities.go b/pkg/chartutil/capabilities.go index 82e5e0c0e..e415306d4 100644 --- a/pkg/chartutil/capabilities.go +++ b/pkg/chartutil/capabilities.go @@ -16,12 +16,26 @@ limitations under the License. package chartutil import ( + "fmt" + "runtime" + "k8s.io/apimachinery/pkg/version" tversion "k8s.io/helm/pkg/proto/hapi/version" ) -// DefaultVersionSet is the default version set, which includes only Core V1 ("v1"). -var DefaultVersionSet = NewVersionSet("v1") +var ( + // DefaultVersionSet is the default version set, which includes only Core V1 ("v1"). + DefaultVersionSet = NewVersionSet("v1") + + // DefaultKubeVersion is the default kubernetes version + DefaultKubeVersion = &version.Info{ + Major: "1", + Minor: "7", + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +) // Capabilities describes the capabilities of the Kubernetes cluster that Tiller is attached to. type Capabilities struct { diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml index bf7672e12..8b6bbaee7 100644 --- a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml @@ -6,6 +6,8 @@ metadata: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" namespace: "{{ .Release.Namespace }}" release-name: "{{ .Release.Name }}" + kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" + kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" spec: type: {{ .Values.service.type }} ports: diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 3ccd9acdb..9343a3a84 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -21,10 +21,8 @@ import ( "fmt" "os" "path/filepath" - "runtime" "github.com/ghodss/yaml" - "k8s.io/apimachinery/pkg/version" "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/engine" "k8s.io/helm/pkg/lint/support" @@ -55,14 +53,8 @@ func Templates(linter *support.Linter) { options := chartutil.ReleaseOptions{Name: "testRelease", Time: timeconv.Now(), Namespace: "testNamespace"} caps := &chartutil.Capabilities{ - APIVersions: chartutil.DefaultVersionSet, - KubeVersion: &version.Info{ - Major: "1", - Minor: "7", - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), - }, + APIVersions: chartutil.DefaultVersionSet, + KubeVersion: chartutil.DefaultKubeVersion, TillerVersion: tversion.GetVersionProto(), } valuesToRender, err := chartutil.ToRenderValuesCaps(chart, chart.Values, options, caps)