From 59d3488d1c08d9317705a4cd9dfc518f30458f4e Mon Sep 17 00:00:00 2001 From: Taylor Thomas Date: Mon, 7 Oct 2019 13:23:42 -0600 Subject: [PATCH] feat(template): Ports api-versions flag to v3 This is a port of #5392. It also takes care of the small chore to update the default k8s version to 1.16, which is the latest supported version Signed-off-by: Taylor Thomas --- cmd/helm/template.go | 4 ++ cmd/helm/template_test.go | 5 ++ .../output/template-name-template.txt | 4 +- cmd/helm/testdata/output/template-set.txt | 4 +- .../testdata/output/template-values-files.txt | 4 +- .../output/template-with-api-version.txt | 56 +++++++++++++++++++ cmd/helm/testdata/output/template.txt | 4 +- pkg/action/install.go | 6 ++ pkg/chartutil/capabilities.go | 4 +- pkg/chartutil/capabilities_test.go | 16 +++--- .../charts/subchart1/templates/service.yaml | 3 + 11 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 cmd/helm/testdata/output/template-with-api-version.txt diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 4556112a1..da1dd816a 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -25,6 +25,7 @@ import ( "helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/cli/values" ) @@ -40,6 +41,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { var validate bool client := action.NewInstall(cfg) valueOpts := &values.Options{} + var extraAPIs []string cmd := &cobra.Command{ Use: "template [NAME] [CHART]", @@ -51,6 +53,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.ReleaseName = "RELEASE-NAME" client.Replace = true // Skip the name check client.ClientOnly = !validate + client.APIVersions = chartutil.VersionSet(extraAPIs) rel, err := runInstall(args, client, valueOpts, out) if err != nil { return err @@ -70,6 +73,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { addInstallFlags(f, client, valueOpts) f.StringVar(&client.OutputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout") f.BoolVar(&validate, "validate", false, "establish a connection to Kubernetes for schema validation") + f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") return cmd } diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index d4131acd3..8aa2f6c06 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -74,6 +74,11 @@ func TestTemplateCmd(t *testing.T) { cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/chart-with-template-lib-archive-dep"), golden: "output/template-chart-with-template-lib-archive-dep.txt", }, + { + name: "check kube api versions", + cmd: fmt.Sprintf("template --api-versions helm.k8s.io/test '%s'", chartPath), + golden: "output/template-with-api-version.txt", + }, } runTestCmd(t, tests) } diff --git a/cmd/helm/testdata/output/template-name-template.txt b/cmd/helm/testdata/output/template-name-template.txt index 88408e57a..acba50360 100644 --- a/cmd/helm/testdata/output/template-name-template.txt +++ b/cmd/helm/testdata/output/template-name-template.txt @@ -42,8 +42,8 @@ metadata: helm.sh/chart: "subchart1-0.1.0" app.kubernetes.io/instance: "foobar-YWJj-baz" kube-version/major: "1" - kube-version/minor: "14" - kube-version/version: "v1.14.0" + kube-version/minor: "16" + kube-version/version: "v1.16.0" spec: type: ClusterIP ports: diff --git a/cmd/helm/testdata/output/template-set.txt b/cmd/helm/testdata/output/template-set.txt index 2bbeb5e0d..b0924b5b6 100644 --- a/cmd/helm/testdata/output/template-set.txt +++ b/cmd/helm/testdata/output/template-set.txt @@ -42,8 +42,8 @@ metadata: helm.sh/chart: "subchart1-0.1.0" app.kubernetes.io/instance: "RELEASE-NAME" kube-version/major: "1" - kube-version/minor: "14" - kube-version/version: "v1.14.0" + kube-version/minor: "16" + kube-version/version: "v1.16.0" spec: type: ClusterIP ports: diff --git a/cmd/helm/testdata/output/template-values-files.txt b/cmd/helm/testdata/output/template-values-files.txt index 2bbeb5e0d..b0924b5b6 100644 --- a/cmd/helm/testdata/output/template-values-files.txt +++ b/cmd/helm/testdata/output/template-values-files.txt @@ -42,8 +42,8 @@ metadata: helm.sh/chart: "subchart1-0.1.0" app.kubernetes.io/instance: "RELEASE-NAME" kube-version/major: "1" - kube-version/minor: "14" - kube-version/version: "v1.14.0" + kube-version/minor: "16" + kube-version/version: "v1.16.0" spec: type: ClusterIP ports: diff --git a/cmd/helm/testdata/output/template-with-api-version.txt b/cmd/helm/testdata/output/template-with-api-version.txt new file mode 100644 index 000000000..da3559082 --- /dev/null +++ b/cmd/helm/testdata/output/template-with-api-version.txt @@ -0,0 +1,56 @@ +--- +# Source: subchart1/charts/subcharta/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: subcharta + labels: + helm.sh/chart: "subcharta-0.1.0" +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 + protocol: TCP + name: apache + selector: + app.kubernetes.io/name: subcharta +--- +# Source: subchart1/charts/subchartb/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: subchartb + labels: + helm.sh/chart: "subchartb-0.1.0" +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 + protocol: TCP + name: nginx + selector: + app.kubernetes.io/name: subchartb +--- +# Source: subchart1/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: subchart1 + labels: + helm.sh/chart: "subchart1-0.1.0" + app.kubernetes.io/instance: "RELEASE-NAME" + kube-version/major: "1" + kube-version/minor: "16" + kube-version/version: "v1.16.0" + kube-api-version/test: v1 +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 + protocol: TCP + name: nginx + selector: + app.kubernetes.io/name: subchart1 diff --git a/cmd/helm/testdata/output/template.txt b/cmd/helm/testdata/output/template.txt index a8919534c..080be618c 100644 --- a/cmd/helm/testdata/output/template.txt +++ b/cmd/helm/testdata/output/template.txt @@ -42,8 +42,8 @@ metadata: helm.sh/chart: "subchart1-0.1.0" app.kubernetes.io/instance: "RELEASE-NAME" kube-version/major: "1" - kube-version/minor: "14" - kube-version/version: "v1.14.0" + kube-version/minor: "16" + kube-version/version: "v1.16.0" spec: type: ClusterIP ports: diff --git a/pkg/action/install.go b/pkg/action/install.go index 0badcdb58..7b47062ee 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -84,6 +84,9 @@ type Install struct { SkipCRDs bool OutputFormat string SubNotes bool + // APIVersions allows a manual set of supported API Versions to be passed + // (for things like templating). These are ignored if ClientOnly is false + APIVersions chartutil.VersionSet } // ChartPathOptions captures common options used for controlling chart paths @@ -175,8 +178,11 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. // Add mock objects in here so it doesn't use Kube API server // NOTE(bacongobbler): used for `helm template` i.cfg.Capabilities = chartutil.DefaultCapabilities + i.cfg.Capabilities.APIVersions = append(i.cfg.Capabilities.APIVersions, i.APIVersions...) i.cfg.KubeClient = &kubefake.PrintingKubeClient{Out: ioutil.Discard} i.cfg.Releases = storage.Init(driver.NewMemory()) + } else if !i.ClientOnly && len(i.APIVersions) > 0 { + i.cfg.Log("API Version list given outside of client only mode, this list will be ignored") } if err := chartutil.ProcessDependencies(chrt, vals); err != nil { diff --git a/pkg/chartutil/capabilities.go b/pkg/chartutil/capabilities.go index a211fbdb3..9d29c4d9a 100644 --- a/pkg/chartutil/capabilities.go +++ b/pkg/chartutil/capabilities.go @@ -26,9 +26,9 @@ var ( // DefaultCapabilities is the default set of capabilities. DefaultCapabilities = &Capabilities{ KubeVersion: KubeVersion{ - Version: "v1.14.0", + Version: "v1.16.0", Major: "1", - Minor: "14", + Minor: "16", }, APIVersions: DefaultVersionSet, } diff --git a/pkg/chartutil/capabilities_test.go b/pkg/chartutil/capabilities_test.go index 88dc1bd83..3eeac76e2 100644 --- a/pkg/chartutil/capabilities_test.go +++ b/pkg/chartutil/capabilities_test.go @@ -42,19 +42,19 @@ func TestDefaultVersionSet(t *testing.T) { func TestDefaultCapabilities(t *testing.T) { kv := DefaultCapabilities.KubeVersion - if kv.String() != "v1.14.0" { - t.Errorf("Expected default KubeVersion.String() to be v1.14.0, got %q", kv.String()) + if kv.String() != "v1.16.0" { + t.Errorf("Expected default KubeVersion.String() to be v1.16.0, got %q", kv.String()) } - if kv.Version != "v1.14.0" { - t.Errorf("Expected default KubeVersion.Version to be v1.14.0, got %q", kv.Version) + if kv.Version != "v1.16.0" { + t.Errorf("Expected default KubeVersion.Version to be v1.16.0, got %q", kv.Version) } - if kv.GitVersion() != "v1.14.0" { - t.Errorf("Expected default KubeVersion.GitVersion() to be v1.14.0, got %q", kv.Version) + if kv.GitVersion() != "v1.16.0" { + t.Errorf("Expected default KubeVersion.GitVersion() to be v1.16.0, got %q", kv.Version) } if kv.Major != "1" { t.Errorf("Expected default KubeVersion.Major to be 1, got %q", kv.Major) } - if kv.Minor != "14" { - t.Errorf("Expected default KubeVersion.Minor to be 14, got %q", kv.Minor) + if kv.Minor != "16" { + t.Errorf("Expected default KubeVersion.Minor to be 16, got %q", kv.Minor) } } diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml index 967e1700f..fee94dced 100644 --- a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml @@ -8,6 +8,9 @@ metadata: kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" kube-version/version: "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: