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

--kubernetes-version add support for checking github k8s versions and exit nicely on error #16865

Merged
merged 14 commits into from
Aug 17, 2023
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
30 changes: 30 additions & 0 deletions cmd/minikube/cmd/config/kubernetes_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package config

import (
"context"
"sort"

"github.com/google/go-github/github"
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
"golang.org/x/mod/semver"
"k8s.io/minikube/pkg/minikube/constants"
)
Expand All @@ -34,3 +38,29 @@ func supportedKubernetesVersions() (releases []string) {
}
return releases
}

// GetGithubKubernetesVersions returns reverse-sort Kubernetes releases
func GetGithubKubernetesVersions(ver string) ([]string, error) {
ghc := github.NewClient(nil)

opts := &github.ListOptions{PerPage: 100}
var releases []string
for {
rls, resp, err := ghc.Repositories.ListReleases(context.Background(), "kubernetes", "kubernetes", opts)
if err != nil {
return nil, err
}
for _, rl := range rls {
tag := rl.GetTagName()
if semver.IsValid(tag) {
releases = append(releases, tag)
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
sort.Slice(releases, func(i, j int) bool { return semver.Compare(releases[i], releases[j]) == -1 })
return releases, nil
}
26 changes: 26 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,21 @@ func validateKubernetesVersion(old *config.ClusterConfig) {
}
if nvs.GT(newestVersion) {
out.WarningT("Specified Kubernetes version {{.specified}} is newer than the newest supported version: {{.newest}}. Use `minikube config defaults kubernetes-version` for details.", out.V{"specified": nvs, "newest": constants.NewestKubernetesVersion})
if contains(constants.ValidKubernetesVersions, kubernetesVer) {
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
out.Styled(style.Check, "Kubernetes version {{.specified}} found in version list", out.V{"specified": nvs})
} else {
out.WarningT("Specified Kubernetes version {{.specified}} not found in Kubernetes version list. Searching the internet...", out.V{"specified": nvs})
k8sVersions, err := cmdcfg.GetGithubKubernetesVersions(kubernetesVer)
if err != nil && !viper.GetBool(force) {
exit.Error(reason.KubernetesNotConnect, "error fetching Kubernetes version list from Github", err)
}
if k8sVersions != nil && contains(k8sVersions, kubernetesVer) {
out.Styled(style.Check, "Kubernetes version {{.specified}} found in Github version list", out.V{"specified": nvs})
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
} else if !viper.GetBool(force) {
out.WarningT("Kubernetes version not found in Github version list. You can force a Kubernetes version via the --force flag")
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
exitIfNotForced(reason.KubernetesTooNew, "Kubernetes {{.version}} is not supported by this release of minikube", out.V{"version": nvs})
}
}
}
if nvs.LT(oldestVersion) {
out.WarningT("Specified Kubernetes version {{.specified}} is less than the oldest supported version: {{.oldest}}. Use `minikube config defaults kubernetes-version` for details.", out.V{"specified": nvs, "oldest": constants.OldestKubernetesVersion})
Expand Down Expand Up @@ -2007,3 +2022,14 @@ func isTwoDigitSemver(ver string) bool {
majorMinorOnly := regexp.MustCompile(`^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)$`)
return majorMinorOnly.MatchString(ver)
}

// contains checks whether the parameter slice contains the parameter string
func contains(sl []string, s string) bool {
for _, k := range sl {
if s == k {
return true
}

}
return false
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/docker/cli v24.0.2+incompatible
github.com/docker/go-connections v0.4.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v53 v53.2.0
github.com/juju/clock v1.0.3
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/google/go-containerregistry v0.15.2 h1:MMkSh+tjSdnmJZO7ljvEqV1DjfekB6VUEAZgy3a+TQE=
github.com/google/go-containerregistry v0.15.2/go.mod h1:wWK+LnOv4jXMM23IT/F1wdYftGWGr47Is8CG+pmHK1Q=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v53 v53.2.0 h1:wvz3FyF53v4BK+AsnvCmeNhf8AkTaeh2SoYu/XUvTtI=
github.com/google/go-github/v53 v53.2.0/go.mod h1:XhFRObz+m/l+UCm9b7KSIC3lT3NWSXGt7mOsAWEloao=
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ var (
KubernetesTooOld = Kind{ID: "K8S_OLD_UNSUPPORTED", ExitCode: ExControlPlaneUnsupported}
// a too new Kubernetes version was specified for minikube to use
KubernetesTooNew = Kind{ID: "K8S_NEW_UNSUPPORTED", ExitCode: ExControlPlaneUnsupported}
// error fetching github kubernetes version list
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
KubernetesNotConnect = Kind{ID: "K8S_FAIL_CONNECT"}
JudahNour marked this conversation as resolved.
Show resolved Hide resolved
// minikube was unable to safely downgrade installed Kubernetes version
KubernetesDowngrade = Kind{
ID: "K8S_DOWNGRADE_UNSUPPORTED",
Expand Down