Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/v1.3] Use semver constraints on Kubernetes version for upper and lower bounds #1817

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pkg/apis/kubeone/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package validation
import (
"bytes"
"crypto/x509"
"fmt"
"net"
"reflect"
"strings"
Expand All @@ -30,6 +31,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.22"
)

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 +196,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 +539,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
}
15 changes: 11 additions & 4 deletions pkg/apis/kubeone/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,16 +679,16 @@ func TestValidateVersionConfig(t *testing.T) {
expectedError: false,
},
{
name: "not supported kubernetes version (1.18.19)",
name: "not supported kubernetes version (1.24.0)",
versionConfig: kubeone.VersionConfig{
Kubernetes: "1.18.19",
Kubernetes: "1.24.0",
},
expectedError: true,
},
{
name: "not supported kubernetes version (1.18.0)",
name: "not supported kubernetes version (1.23.3)",
versionConfig: kubeone.VersionConfig{
Kubernetes: "1.18.0",
Kubernetes: "1.23.3",
},
expectedError: true,
},
Expand All @@ -699,6 +699,13 @@ func TestValidateVersionConfig(t *testing.T) {
},
expectedError: true,
},
{
name: "not supported kubernetes version (1.18.19)",
versionConfig: kubeone.VersionConfig{
Kubernetes: "1.18.19",
},
expectedError: true,
},
{
name: "invalid kubernetes version (2.0.0)",
versionConfig: kubeone.VersionConfig{
Expand Down