From 47c8f48f209f363807c8fb9f17ef072d7ae32139 Mon Sep 17 00:00:00 2001 From: Thomas Dy Date: Fri, 20 Nov 2020 18:32:29 +0900 Subject: [PATCH] feat(helm): Support setting --kube-version Signed-off-by: Thomas Dy --- cmd/helm/template.go | 10 ++++++++++ pkg/action/install.go | 5 +++++ pkg/chartutil/capabilities.go | 14 ++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 1110771f0..e3c1d421f 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -52,6 +52,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { var skipTests bool client := action.NewInstall(cfg) valueOpts := &values.Options{} + var kubeVersion string var extraAPIs []string var showFiles []string @@ -64,6 +65,14 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return compInstall(args, toComplete, client) }, RunE: func(_ *cobra.Command, args []string) error { + if kubeVersion != "" { + parsedKubeVersion, err := chartutil.ParseKubeVersion(kubeVersion) + if err != nil { + return fmt.Errorf("invalid kube version '%s': %s", kubeVersion, err) + } + client.KubeVersion = parsedKubeVersion + } + client.DryRun = true client.ReleaseName = "RELEASE-NAME" client.Replace = true // Skip the name check @@ -171,6 +180,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.BoolVar(&includeCrds, "include-crds", false, "include CRDs in the templated output") f.BoolVar(&skipTests, "skip-tests", false, "skip tests from templated output") f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall") + f.StringVar(&kubeVersion, "kube-version", "", "Kubernetes version used for Capabilities.KubeVersion") f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.") bindPostRenderFlag(cmd, &client.PostRenderer) diff --git a/pkg/action/install.go b/pkg/action/install.go index 4de0b64e6..6fdd672b9 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -92,8 +92,10 @@ type Install struct { SubNotes bool DisableOpenAPIValidation bool IncludeCRDs bool + // KubeVersion allows specifying a custom kubernetes version to use and // APIVersions allows a manual set of supported API Versions to be passed // (for things like templating). These are ignored if ClientOnly is false + KubeVersion *chartutil.KubeVersion APIVersions chartutil.VersionSet // Used by helm template to render charts with .Release.IsUpgrade. Ignored if Dry-Run is false IsUpgrade bool @@ -197,6 +199,9 @@ 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 + if i.KubeVersion != nil { + i.cfg.Capabilities.KubeVersion = *i.KubeVersion + } i.cfg.Capabilities.APIVersions = append(i.cfg.Capabilities.APIVersions, i.APIVersions...) i.cfg.KubeClient = &kubefake.PrintingKubeClient{Out: ioutil.Discard} diff --git a/pkg/chartutil/capabilities.go b/pkg/chartutil/capabilities.go index c002e33f2..f9a454ad6 100644 --- a/pkg/chartutil/capabilities.go +++ b/pkg/chartutil/capabilities.go @@ -19,6 +19,7 @@ import ( "fmt" "strconv" + "github.com/Masterminds/semver/v3" "k8s.io/client-go/kubernetes/scheme" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -73,6 +74,19 @@ func (kv *KubeVersion) String() string { return kv.Version } // Deprecated: use KubeVersion.Version. func (kv *KubeVersion) GitVersion() string { return kv.Version } +// ParseKubeVersion parses kubernetes version from string +func ParseKubeVersion(version string) (*KubeVersion, error) { + sv, err := semver.NewVersion(version) + if err != nil { + return nil, err + } + return &KubeVersion{ + Version: sv.Original(), + Major: strconv.FormatUint(sv.Major(), 10), + Minor: strconv.FormatUint(sv.Minor(), 10), + }, nil +} + // VersionSet is a set of Kubernetes API versions. type VersionSet []string