Skip to content

Commit

Permalink
only allow major minor k8s version in config generate flag
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Sep 5, 2023
1 parent 86ecad3 commit 9c54bc0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
8 changes: 6 additions & 2 deletions cli/internal/cmd/configgenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -155,7 +159,7 @@ func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) {
}
return generateFlags{
pf: pathprefix.New(workDir),
k8sVersion: resolvedVersion,
k8sVersion: validK8sVersion,
attestationVariant: attestationVariant,
}, nil
}
Expand Down
6 changes: 5 additions & 1 deletion cli/internal/cmd/upgradecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 0 additions & 11 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 8 additions & 12 deletions internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down

0 comments on commit 9c54bc0

Please sign in to comment.