mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-10 07:50:08 -04:00
config: fix incorrect kubernetes version validation (#1155)
Fix incorrect string comparison by replacing it with call to semver.Compare. Also add handling to check for missing v prefix.
This commit is contained in:
parent
4c5ab7c5e9
commit
fd860ddb91
3 changed files with 23 additions and 15 deletions
|
@ -506,10 +506,6 @@ func (c *Config) Validate(force bool) error {
|
||||||
return err
|
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 {
|
if err := validate.RegisterValidation("no_placeholders", validateNoPlaceholder); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,22 +55,33 @@ func translateInvalidK8sVersionError(ut ut.Translator, fe validator.FieldError)
|
||||||
validVersionsSorted := semver.ByVersion(validVersions)
|
validVersionsSorted := semver.ByVersion(validVersions)
|
||||||
sort.Sort(validVersionsSorted)
|
sort.Sort(validVersionsSorted)
|
||||||
|
|
||||||
errorMsg := fmt.Sprintf("Supported versions: %s", strings.Join(validVersionsSorted, " "))
|
if len(validVersionsSorted) == 0 {
|
||||||
configured, ok := fe.Value().(string)
|
t, _ := ut.T("supported_k8s_version", fe.Field(), "No valid versions available. This should never happen")
|
||||||
if !ok {
|
return t
|
||||||
errorMsg = "The configured version is not a valid string"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maxVersion := validVersionsSorted[len(validVersionsSorted)-1]
|
maxVersion := validVersionsSorted[len(validVersionsSorted)-1]
|
||||||
minVersion := validVersionsSorted[0]
|
minVersion := validVersionsSorted[0]
|
||||||
|
|
||||||
if configured < minVersion {
|
var errorMsg string
|
||||||
errorMsg = fmt.Sprintf("The configured version %s is older than the oldest version supported by this CLI: %s.", configured, minVersion)
|
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)
|
t, _ := ut.T("supported_k8s_version", fe.Field(), errorMsg)
|
||||||
|
|
||||||
return t
|
return t
|
||||||
|
@ -299,7 +310,7 @@ func getPlaceholderEntries(m Measurements) []uint32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateK8sVersion(fl validator.FieldLevel) bool {
|
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 {
|
func registerVersionCompatibilityError(ut ut.Translator) error {
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
||||||
"golang.org/x/mod/semver"
|
"golang.org/x/mod/semver"
|
||||||
|
@ -26,7 +27,7 @@ func SupportedK8sVersions() []string {
|
||||||
validVersions := make([]string, len(VersionConfigs))
|
validVersions := make([]string, len(VersionConfigs))
|
||||||
i := 0
|
i := 0
|
||||||
for _, conf := range VersionConfigs {
|
for _, conf := range VersionConfigs {
|
||||||
validVersions[i] = conf.ClusterVersion
|
validVersions[i] = compatibility.EnsurePrefixV(conf.ClusterVersion)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
validVersionsSorted := semver.ByVersion(validVersions)
|
validVersionsSorted := semver.ByVersion(validVersions)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue