move to 'config defaults'
parent
219abcc1f1
commit
899b43c323
|
@ -102,8 +102,9 @@ var settings = []Setting{
|
|||
validations: []setFn{IsValidPath},
|
||||
},
|
||||
{
|
||||
name: "kubernetes-version",
|
||||
set: SetString,
|
||||
name: "kubernetes-version",
|
||||
set: SetString,
|
||||
validDefaults: supportedKubernetesVersions,
|
||||
},
|
||||
{
|
||||
name: "iso-url",
|
||||
|
|
|
@ -21,32 +21,31 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v2"
|
||||
"k8s.io/minikube/pkg/minikube/exit"
|
||||
"k8s.io/minikube/pkg/minikube/out"
|
||||
"k8s.io/minikube/pkg/minikube/reason"
|
||||
)
|
||||
|
||||
var defaultsOutput string
|
||||
|
||||
var configDefaultsCommand = &cobra.Command{
|
||||
Use: "defaults PROPERTY_NAME",
|
||||
Short: "Lists all valid default values for PROPERTY_NAME",
|
||||
Long: `list displays all valid default settings for PROPERTY_NAME
|
||||
Acceptable fields: ` + "\n\n" + fieldsWithDefaults(),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
cmd.SilenceErrors = true
|
||||
return errors.New("not enough arguments.\nusage: minikube config list PROPERTY_NAME")
|
||||
exit.Message(reason.Usage, "usage: minikube config list PROPERTY_NAME")
|
||||
}
|
||||
if len(args) > 1 {
|
||||
cmd.SilenceErrors = true
|
||||
return fmt.Errorf("too many arguments (%d)\nusage: minikube config list PROPERTY_NAME", len(args))
|
||||
}
|
||||
|
||||
property := args[0]
|
||||
defaults, err := getDefaults(property)
|
||||
if err != nil {
|
||||
return err
|
||||
exit.Message(reason.Usage, "error getting defaults: {{.error}}", out.V{"error": err})
|
||||
}
|
||||
return printDefaults(defaults)
|
||||
printDefaults(defaults)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -61,19 +60,27 @@ func getDefaults(property string) ([]string, error) {
|
|||
return setting.validDefaults(), nil
|
||||
}
|
||||
|
||||
func printDefaults(defaults []string) error {
|
||||
if output == "json" {
|
||||
func printDefaults(defaults []string) {
|
||||
switch strings.ToLower(defaultsOutput) {
|
||||
case "":
|
||||
for _, d := range defaults {
|
||||
out.Ln("* %s", d)
|
||||
}
|
||||
case "json":
|
||||
encoding, err := json.Marshal(defaults)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "encoding json")
|
||||
exit.Error(reason.InternalJSONMarshal, "json encoding failure", err)
|
||||
}
|
||||
out.Ln(string(encoding))
|
||||
return nil
|
||||
case "yaml":
|
||||
encoding, err := yaml.Marshal(defaults)
|
||||
if err != nil {
|
||||
exit.Error(reason.InternalYamlMarshal, "yaml encoding failure", err)
|
||||
}
|
||||
out.Ln(string(encoding))
|
||||
default:
|
||||
exit.Message(reason.InternalOutputUsage, "error: --output must be 'yaml' or 'json'")
|
||||
}
|
||||
for _, d := range defaults {
|
||||
out.Ln("* %s", d)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fieldsWithDefaults() string {
|
||||
|
@ -87,6 +94,6 @@ func fieldsWithDefaults() string {
|
|||
}
|
||||
|
||||
func init() {
|
||||
configDefaultsCommand.Flags().StringVar(&output, "output", "", "Output format. Accepted values: [json]")
|
||||
configDefaultsCommand.Flags().StringVarP(&defaultsOutput, "output", "o", "", "Output format. Accepted values: [json, yaml]")
|
||||
ConfigCmd.AddCommand(configDefaultsCommand)
|
||||
}
|
||||
|
|
|
@ -77,12 +77,10 @@ func TestPrintDefaults(t *testing.T) {
|
|||
}
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
output = tc.format
|
||||
defaultsOutput = tc.format
|
||||
f := tests.NewFakeFile()
|
||||
out.SetOutFile(f)
|
||||
if err := printDefaults(defaults); err != nil {
|
||||
t.Fatalf("error printing defaults: %v", err)
|
||||
}
|
||||
printDefaults(defaults)
|
||||
if f.String() != tc.expected {
|
||||
t.Fatalf("Expected: %v\n Actual: %v\n", tc.expected, f.String())
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
Copyright 2022 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"github.com/google/go-github/v43/github"
|
||||
"golang.org/x/mod/semver"
|
||||
"k8s.io/minikube/pkg/minikube/constants"
|
||||
)
|
||||
|
||||
// supportedKubernetesVersions returns reverse-sort supported Kubernetes releases from GitHub that are in [constants.OldestKubernetesVersion, constants.NewestKubernetesVersion] range, excluding prereleases.
|
||||
// in case it cannot get it from GitHub, in addition to [constants.NewestKubernetesVersion, constants.OldestKubernetesVersion], 'constants.DefaultKubernetesVersion' is also returned if different from 'constants.NewestKubernetesVersion'.
|
||||
func supportedKubernetesVersions() (releases []string) {
|
||||
minver := constants.OldestKubernetesVersion
|
||||
defver := constants.DefaultKubernetesVersion
|
||||
maxver := constants.NewestKubernetesVersion
|
||||
|
||||
ghc := github.NewClient(nil)
|
||||
|
||||
opts := &github.ListOptions{PerPage: 100}
|
||||
for (opts.Page+1)*100 <= 300 {
|
||||
rls, resp, err := ghc.Repositories.ListReleases(context.Background(), "kubernetes", "kubernetes", opts)
|
||||
if err != nil {
|
||||
v := []string{maxver}
|
||||
if defver != maxver {
|
||||
v = append(v, defver)
|
||||
}
|
||||
v = append(v, minver)
|
||||
return v
|
||||
}
|
||||
for _, rl := range rls {
|
||||
ver := rl.GetTagName()
|
||||
if !semver.IsValid(ver) {
|
||||
continue
|
||||
}
|
||||
// skip pre-release versions
|
||||
if rl.GetPrerelease() {
|
||||
continue
|
||||
}
|
||||
// skip out-of-range versions
|
||||
if (minver != "" && semver.Compare(minver, ver) == 1) || (maxver != "" && semver.Compare(ver, maxver) == 1) {
|
||||
continue
|
||||
}
|
||||
releases = append(releases, ver)
|
||||
}
|
||||
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
|
||||
}
|
|
@ -41,7 +41,7 @@ import (
|
|||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
var output string
|
||||
var profileOutput string
|
||||
var isLight bool
|
||||
|
||||
var profileListCmd = &cobra.Command{
|
||||
|
@ -49,13 +49,13 @@ var profileListCmd = &cobra.Command{
|
|||
Short: "Lists all minikube profiles.",
|
||||
Long: "Lists all valid minikube profiles and detects all possible invalid profiles.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
switch strings.ToLower(output) {
|
||||
switch strings.ToLower(profileOutput) {
|
||||
case "json":
|
||||
printProfilesJSON()
|
||||
case "table":
|
||||
printProfilesTable()
|
||||
default:
|
||||
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'table', 'json'", output))
|
||||
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'table', 'json'", profileOutput))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -217,7 +217,7 @@ func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
|
|||
}
|
||||
|
||||
func init() {
|
||||
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
|
||||
profileListCmd.Flags().StringVarP(&profileOutput, "output", "o", "table", "The output format. One of 'json', 'table'")
|
||||
profileListCmd.Flags().BoolVarP(&isLight, "light", "l", false, "If true, returns list of profiles faster by skipping validating the status of the cluster.")
|
||||
ProfileCmd.AddCommand(profileListCmd)
|
||||
}
|
||||
|
|
|
@ -1585,10 +1585,10 @@ func validateKubernetesVersion(old *config.ClusterConfig) {
|
|||
exitIfNotForced(reason.KubernetesTooNew, "Kubernetes {{.version}} is not supported by this release of minikube", out.V{"version": nvs})
|
||||
}
|
||||
if nvs.GT(newestVersion) {
|
||||
out.WarningT("Specified Kubernetes version {{.specified}} is newer than the newest supported version: {{.newest}}. Use `minikube version --kubernetes` for details.", out.V{"specified": nvs, "newest": constants.NewestKubernetesVersion})
|
||||
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 nvs.LT(oldestVersion) {
|
||||
out.WarningT("Specified Kubernetes version {{.specified}} is less than the oldest supported version: {{.oldest}}. Use `minikube version --kubernetes` for details.", out.V{"specified": nvs, "oldest": constants.OldestKubernetesVersion})
|
||||
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})
|
||||
if !viper.GetBool(force) {
|
||||
out.WarningT("You can force an unsupported Kubernetes version via the --force flag")
|
||||
}
|
||||
|
|
|
@ -17,19 +17,14 @@ limitations under the License.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-github/v43/github"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/mod/semver"
|
||||
"gopkg.in/yaml.v2"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/minikube/pkg/minikube/constants"
|
||||
"k8s.io/minikube/pkg/minikube/exit"
|
||||
"k8s.io/minikube/pkg/minikube/mustload"
|
||||
"k8s.io/minikube/pkg/minikube/out"
|
||||
|
@ -41,7 +36,6 @@ var (
|
|||
versionOutput string
|
||||
shortVersion bool
|
||||
listComponentsVersions bool
|
||||
listKubernetesVersions bool
|
||||
)
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
|
@ -88,16 +82,6 @@ var versionCmd = &cobra.Command{
|
|||
|
||||
}
|
||||
|
||||
if listKubernetesVersions && !shortVersion {
|
||||
skv, err := supportedKubernetesVersions(constants.OldestKubernetesVersion, constants.NewestKubernetesVersion, true)
|
||||
if err != nil {
|
||||
klog.Warningf("Unable to get supported Kubernetes versions: {{.error}}", out.V{"error": err})
|
||||
data["supportedKubernetesVersions"] = []string{constants.OldestKubernetesVersion, constants.NewestKubernetesVersion}
|
||||
} else {
|
||||
data["supportedKubernetesVersions"] = skv
|
||||
}
|
||||
}
|
||||
|
||||
switch versionOutput {
|
||||
case "":
|
||||
if !shortVersion {
|
||||
|
@ -141,49 +125,8 @@ var versionCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
// supportedKubernetesVersions returns reverse-sort supported Kubernetes releases from GitHub that are in [minver, maxver] range, optionally including prereleases, and any error occurred.
|
||||
func supportedKubernetesVersions(minver, maxver string, prereleases bool) (releases []string, err error) {
|
||||
ghc := github.NewClient(nil)
|
||||
|
||||
if (minver != "" && !semver.IsValid(minver)) || (maxver != "" && !semver.IsValid(maxver)) {
|
||||
return nil, fmt.Errorf("invalid release version(s) semver format: %q or %q", minver, maxver)
|
||||
}
|
||||
if minver != "" && maxver != "" && semver.Compare(minver, maxver) == 1 {
|
||||
return nil, fmt.Errorf("invalid release versions range: min(%s) > max(%s)", minver, maxver)
|
||||
}
|
||||
|
||||
opts := &github.ListOptions{PerPage: 100}
|
||||
for (opts.Page+1)*100 <= 300 {
|
||||
rls, resp, err := ghc.Repositories.ListReleases(context.Background(), "kubernetes", "kubernetes", opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, rl := range rls {
|
||||
ver := rl.GetTagName()
|
||||
if !semver.IsValid(ver) {
|
||||
continue
|
||||
}
|
||||
if !prereleases && rl.GetPrerelease() {
|
||||
continue
|
||||
}
|
||||
// skip out-of-range versions
|
||||
if (minver != "" && semver.Compare(minver, ver) == 1) || (maxver != "" && semver.Compare(ver, maxver) == 1) {
|
||||
continue
|
||||
}
|
||||
releases = append(releases, ver)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func init() {
|
||||
versionCmd.Flags().StringVarP(&versionOutput, "output", "o", "", "One of 'yaml' or 'json'.")
|
||||
versionCmd.Flags().BoolVar(&shortVersion, "short", false, "Print just the version number.")
|
||||
versionCmd.Flags().BoolVar(&listComponentsVersions, "components", false, "list versions of all components included with minikube. (the cluster must be running)")
|
||||
versionCmd.Flags().BoolVar(&listKubernetesVersions, "kubernetes", false, "list all Kubernetes versions supported by this minikube version.")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue