Skip to content

Commit 02cee02

Browse files
author
priyawadhwa
authored
Merge pull request #8143 from priyawadhwa/list
Add defaults subcommand to `minikube config`
2 parents 9a03195 + bd08d04 commit 02cee02

File tree

5 files changed

+232
-9
lines changed

5 files changed

+232
-9
lines changed

cmd/minikube/cmd/config/config.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/golang/glog"
2323
"github.com/spf13/cobra"
2424
"k8s.io/minikube/pkg/minikube/config"
25+
"k8s.io/minikube/pkg/minikube/driver"
2526
"k8s.io/minikube/pkg/minikube/localpath"
2627
)
2728

@@ -32,21 +33,23 @@ type setFn func(string, string) error
3233

3334
// Setting represents a setting
3435
type Setting struct {
35-
name string
36-
set func(config.MinikubeConfig, string, string) error
37-
setMap func(config.MinikubeConfig, string, map[string]interface{}) error
38-
validations []setFn
39-
callbacks []setFn
36+
name string
37+
set func(config.MinikubeConfig, string, string) error
38+
setMap func(config.MinikubeConfig, string, map[string]interface{}) error
39+
validDefaults func() []string
40+
validations []setFn
41+
callbacks []setFn
4042
}
4143

4244
// These are all the settings that are configurable
4345
// and their validation and callback fn run on Set
4446
var settings = []Setting{
4547
{
46-
name: "driver",
47-
set: SetString,
48-
validations: []setFn{IsValidDriver},
49-
callbacks: []setFn{RequiresRestartMsg},
48+
name: "driver",
49+
set: SetString,
50+
validDefaults: driver.SupportedDrivers,
51+
validations: []setFn{IsValidDriver},
52+
callbacks: []setFn{RequiresRestartMsg},
5053
},
5154
{
5255
name: "vm-driver",

cmd/minikube/cmd/config/defaults.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"strings"
23+
24+
"github.com/pkg/errors"
25+
"github.com/spf13/cobra"
26+
"k8s.io/minikube/pkg/minikube/out"
27+
)
28+
29+
var configDefaultsCommand = &cobra.Command{
30+
Use: "defaults PROPERTY_NAME",
31+
Short: "Lists all valid default values for PROPERTY_NAME",
32+
Long: `list displays all valid default settings for PROPERTY_NAME
33+
Acceptable fields: ` + "\n\n" + fieldsWithDefaults(),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
if len(args) == 0 {
36+
cmd.SilenceErrors = true
37+
return errors.New("not enough arguments.\nusage: minikube config list PROPERTY_NAME")
38+
}
39+
if len(args) > 1 {
40+
cmd.SilenceErrors = true
41+
return fmt.Errorf("too many arguments (%d)\nusage: minikube config list PROPERTY_NAME", len(args))
42+
}
43+
44+
property := args[0]
45+
defaults, err := getDefaults(property)
46+
if err != nil {
47+
return err
48+
}
49+
return printDefaults(defaults)
50+
},
51+
}
52+
53+
func getDefaults(property string) ([]string, error) {
54+
setting, err := findSetting(property)
55+
if err != nil {
56+
return nil, err
57+
}
58+
if setting.validDefaults == nil {
59+
return nil, fmt.Errorf("%s is not a valid option for the `defaults` command; to see valid options run `minikube config defaults -h`", property)
60+
}
61+
return setting.validDefaults(), nil
62+
}
63+
64+
func printDefaults(defaults []string) error {
65+
if output == "json" {
66+
encoding, err := json.Marshal(defaults)
67+
if err != nil {
68+
return errors.Wrap(err, "encoding json")
69+
}
70+
out.Ln(string(encoding))
71+
return nil
72+
}
73+
for _, d := range defaults {
74+
out.Ln("* %s", d)
75+
}
76+
return nil
77+
}
78+
79+
func fieldsWithDefaults() string {
80+
fields := []string{}
81+
for _, s := range settings {
82+
if s.validDefaults != nil {
83+
fields = append(fields, " * "+s.name)
84+
}
85+
}
86+
return strings.Join(fields, "\n")
87+
}
88+
89+
func init() {
90+
configDefaultsCommand.Flags().StringVar(&output, "output", "", "Output format. Accepted values: [json]")
91+
ConfigCmd.AddCommand(configDefaultsCommand)
92+
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/minikube/pkg/minikube/out"
23+
"k8s.io/minikube/pkg/minikube/tests"
24+
)
25+
26+
func TestGetDefaults(t *testing.T) {
27+
tcs := []struct {
28+
property string
29+
expectedContents string
30+
shouldErr bool
31+
}{
32+
{
33+
property: "driver",
34+
expectedContents: "docker",
35+
}, {
36+
property: "invalid",
37+
shouldErr: true,
38+
},
39+
}
40+
for _, tc := range tcs {
41+
t.Run(tc.property, func(t *testing.T) {
42+
defaults, err := getDefaults(tc.property)
43+
if err != nil && !tc.shouldErr {
44+
t.Fatalf("test shouldn't have failed, error listing defaults: %v", err)
45+
}
46+
if err == nil && tc.shouldErr {
47+
t.Fatal("test should have failed but did not")
48+
}
49+
if tc.shouldErr {
50+
return
51+
}
52+
for _, d := range defaults {
53+
if d == tc.expectedContents {
54+
return
55+
}
56+
}
57+
t.Fatalf("defaults didn't contain expected default. Actual: %v\nExpected: %v\n", defaults, tc.expectedContents)
58+
})
59+
}
60+
}
61+
62+
func TestPrintDefaults(t *testing.T) {
63+
defaults := []string{"a", "b", "c"}
64+
tcs := []struct {
65+
description string
66+
format string
67+
expected string
68+
}{
69+
{
70+
description: "print to stdout",
71+
expected: "* a\n* b\n* c\n",
72+
}, {
73+
description: "print in json",
74+
format: "json",
75+
expected: "[\"a\",\"b\",\"c\"]\n",
76+
},
77+
}
78+
for _, tc := range tcs {
79+
t.Run(tc.description, func(t *testing.T) {
80+
output = tc.format
81+
f := tests.NewFakeFile()
82+
out.SetOutFile(f)
83+
if err := printDefaults(defaults); err != nil {
84+
t.Fatalf("error printing defaults: %v", err)
85+
}
86+
if f.String() != tc.expected {
87+
t.Fatalf("Expected: %v\n Actual: %v\n", tc.expected, f.String())
88+
}
89+
})
90+
}
91+
}

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de
405405
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
406406
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
407407
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
408+
github.com/grpc-ecosystem/grpc-gateway v1.9.5 h1:UImYN5qQ8tuGpGE16ZmjvcTtTw24zw1QAp/SlnNrZhI=
408409
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
409410
github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
410411
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=

site/content/en/docs/commands/config.md

+36
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ minikube config SUBCOMMAND [flags]
6868
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
6969
```
7070

71+
## minikube config defaults
72+
73+
Lists all valid default values for PROPERTY_NAME
74+
75+
### Synopsis
76+
77+
list displays all valid default settings for PROPERTY_NAME
78+
Acceptable fields:
79+
80+
* driver
81+
82+
```
83+
minikube config defaults PROPERTY_NAME [flags]
84+
```
85+
86+
### Options
87+
88+
```
89+
-h, --help help for defaults
90+
--output string Output format. Accepted values: [json]
91+
```
92+
93+
### Options inherited from parent commands
94+
95+
```
96+
--alsologtostderr log to standard error as well as files
97+
-b, --bootstrapper string The name of the cluster bootstrapper that will set up the Kubernetes cluster. (default "kubeadm")
98+
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
99+
--log_dir string If non-empty, write log files in this directory
100+
--logtostderr log to standard error instead of files
101+
-p, --profile string The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently. (default "minikube")
102+
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
103+
-v, --v Level log level for V logs
104+
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
105+
```
106+
71107
## minikube config get
72108

73109
Gets the value of PROPERTY_NAME from the minikube config file

0 commit comments

Comments
 (0)