Skip to content

Commit 1a26ae8

Browse files
committed
Start removing viper calls in minikube
Pass --interactive, --download-only, and --delete-on-failure via new run.Options struct so minikube code can stop accessing the flags via viper. There are many more viper calls in the minikube package. Removing all of them requires adding mode options and modifying more code to accept options argument. To pass options to driver we keep Options in drivers.common.CommonDriver. Options are not persisted so un-marshalling drivers from config.json use the default options.
1 parent 63d7b46 commit 1a26ae8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+260
-161
lines changed

cmd/minikube/cmd/config/profile_list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/minikube/pkg/minikube/notify"
3232
"k8s.io/minikube/pkg/minikube/out"
3333
"k8s.io/minikube/pkg/minikube/reason"
34+
"k8s.io/minikube/pkg/minikube/run"
3435
"k8s.io/minikube/pkg/minikube/style"
3536

3637
"github.com/docker/machine/libmachine"
@@ -54,7 +55,7 @@ var profileListCmd = &cobra.Command{
5455
Run: func(_ *cobra.Command, _ []string) {
5556
output := strings.ToLower(profileOutput)
5657
out.SetJSON(output == "json")
57-
go notify.MaybePrintUpdateTextFromGithub()
58+
go notify.MaybePrintUpdateTextFromGithub(run.Options{})
5859

5960
switch output {
6061
case "json":

cmd/minikube/cmd/config/set.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Set(name string, value string) error {
5656
return errors.Wrapf(err, "find settings for %q value of %q", name, value)
5757
}
5858
// Validate the new value
59-
err = run(name, value, s.validations)
59+
err = invoke(name, value, s.validations)
6060
if err != nil {
6161
return errors.Wrapf(err, "run validations for %q with value of %q", name, value)
6262
}
@@ -71,8 +71,8 @@ func Set(name string, value string) error {
7171
return errors.Wrapf(err, "set")
7272
}
7373

74-
// Run any callbacks for this property
75-
err = run(name, value, s.callbacks)
74+
// Invoke any callbacks for this property
75+
err = invoke(name, value, s.callbacks)
7676
if err != nil {
7777
return errors.Wrapf(err, "run callbacks for %q with value of %q", name, value)
7878
}

cmd/minikube/cmd/config/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"k8s.io/minikube/pkg/minikube/out"
2626
)
2727

28-
// Runs all the validation or callback functions and collects errors
29-
func run(name string, value string, fns []setFn) error {
28+
// Invoke all the validation or callback functions and collects errors
29+
func invoke(name string, value string, fns []setFn) error {
3030
var errors []error
3131
for _, fn := range fns {
3232
err := fn(name, value)

cmd/minikube/cmd/node_add.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ var nodeAddCmd = &cobra.Command{
9090
}
9191

9292
register.Reg.SetStep(register.InitialSetup)
93-
if err := node.Add(cc, n, deleteNodeOnFailure); err != nil {
93+
options := commandOptions()
94+
options.DeleteOnFailure = deleteNodeOnFailure
95+
if err := node.Add(cc, n, options); err != nil {
9496
_, err := maybeDeleteAndRetry(cmd, *cc, n, nil, err)
9597
if err != nil {
9698
exit.Error(reason.GuestNodeAdd, "failed to add node", err)

cmd/minikube/cmd/node_start.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"os"
2121

2222
"github.com/spf13/cobra"
23-
"github.com/spf13/viper"
2423
"k8s.io/minikube/pkg/minikube/config"
2524
"k8s.io/minikube/pkg/minikube/exit"
2625
"k8s.io/minikube/pkg/minikube/machine"
@@ -56,7 +55,8 @@ var nodeStartCmd = &cobra.Command{
5655
}
5756

5857
register.Reg.SetStep(register.InitialSetup)
59-
r, p, m, h, err := node.Provision(cc, n, viper.GetBool(deleteOnFailure))
58+
options := commandOptions()
59+
r, p, m, h, err := node.Provision(cc, n, options)
6060
if err != nil {
6161
exit.Error(reason.GuestNodeProvision, "provisioning host for node", err)
6262
}
@@ -71,7 +71,7 @@ var nodeStartCmd = &cobra.Command{
7171
ExistingAddons: cc.Addons,
7272
}
7373

74-
if _, err = node.Start(s); err != nil {
74+
if _, err = node.Start(s, options); err != nil {
7575
if _, err := maybeDeleteAndRetry(cmd, *cc, *n, nil, err); err != nil {
7676
node.ExitIfFatal(err, false)
7777
exit.Error(reason.GuestNodeStart, "failed to start node", err)

cmd/minikube/cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"k8s.io/minikube/pkg/minikube/notify"
4141
"k8s.io/minikube/pkg/minikube/out"
4242
"k8s.io/minikube/pkg/minikube/reason"
43+
"k8s.io/minikube/pkg/minikube/run"
4344
"k8s.io/minikube/pkg/minikube/translate"
4445
"k8s.io/minikube/pkg/version"
4546
)
@@ -352,3 +353,11 @@ func applyToAllCommands(cmd *cobra.Command, f func(subCmd *cobra.Command)) {
352353
}
353354
}
354355
}
356+
357+
func commandOptions() run.Options {
358+
return run.Options{
359+
NonInteractive: !viper.GetBool(interactive),
360+
DownloadOnly: viper.GetBool(downloadOnly),
361+
DeleteOnFailure: viper.GetBool(deleteOnFailure),
362+
}
363+
}

cmd/minikube/cmd/start.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func runStart(cmd *cobra.Command, _ []string) {
165165
go download.CleanUpOlderPreloads()
166166

167167
// Avoid blocking execution on optional HTTP fetches
168-
go notify.MaybePrintUpdateTextFromGithub()
168+
go notify.MaybePrintUpdateTextFromGithub(commandOptions())
169169

170170
displayEnviron(os.Environ())
171171
if viper.GetBool(force) {
@@ -300,6 +300,8 @@ func runStart(cmd *cobra.Command, _ []string) {
300300
}
301301

302302
func provisionWithDriver(cmd *cobra.Command, ds registry.DriverState, existing *config.ClusterConfig) (node.Starter, error) {
303+
options := commandOptions()
304+
303305
driverName := ds.Name
304306
klog.Infof("selected driver: %s", driverName)
305307
validateDriver(ds, existing)
@@ -348,7 +350,7 @@ func provisionWithDriver(cmd *cobra.Command, ds registry.DriverState, existing *
348350
klog.Infof("cluster config:\n%+v", cc)
349351

350352
if firewall.IsBootpdBlocked(cc) {
351-
if err := firewall.UnblockBootpd(); err != nil {
353+
if err := firewall.UnblockBootpd(options); err != nil {
352354
klog.Warningf("failed unblocking bootpd from firewall: %v", err)
353355
}
354356
}
@@ -385,7 +387,7 @@ func provisionWithDriver(cmd *cobra.Command, ds registry.DriverState, existing *
385387
ssh.SetDefaultClient(ssh.External)
386388
}
387389

388-
mRunner, preExists, mAPI, host, err := node.Provision(&cc, &n, viper.GetBool(deleteOnFailure))
390+
mRunner, preExists, mAPI, host, err := node.Provision(&cc, &n, options)
389391
if err != nil {
390392
return node.Starter{}, err
391393
}
@@ -471,8 +473,10 @@ func imageMatchesBinaryVersion(imageVersion, binaryVersion string) bool {
471473
}
472474

473475
func startWithDriver(cmd *cobra.Command, starter node.Starter, existing *config.ClusterConfig) (*kubeconfig.Settings, error) {
476+
options := commandOptions()
477+
474478
// start primary control-plane node
475-
configInfo, err := node.Start(starter)
479+
configInfo, err := node.Start(starter, options)
476480
if err != nil {
477481
configInfo, err = maybeDeleteAndRetry(cmd, *starter.Cfg, *starter.Node, starter.ExistingAddons, err)
478482
if err != nil {
@@ -515,7 +519,7 @@ func startWithDriver(cmd *cobra.Command, starter node.Starter, existing *config.
515519
}
516520

517521
out.Ln("") // extra newline for clarity on the command line
518-
if err := node.Add(starter.Cfg, n, viper.GetBool(deleteOnFailure)); err != nil {
522+
if err := node.Add(starter.Cfg, n, options); err != nil {
519523
return nil, errors.Wrap(err, "adding node")
520524
}
521525
}
@@ -648,8 +652,13 @@ func maybeDeleteAndRetry(cmd *cobra.Command, existing config.ClusterConfig, n co
648652
// Re-generate the cluster config, just in case the failure was related to an old config format
649653
cc := updateExistingConfigFromFlags(cmd, &existing)
650654
var configInfo *kubeconfig.Settings
655+
656+
options := commandOptions()
657+
provisionOptions := commandOptions()
658+
provisionOptions.DeleteOnFailure = false
659+
651660
for _, n := range cc.Nodes {
652-
r, p, m, h, err := node.Provision(&cc, &n, false)
661+
r, p, m, h, err := node.Provision(&cc, &n, provisionOptions)
653662
s := node.Starter{
654663
Runner: r,
655664
PreExists: p,
@@ -664,7 +673,7 @@ func maybeDeleteAndRetry(cmd *cobra.Command, existing config.ClusterConfig, n co
664673
return nil, err
665674
}
666675

667-
k, err := node.Start(s)
676+
k, err := node.Start(s, options)
668677
if n.ControlPlane {
669678
configInfo = k
670679
}
@@ -706,13 +715,16 @@ func kubectlVersion(path string) (string, error) {
706715

707716
// returns (current_driver, suggested_drivers, "true, if the driver is set by command line arg or in the config file")
708717
func selectDriver(existing *config.ClusterConfig) (registry.DriverState, []registry.DriverState, bool) {
718+
options := commandOptions()
719+
709720
// Technically unrelated, but important to perform before detection
710721
driver.SetLibvirtURI(viper.GetString(kvmQemuURI))
711722
register.Reg.SetStep(register.SelectingDriver)
723+
712724
// By default, the driver is whatever we used last time
713725
if existing != nil {
714726
old := hostDriver(existing)
715-
ds := driver.Status(old)
727+
ds := driver.Status(old, options)
716728
out.Step(style.Sparkle, `Using the {{.driver}} driver based on existing profile`, out.V{"driver": ds.String()})
717729
return ds, nil, true
718730
}
@@ -729,7 +741,7 @@ func selectDriver(existing *config.ClusterConfig) (registry.DriverState, []regis
729741
`
730742
out.WarningT(warning, out.V{"driver": d, "vmd": vmd})
731743
}
732-
ds := driver.Status(d)
744+
ds := driver.Status(d, options)
733745
if ds.Name == "" {
734746
exit.Message(reason.DrvUnsupportedOS, "The driver '{{.driver}}' is not supported on {{.os}}/{{.arch}}", out.V{"driver": d, "os": runtime.GOOS, "arch": runtime.GOARCH})
735747
}
@@ -739,15 +751,15 @@ func selectDriver(existing *config.ClusterConfig) (registry.DriverState, []regis
739751

740752
// Fallback to old driver parameter
741753
if d := viper.GetString("vm-driver"); d != "" {
742-
ds := driver.Status(viper.GetString("vm-driver"))
754+
ds := driver.Status(viper.GetString("vm-driver"), options)
743755
if ds.Name == "" {
744756
exit.Message(reason.DrvUnsupportedOS, "The driver '{{.driver}}' is not supported on {{.os}}/{{.arch}}", out.V{"driver": d, "os": runtime.GOOS, "arch": runtime.GOARCH})
745757
}
746758
out.Step(style.Sparkle, `Using the {{.driver}} driver based on user configuration`, out.V{"driver": ds.String()})
747759
return ds, nil, true
748760
}
749761

750-
choices := driver.Choices(viper.GetBool("vm"))
762+
choices := driver.Choices(viper.GetBool("vm"), options)
751763
pick, alts, rejects := driver.Suggest(choices)
752764
if pick.Name == "" {
753765
out.Step(style.ThumbsDown, "Unable to pick a default driver. Here is what was considered, in preference order:")

cmd/minikube/cmd/start_flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func validateVfkitNetwork(n string) string {
535535
// always available
536536
case "vmnet-shared":
537537
// "vment-shared" provides access between machines, with lower performance compared to "nat".
538-
if err := vmnet.ValidateHelper(); err != nil {
538+
if err := vmnet.ValidateHelper(commandOptions()); err != nil {
539539
vmnetErr := err.(*vmnet.Error)
540540
exit.Message(vmnetErr.Kind, "failed to validate {{.network}} network: {{.reason}}", out.V{"network": n, "reason": err})
541541
}

cmd/minikube/cmd/status.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/minikube/pkg/minikube/notify"
3939
"k8s.io/minikube/pkg/minikube/out"
4040
"k8s.io/minikube/pkg/minikube/reason"
41+
"k8s.io/minikube/pkg/minikube/run"
4142
)
4243

4344
var (
@@ -90,7 +91,7 @@ var statusCmd = &cobra.Command{
9091
}
9192

9293
out.SetJSON(output == "json")
93-
go notify.MaybePrintUpdateTextFromGithub()
94+
go notify.MaybePrintUpdateTextFromGithub(run.Options{})
9495

9596
cname := ClusterFlagValue()
9697
api, cc := mustload.Partial(cname)

pkg/drivers/common/common.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/pkg/errors"
3636

3737
"k8s.io/klog/v2"
38+
"k8s.io/minikube/pkg/minikube/run"
3839
"k8s.io/minikube/pkg/util"
3940
)
4041

@@ -85,7 +86,9 @@ func CreateRawDisk(diskPath string, sizeMB int) error {
8586
}
8687

8788
// CommonDriver is the common driver base class
88-
type CommonDriver struct{}
89+
type CommonDriver struct {
90+
Options run.Options `json:"-"`
91+
}
8992

9093
// GetCreateFlags is not implemented yet
9194
func (d *CommonDriver) GetCreateFlags() []mcnflag.Flag {

0 commit comments

Comments
 (0)