From 9c54bc012ac0bd2f6798825f66cee9f03c3a6c30 Mon Sep 17 00:00:00 2001 From: Adrian Stobbe Date: Mon, 28 Aug 2023 16:43:54 +0200 Subject: [PATCH] only allow major minor k8s version in config generate flag --- cli/internal/cmd/configgenerate.go | 8 ++++++-- cli/internal/cmd/upgradecheck.go | 6 +++++- internal/config/config_test.go | 11 ----------- internal/versions/versions.go | 20 ++++++++------------ 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cli/internal/cmd/configgenerate.go b/cli/internal/cmd/configgenerate.go index 5fe0f057b7..1e857af58f 100644 --- a/cli/internal/cmd/configgenerate.go +++ b/cli/internal/cmd/configgenerate.go @@ -133,7 +133,11 @@ func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) { if err != nil { return generateFlags{}, fmt.Errorf("parsing Kubernetes flag: %w", err) } - resolvedVersion, err := versions.NewValidK8sVersion(k8sVersion, true) + resolvedVersion, err := versions.ResolveK8sPatchVersion(k8sVersion) + if err != nil { + return generateFlags{}, fmt.Errorf("resolving kubernetes patch version from flag: %w", err) + } + validK8sVersion, err := versions.NewValidK8sVersion(resolvedVersion, true) if err != nil { return generateFlags{}, fmt.Errorf("resolving Kubernetes version from flag: %w", err) } @@ -155,7 +159,7 @@ func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) { } return generateFlags{ pf: pathprefix.New(workDir), - k8sVersion: resolvedVersion, + k8sVersion: validK8sVersion, attestationVariant: attestationVariant, }, nil } diff --git a/cli/internal/cmd/upgradecheck.go b/cli/internal/cmd/upgradecheck.go index 30e39a4bc8..8bb0844264 100644 --- a/cli/internal/cmd/upgradecheck.go +++ b/cli/internal/cmd/upgradecheck.go @@ -576,7 +576,11 @@ func (v *versionUpgrade) writeConfig(conf *config.Config, fileHandler file.Handl conf.MicroserviceVersion = v.newServices } if len(v.newKubernetes) > 0 { - conf.KubernetesVersion = versions.ValidK8sVersion(v.newKubernetes[0]) + var err error + conf.KubernetesVersion, err = versions.NewValidK8sVersion(v.newKubernetes[0], true) + if err != nil { + return fmt.Errorf("parsing Kubernetes version: %w", err) + } } if len(v.newImages) > 0 { imageUpgrade := sortedMapKeys(v.newImages)[0] diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 701fbce551..2e1534cdde 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -248,17 +248,6 @@ func TestFromFile(t *testing.T) { configName: "wrong-name.yaml", wantErr: true, }, - // TODO why do we support an empty file? - //"custom config from default file": { - // config: &Config{ - // Version: Version4, - // }, - // configName: constants.ConfigFilename, - // wantResult: &Config{ - // Version: Version4, - // NodeGroups: map[string]NodeGroup{}, - // }, - //}, "modify default config": { config: func() *Config { conf := Default() diff --git a/internal/versions/versions.go b/internal/versions/versions.go index dd184433d4..faefd04d69 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -48,27 +48,22 @@ func SupportedValidK8sVersions() (res []ValidK8sVersion) { type ValidK8sVersion string // NewValidK8sVersion validates the given string and produces a new ValidK8sVersion object. -// It accepts a major minor version (e.g. 1.26) and transforms it into a supported patch version (e.g. 1.26.7). -// It also accepts a full version (e.g. 1.26.7) and validates it. +// It accepts a full version (e.g. 1.26.7) and validates it. // Returns an empty string if the given version is invalid. // strict controls whether the patch version is checked or not. // If strict is false, the patch version validation is skipped. func NewValidK8sVersion(k8sVersion string, strict bool) (ValidK8sVersion, error) { prefixedVersion := compatibility.EnsurePrefixV(k8sVersion) - parsedVersion, err := resolveK8sPatchVersion(prefixedVersion) - if err != nil { - return "", fmt.Errorf("resolving kubernetes patch version from flag: %w", err) - } var supported bool if strict { - supported = isSupportedK8sVersionStrict(parsedVersion) + supported = isSupportedK8sVersionStrict(prefixedVersion) } else { - supported = isSupportedK8sVersion(parsedVersion) + supported = isSupportedK8sVersion(prefixedVersion) } if !supported { - return "", fmt.Errorf("invalid Kubernetes version: %s; supported versions are %v", parsedVersion, SupportedK8sVersions()) + return "", fmt.Errorf("invalid Kubernetes version: %s; supported versions are %v", prefixedVersion, SupportedK8sVersions()) } - return ValidK8sVersion(parsedVersion), nil + return ValidK8sVersion(prefixedVersion), nil } // hasPatchVersion returns if the given version has specified a patch version. @@ -90,9 +85,10 @@ func (v *ValidK8sVersion) UnmarshalYAML(unmarshal func(interface{}) error) error return nil } -// resolveK8sPatchVersion takes the user input from --kubernetes and transforms a MAJOR.MINOR definition into a supported +// ResolveK8sPatchVersion transforms a MAJOR.MINOR definition into a supported // MAJOR.MINOR.PATCH release. -func resolveK8sPatchVersion(k8sVersion string) (string, error) { +func ResolveK8sPatchVersion(k8sVersion string) (string, error) { + k8sVersion = compatibility.EnsurePrefixV(k8sVersion) if !semver.IsValid(k8sVersion) { return "", fmt.Errorf("Kubernetes version does not specify a valid semantic version: %s", k8sVersion) }