Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Make handling Kubernetes versions easier #2506

Merged
merged 7 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
121 changes: 0 additions & 121 deletions pkg/api/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"strings"

"github.com/Masterminds/semver"

validator "gopkg.in/go-playground/validator.v9"
)

Expand Down Expand Up @@ -53,122 +51,3 @@ func HandleValidationErrors(e validator.ValidationErrors) error {
}
return fmt.Errorf("Namespace %s is not caught, %+v", ns, e)
}

// GetSupportedVersions get supported version list for a certain orchestrator
func GetSupportedVersions(orchType string, hasWindows bool) (versions []string, defaultVersion string) {
switch orchType {
case Kubernetes:
if hasWindows {
return GetAllSupportedKubernetesVersionsWindows(), string(KubernetesDefaultVersion)
}
return GetAllSupportedKubernetesVersions(), string(KubernetesDefaultVersion)

case DCOS:
return AllDCOSSupportedVersions, DCOSDefaultVersion
default:
return nil, ""
}
}

//GetValidPatchVersion gets the current valid patch version for the minor version of the passed in version
func GetValidPatchVersion(orchType, orchVer string) string {
if orchVer == "" {
return RationalizeReleaseAndVersion(
orchType,
"",
"",
false)
}

// check if the current version is valid, this allows us to have multiple supported patch versions in the future if we need it
version := RationalizeReleaseAndVersion(
orchType,
"",
orchVer,
false)

if version == "" {
sv, err := semver.NewVersion(orchVer)
if err != nil {
return ""
}
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())

version = RationalizeReleaseAndVersion(
orchType,
sr,
"",
false)
}
return version
}

// RationalizeReleaseAndVersion return a version when it can be rationalized from the input, otherwise ""
func RationalizeReleaseAndVersion(orchType, orchRel, orchVer string, hasWindows bool) (version string) {
// ignore "v" prefix in orchestrator version and release: "v1.8.0" is equivalent to "1.8.0", "v1.9" is equivalent to "1.9"
orchVer = strings.TrimPrefix(orchVer, "v")
orchRel = strings.TrimPrefix(orchRel, "v")
supportedVersions, defaultVersion := GetSupportedVersions(orchType, hasWindows)
if supportedVersions == nil {
return ""
}

if orchRel == "" && orchVer == "" {
return defaultVersion
}

if orchVer == "" {
// Try to get latest version matching the release
version = GetLatestPatchVersion(orchRel, supportedVersions)
return version
} else if orchRel == "" {
// Try to get version the same with orchVer
version = ""
for _, ver := range supportedVersions {
if ver == orchVer {
version = ver
break
}
}
return version
}
// Try to get latest version matching the release
version = ""
for _, ver := range supportedVersions {
sv, _ := semver.NewVersion(ver)
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())
if sr == orchRel && ver == orchVer {
version = ver
break
}
}
return version
}

// IsKubernetesVersionGe returns if a semver string is >= to a compare-against semver string (suppport "-" suffixes)
func IsKubernetesVersionGe(actualVersion, version string) bool {
orchestratorVersion, _ := semver.NewVersion(strings.Split(actualVersion, "-")[0]) // to account for -alpha and -beta suffixes
constraint, _ := semver.NewConstraint(">=" + version)
return constraint.Check(orchestratorVersion)
}

// GetLatestPatchVersion gets the most recent patch version from a list of semver versions given a major.minor string
func GetLatestPatchVersion(majorMinor string, versionsList []string) (version string) {
// Try to get latest version matching the release
version = ""
for _, ver := range versionsList {
sv, _ := semver.NewVersion(ver)
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())
if sr == majorMinor {
if version == "" {
version = ver
} else {
cons, _ := semver.NewConstraint(">" + version)
if cons.Check(sv) {
version = ver
}
}
}
}
return version
}
85 changes: 0 additions & 85 deletions pkg/api/common/helper_test.go

This file was deleted.

Loading