Skip to content

Commit 539567b

Browse files
committed
run: Introduce minikube/run and cmd/flags packages
This change introduce the basic infrastructure for passing command line options from the cmd/minikube/cmd package to other packages. The cmd/flags package provides the CommandOptions() function returning run.CommandOptions initialized using viper. This package keeps the constants for command line options (e.g. "interactive") that we want to share with various packages without accessing global state via viper. To use options in drivers code, include CommandOptions in the CommonDriver struct. The options will be initialized from the command line options when creating a driver. The basic idea is to create options in the command: options := flags.CommandOptions() And pass it to other packages, where code will use: if options.NonInteractive { Instead of: if viper.GetBool("interactive") { This is type safe and allows reliable parallel testing.
1 parent 5ca02f9 commit 539567b

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

cmd/minikube/cmd/flags/flags.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2025 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 flags
18+
19+
import (
20+
"github.com/spf13/viper"
21+
"k8s.io/minikube/pkg/minikube/run"
22+
)
23+
24+
// Flag names passed to minikube via run.Options.
25+
const (
26+
Interactive = "interactive"
27+
)
28+
29+
// CommandOptions returns minikube runtime options from command line flags.
30+
// Flags that must be handled outside of the cmd package must be added to
31+
// run.CommandOptions.
32+
func CommandOptions() *run.CommandOptions {
33+
return &run.CommandOptions{
34+
NonInteractive: !viper.GetBool(Interactive),
35+
}
36+
}

cmd/minikube/cmd/start.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"k8s.io/klog/v2"
5252

5353
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
54+
"k8s.io/minikube/cmd/minikube/cmd/flags"
5455
"k8s.io/minikube/pkg/drivers/kic/oci"
5556
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil"
5657
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
@@ -534,7 +535,7 @@ func updateDriver(driverName string) {
534535
v, err := version.GetSemverVersion()
535536
if err != nil {
536537
out.WarningT("Error parsing minikube version: {{.error}}", out.V{"error": err})
537-
} else if err := auxdriver.InstallOrUpdate(driverName, localpath.MakeMiniPath("bin"), v, viper.GetBool(interactive), viper.GetBool(autoUpdate)); err != nil {
538+
} else if err := auxdriver.InstallOrUpdate(driverName, localpath.MakeMiniPath("bin"), v, viper.GetBool(flags.Interactive), viper.GetBool(autoUpdate)); err != nil {
538539
if errors.Is(err, auxdriver.ErrAuxDriverVersionCommandFailed) {
539540
exit.Error(reason.DrvAuxNotHealthy, "Aux driver "+driverName, err)
540541
}
@@ -1052,7 +1053,7 @@ func validateUser(drvName string) {
10521053

10531054
// None driver works with root and without root on Linux
10541055
if runtime.GOOS == "linux" && drvName == driver.None {
1055-
if !viper.GetBool(interactive) {
1056+
if !viper.GetBool(flags.Interactive) {
10561057
test := exec.Command("sudo", "-n", "echo", "-n")
10571058
if err := test.Run(); err != nil {
10581059
exit.Message(reason.DrvNeedsRoot, `sudo requires a password, and --interactive=false`)

cmd/minikube/cmd/start_flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/spf13/cobra"
2929
"github.com/spf13/viper"
3030
"k8s.io/klog/v2"
31+
"k8s.io/minikube/cmd/minikube/cmd/flags"
3132
"k8s.io/minikube/pkg/drivers/common/vmnet"
3233
"k8s.io/minikube/pkg/drivers/kic"
3334
"k8s.io/minikube/pkg/drivers/kic/oci"
@@ -106,7 +107,6 @@ const (
106107
waitComponents = "wait"
107108
force = "force"
108109
dryRun = "dry-run"
109-
interactive = "interactive"
110110
waitTimeout = "wait-timeout"
111111
nativeSSH = "native-ssh"
112112
minUsableMem = 1800 // Kubernetes (kubeadm) will not start with less
@@ -160,7 +160,7 @@ func initMinikubeFlags() {
160160
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
161161
viper.AutomaticEnv()
162162
startCmd.Flags().Bool(force, false, "Force minikube to perform possibly dangerous operations")
163-
startCmd.Flags().Bool(interactive, true, "Allow user prompts for more information")
163+
startCmd.Flags().Bool(flags.Interactive, true, "Allow user prompts for more information")
164164
startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does not mutate system state")
165165

166166
startCmd.Flags().String(cpus, "2", fmt.Sprintf("Number of CPUs allocated to Kubernetes. Use %q to use the maximum number of CPUs. Use %q to not specify a limit (Docker/Podman only)", constants.MaxResources, constants.NoLimit))

pkg/drivers/common/common.go

Lines changed: 9 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,14 @@ 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+
// CommandOptions keeps the minikube command line options shared with the
91+
// minikube packages. This is initialized when loading the driver and should
92+
// not be persisted.
93+
// TODO: Consider removing when libmachine API is part of minikube:
94+
// https://github.com/kubernetes/minikube/issues/21789
95+
CommandOptions run.CommandOptions `json:"-"`
96+
}
8997

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

pkg/minikube/run/options.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2025 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 run
18+
19+
// CommandOptions are minikube command line options.
20+
type CommandOptions struct {
21+
// NonInteractive is true if the minikube command run with the
22+
// --interactive=false flag and we can not interact with the user.
23+
NonInteractive bool
24+
}

0 commit comments

Comments
 (0)