diff --git a/internal/config/config.go b/internal/config/config.go index 7ea61e6b9e..96d5aa5e0d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -506,10 +506,6 @@ func (c *Config) Validate(force bool) error { return err } - if err := validate.RegisterTranslation("supported_k8s_version", trans, registerInvalidK8sVersionError, translateInvalidK8sVersionError); err != nil { - return err - } - if err := validate.RegisterValidation("no_placeholders", validateNoPlaceholder); err != nil { return err } diff --git a/internal/config/validation.go b/internal/config/validation.go index 061af45e82..8ff1c477bb 100644 --- a/internal/config/validation.go +++ b/internal/config/validation.go @@ -55,22 +55,33 @@ func translateInvalidK8sVersionError(ut ut.Translator, fe validator.FieldError) validVersionsSorted := semver.ByVersion(validVersions) sort.Sort(validVersionsSorted) - errorMsg := fmt.Sprintf("Supported versions: %s", strings.Join(validVersionsSorted, " ")) - configured, ok := fe.Value().(string) - if !ok { - errorMsg = "The configured version is not a valid string" + if len(validVersionsSorted) == 0 { + t, _ := ut.T("supported_k8s_version", fe.Field(), "No valid versions available. This should never happen") + return t } - maxVersion := validVersionsSorted[len(validVersionsSorted)-1] minVersion := validVersionsSorted[0] - if configured < minVersion { - errorMsg = fmt.Sprintf("The configured version %s is older than the oldest version supported by this CLI: %s.", configured, minVersion) + var errorMsg string + configured, ok := fe.Value().(string) + if !ok { + errorMsg = fmt.Sprintf("The configured version is not a valid string. Supported versions: %s", strings.Join(validVersionsSorted, " ")) + t, _ := ut.T("supported_k8s_version", fe.Field(), errorMsg) + return t } - if configured > maxVersion { - errorMsg = fmt.Sprintf("The configured version %s is newer than the newest version supported by this CLI: %s.", configured, maxVersion) + + configured = compatibility.EnsurePrefixV(configured) + switch { + case !semver.IsValid(configured): + errorMsg = "The configured version is not a valid semantic version" + case semver.Compare(configured, minVersion) == -1: + errorMsg = fmt.Sprintf("The configured version %s is older than the oldest version supported by this CLI: %s", configured, minVersion) + case semver.Compare(configured, maxVersion) == 1: + errorMsg = fmt.Sprintf("The configured version %s is newer than the newest version supported by this CLI: %s", configured, maxVersion) } + errorMsg = errorMsg + fmt.Sprintf("Supported versions: %s", strings.Join(validVersionsSorted, " ")) + t, _ := ut.T("supported_k8s_version", fe.Field(), errorMsg) return t @@ -299,7 +310,7 @@ func getPlaceholderEntries(m Measurements) []uint32 { } func validateK8sVersion(fl validator.FieldLevel) bool { - return versions.IsSupportedK8sVersion(fl.Field().String()) + return versions.IsSupportedK8sVersion(compatibility.EnsurePrefixV(fl.Field().String())) } func registerVersionCompatibilityError(ut ut.Translator) error { diff --git a/internal/versions/versions.go b/internal/versions/versions.go index cbfedb4bcc..d6fa2ed633 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -16,6 +16,7 @@ import ( "sort" "strings" + "github.com/edgelesssys/constellation/v2/internal/compatibility" "github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/versions/components" "golang.org/x/mod/semver" @@ -26,7 +27,7 @@ func SupportedK8sVersions() []string { validVersions := make([]string, len(VersionConfigs)) i := 0 for _, conf := range VersionConfigs { - validVersions[i] = conf.ClusterVersion + validVersions[i] = compatibility.EnsurePrefixV(conf.ClusterVersion) i++ } validVersionsSorted := semver.ByVersion(validVersions)