Skip to content

Commit

Permalink
Use semver constraints on Kubernetes version for upper and lower boun…
Browse files Browse the repository at this point in the history
…ds (kubermatic#1808)

* Use semver constraints on Kubernetes version for upper and lower bounds

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>

* Fix linting issues

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>

* Improve constraint usage and correctly append errors

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>

* Use mustParseConstraint

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>

* Fix test case

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>

* Appease linter

Signed-off-by: Marvin Beckers <marvin@kubermatic.com>
  • Loading branch information
embik committed Feb 8, 2022
1 parent 518a842 commit 7d0e5ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
37 changes: 34 additions & 3 deletions pkg/apis/kubeone/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

const (
// lowerVersionConstraint defines a semver constraint that validates Kubernetes versions against a lower bound
lowerVersionConstraint = ">= 1.19"
// upperVersionConstraint defines a semver constraint that validates Kubernetes versions against an upper bound
upperVersionConstraint = "<= 1.23"
)

var (
lowerConstraint = mustParseConstraint(lowerVersionConstraint)
upperConstraint = mustParseConstraint(upperVersionConstraint)
)

// ValidateKubeOneCluster validates the KubeOneCluster object
func ValidateKubeOneCluster(c kubeone.KubeOneCluster) field.ErrorList {
allErrs := field.ErrorList{}
Expand Down Expand Up @@ -183,13 +195,23 @@ func ValidateVersionConfig(version kubeone.VersionConfig, fldPath *field.Path) f
allErrs = append(allErrs, field.Invalid(fldPath.Child("kubernetes"), version, ".versions.kubernetes is not a semver string"))
return allErrs
}
if v.Major() != 1 || v.Minor() < 19 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("kubernetes"), version, "kubernetes versions lower than 1.19 are not supported. You need to use an older KubeOne version to upgrade your cluster to v1.19. Please refer to the Compatibility section of docs for more details."))
}

if strings.HasPrefix(version.Kubernetes, "v") {
allErrs = append(allErrs, field.Invalid(fldPath.Child("kubernetes"), version, ".versions.kubernetes can't start with a leading 'v'"))
}

if valid, errs := lowerConstraint.Validate(v); !valid {
for _, err := range errs {
allErrs = append(allErrs, field.Invalid(fldPath.Child("kubernetes"), version, fmt.Sprintf("kubernetes version does not satisfy version constraint '%s': %s. You need to use an older KubeOne version to upgrade your cluster to a supported version. Please refer to the Compatibility section of docs for more details.", lowerVersionConstraint, err.Error())))
}
}

if valid, errs := upperConstraint.Validate(v); !valid {
for _, err := range errs {
allErrs = append(allErrs, field.Invalid(fldPath.Child("kubernetes"), version, fmt.Sprintf("kubernetes version does not satisfy version constraint '%s': %s. This version is not yet supported. Please refer to the Compatibility section of docs for more details.", upperVersionConstraint, err.Error())))
}
}

return allErrs
}

Expand Down Expand Up @@ -516,3 +538,12 @@ func ValidateAssetConfiguration(a *kubeone.AssetConfiguration, fldPath *field.Pa

return allErrs
}

func mustParseConstraint(constraint string) *semver.Constraints {
result, err := semver.NewConstraint(constraint)
if err != nil {
panic(err)
}

return result
}
14 changes: 14 additions & 0 deletions pkg/apis/kubeone/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ func TestValidateVersionConfig(t *testing.T) {
versionConfig kubeone.VersionConfig
expectedError bool
}{
{
name: "valid version config (1.23.1)",
versionConfig: kubeoneapi.VersionConfig{
Kubernetes: "1.23.1",
},
expectedError: false,
},
{
name: "valid version config (1.22.1)",
versionConfig: kubeone.VersionConfig{
Expand Down Expand Up @@ -678,6 +685,13 @@ func TestValidateVersionConfig(t *testing.T) {
},
expectedError: false,
},
{
name: "not supported kubernetes version (1.24.0)",
versionConfig: kubeoneapi.VersionConfig{
Kubernetes: "1.24.0",
},
expectedError: true,
},
{
name: "not supported kubernetes version (1.18.19)",
versionConfig: kubeone.VersionConfig{
Expand Down

0 comments on commit 7d0e5ab

Please sign in to comment.