Skip to content

Commit 0500345

Browse files
committed
force: Remove viper force checks outside cmd
Add run.CommandOptions.Force option and use it to replace calls to viper.GetBool("force") outside the cmd package, and call to viper.GetBool(force) in the cmd package.
1 parent 118da4d commit 0500345

File tree

8 files changed

+85
-85
lines changed

8 files changed

+85
-85
lines changed

cmd/minikube/cmd/flags/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
const (
2727
Interactive = "interactive"
2828
DownloadOnly = "download-only"
29+
Force = "force"
2930
)
3031

3132
// CommandOptions returns minikube runtime options from command line flags.
@@ -36,5 +37,6 @@ func CommandOptions() *run.CommandOptions {
3637
NonInteractive: !viper.GetBool(Interactive),
3738
DownloadOnly: viper.GetBool(DownloadOnly),
3839
ProfileName: viper.GetString(config.ProfileName),
40+
Force: viper.GetBool(Force),
3941
}
4042
}

cmd/minikube/cmd/start.go

Lines changed: 56 additions & 59 deletions
Large diffs are not rendered by default.

cmd/minikube/cmd/start_flags.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ const (
105105
dnsProxy = "dns-proxy"
106106
hostDNSResolver = "host-dns-resolver"
107107
waitComponents = "wait"
108-
force = "force"
109108
dryRun = "dry-run"
110109
waitTimeout = "wait-timeout"
111110
nativeSSH = "native-ssh"
@@ -159,7 +158,7 @@ func initMinikubeFlags() {
159158
// e.g. iso-url => $ENVPREFIX_ISO_URL
160159
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
161160
viper.AutomaticEnv()
162-
startCmd.Flags().Bool(force, false, "Force minikube to perform possibly dangerous operations")
161+
startCmd.Flags().Bool(flags.Force, false, "Force minikube to perform possibly dangerous operations")
163162
startCmd.Flags().Bool(flags.Interactive, true, "Allow user prompts for more information")
164163
startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does not mutate system state")
165164

@@ -317,7 +316,7 @@ func ClusterFlagValue() string {
317316
func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k8sVersion string, rtime string, drvName string, options *run.CommandOptions) (config.ClusterConfig, config.Node, error) {
318317
var cc config.ClusterConfig
319318
if existing != nil {
320-
cc = updateExistingConfigFromFlags(cmd, existing)
319+
cc = updateExistingConfigFromFlags(cmd, existing, options)
321320

322321
// identify appropriate cni then configure cruntime accordingly
323322
if _, err := cni.New(&cc); err != nil {
@@ -382,7 +381,7 @@ func getCPUCount(drvName string) int {
382381
return si.CPUs
383382
}
384383

385-
func getMemorySize(cmd *cobra.Command, drvName string) int {
384+
func getMemorySize(cmd *cobra.Command, drvName string, options *run.CommandOptions) int {
386385
sysLimit, containerLimit, err := memoryLimits(drvName)
387386
if err != nil {
388387
klog.Warningf("Unable to query memory limits: %+v", err)
@@ -406,7 +405,7 @@ func getMemorySize(cmd *cobra.Command, drvName string) int {
406405
exit.Message(reason.Usage, "{{.driver_name}} has only {{.container_limit}}MB memory but you specified {{.specified_memory}}MB", out.V{"container_limit": containerLimit, "specified_memory": mem, "driver_name": driver.FullName(drvName)})
407406
}
408407
} else {
409-
validateRequestedMemorySize(mem, drvName)
408+
validateRequestedMemorySize(mem, drvName, options)
410409
klog.Infof("Using suggested %dMB memory alloc based on sys=%dMB, container=%dMB", mem, sysLimit, containerLimit)
411410
}
412411

@@ -576,7 +575,7 @@ func generateNewConfigFromFlags(cmd *cobra.Command, k8sVersion string, rtime str
576575
KicBaseImage: viper.GetString(kicBaseImage),
577576
Network: getNetwork(drvName, options),
578577
Subnet: viper.GetString(subnet),
579-
Memory: getMemorySize(cmd, drvName),
578+
Memory: getMemorySize(cmd, drvName, options),
580579
CPUs: getCPUCount(drvName),
581580
DiskSize: getDiskSize(),
582581
Driver: drvName,
@@ -749,7 +748,7 @@ func checkNumaCount(k8sVersion string) {
749748
}
750749

751750
// upgradeExistingConfig upgrades legacy configuration files
752-
func upgradeExistingConfig(cmd *cobra.Command, cc *config.ClusterConfig) {
751+
func upgradeExistingConfig(cmd *cobra.Command, cc *config.ClusterConfig, options *run.CommandOptions) {
753752
if cc == nil {
754753
return
755754
}
@@ -772,7 +771,7 @@ func upgradeExistingConfig(cmd *cobra.Command, cc *config.ClusterConfig) {
772771

773772
if cc.Memory == 0 && !driver.IsKIC(cc.Driver) {
774773
klog.Info("Existing config file was missing memory. (could be an old minikube config), will use the default value")
775-
memInMB := getMemorySize(cmd, cc.Driver)
774+
memInMB := getMemorySize(cmd, cc.Driver, options)
776775
cc.Memory = memInMB
777776
}
778777

@@ -783,8 +782,8 @@ func upgradeExistingConfig(cmd *cobra.Command, cc *config.ClusterConfig) {
783782

784783
// updateExistingConfigFromFlags will update the existing config from the flags - used on a second start
785784
// skipping updating existing docker env, docker opt, InsecureRegistry, registryMirror, extra-config, apiserver-ips
786-
func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterConfig) config.ClusterConfig { //nolint to suppress cyclomatic complexity 45 of func `updateExistingConfigFromFlags` is high (> 30)
787-
validateFlags(cmd, existing.Driver)
785+
func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterConfig, options *run.CommandOptions) config.ClusterConfig { //nolint to suppress cyclomatic complexity 45 of func `updateExistingConfigFromFlags` is high (> 30)
786+
validateFlags(cmd, existing.Driver, options)
788787

789788
cc := *existing
790789

@@ -802,7 +801,7 @@ func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterC
802801
updateIntFromFlag(cmd, &cc.APIServerPort, apiServerPort)
803802
}
804803

805-
if cmd.Flags().Changed(memory) && getMemorySize(cmd, cc.Driver) != cc.Memory {
804+
if cmd.Flags().Changed(memory) && getMemorySize(cmd, cc.Driver, options) != cc.Memory {
806805
out.WarningT("You cannot change the memory size for an existing minikube cluster. Please first delete the cluster.")
807806
}
808807

@@ -811,7 +810,7 @@ func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterC
811810
}
812811

813812
// validate the memory size in case user changed their system memory limits (example change docker desktop or upgraded memory.)
814-
validateRequestedMemorySize(cc.Memory, cc.Driver)
813+
validateRequestedMemorySize(cc.Memory, cc.Driver, options)
815814

816815
if cmd.Flags().Changed(humanReadableDiskSize) && getDiskSize() != existing.DiskSize {
817816
out.WarningT("You cannot change the disk size for an existing minikube cluster. Please first delete the cluster.")

pkg/minikube/machine/fix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func fixHost(api libmachine.API, cc *config.ClusterConfig, n *config.Node, optio
6161
if err != nil {
6262
return h, errors.Wrap(err, "error loading existing host. Please try running [minikube delete], then run [minikube start] again")
6363
}
64-
defer postStartValidations(h, cc.Driver)
64+
defer postStartValidations(h, cc.Driver, options)
6565

6666
driverName := h.Driver.DriverName()
6767

pkg/minikube/machine/start.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/docker/machine/libmachine/host"
3636
"github.com/juju/mutex/v2"
3737
"github.com/pkg/errors"
38-
"github.com/spf13/viper"
3938
"k8s.io/klog/v2"
4039
"k8s.io/minikube/pkg/drivers/kic/oci"
4140
"k8s.io/minikube/pkg/minikube/command"
@@ -150,7 +149,7 @@ func createHost(api libmachine.API, cfg *config.ClusterConfig, n *config.Node, o
150149
if err != nil {
151150
return nil, errors.Wrap(err, "new host")
152151
}
153-
defer postStartValidations(h, cfg.Driver)
152+
defer postStartValidations(h, cfg.Driver, options)
154153

155154
h.HostOptions.AuthOptions.CertDir = localpath.MiniPath()
156155
h.HostOptions.AuthOptions.StorePath = localpath.MiniPath()
@@ -202,7 +201,7 @@ func timedCreateHost(h *host.Host, api libmachine.API, t time.Duration) error {
202201

203202
// postStartValidations are validations against the host after it is created
204203
// TODO: Add validations for VM drivers as well, see issue #9035
205-
func postStartValidations(h *host.Host, drvName string) {
204+
func postStartValidations(h *host.Host, drvName string, options *run.CommandOptions) {
206205
if !driver.IsKIC(drvName) {
207206
return
208207
}
@@ -226,7 +225,7 @@ func postStartValidations(h *host.Host, drvName string) {
226225
return
227226
}
228227

229-
if viper.GetBool("force") {
228+
if options.Force {
230229
return
231230
}
232231

pkg/minikube/node/start.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func Start(starter Starter, options *run.CommandOptions) (*kubeconfig.Settings,
211211
addonList := viper.GetStringSlice(config.AddonListFlag)
212212
enabledAddons := make(chan []string, 1)
213213
if starter.ExistingAddons != nil {
214-
if viper.GetBool("force") {
214+
if options.Force {
215215
addons.Force = true
216216
}
217217
list := addons.ToEnable(starter.Cfg, starter.ExistingAddons, addonList)
@@ -669,7 +669,7 @@ func startMachine(cfg *config.ClusterConfig, node *config.Node, delOnFail bool,
669669
return runner, preExists, m, hostInfo, errors.Wrap(err, "Failed to get command runner")
670670
}
671671

672-
ip, err := validateNetwork(hostInfo, runner, cfg.KubernetesConfig.ImageRepository)
672+
ip, err := validateNetwork(hostInfo, runner, cfg.KubernetesConfig.ImageRepository, options)
673673
if err != nil {
674674
return runner, preExists, m, hostInfo, errors.Wrap(err, "Failed to validate network")
675675
}
@@ -752,7 +752,7 @@ func startHostInternal(api libmachine.API, cc *config.ClusterConfig, n *config.N
752752
}
753753

754754
// validateNetwork tries to catch network problems as soon as possible
755-
func validateNetwork(h *host.Host, r command.Runner, imageRepository string) (string, error) {
755+
func validateNetwork(h *host.Host, r command.Runner, imageRepository string, options *run.CommandOptions) (string, error) {
756756
ip, err := h.Driver.GetIP()
757757
if err != nil {
758758
return ip, err
@@ -783,7 +783,7 @@ func validateNetwork(h *host.Host, r command.Runner, imageRepository string) (st
783783
}
784784

785785
if shouldTrySSH(h.Driver.DriverName(), ip) {
786-
if err := trySSH(h, ip); err != nil {
786+
if err := trySSH(h, ip, options); err != nil {
787787
return ip, err
788788
}
789789
}
@@ -804,8 +804,8 @@ func shouldTrySSH(driverName, ip string) bool {
804804
return true
805805
}
806806

807-
func trySSH(h *host.Host, ip string) error {
808-
if viper.GetBool("force") {
807+
func trySSH(h *host.Host, ip string, options *run.CommandOptions) error {
808+
if options.Force {
809809
return nil
810810
}
811811

pkg/minikube/registry/drvs/docker/docker.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/blang/semver/v4"
2929
"github.com/docker/machine/libmachine/drivers"
3030
"github.com/pkg/errors"
31-
"github.com/spf13/viper"
3231
"k8s.io/klog/v2"
3332
"k8s.io/minikube/pkg/drivers/kic"
3433
"k8s.io/minikube/pkg/drivers/kic/oci"
@@ -96,7 +95,7 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
9695
}), nil
9796
}
9897

99-
func status(_ *run.CommandOptions) (retState registry.State) {
98+
func status(options *run.CommandOptions) (retState registry.State) {
10099
version, state := dockerVersionOrState()
101100
if state.Error != nil {
102101
return state
@@ -122,7 +121,7 @@ func status(_ *run.CommandOptions) (retState registry.State) {
122121
dockerEngineVersion := versions[0]
123122
dockerPlatformVersion := versions[1]
124123
klog.Infof("docker version: %s", version)
125-
if !viper.GetBool("force") {
124+
if !options.Force {
126125
if s := checkDockerDesktopVersion(dockerPlatformVersion); s.Error != nil {
127126
return s
128127
}

pkg/minikube/run/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ type CommandOptions struct {
3030
// ProfileName is set if the minikube command run with the --profile flag, using
3131
// specific minikube instance.
3232
ProfileName string
33+
34+
// Force is true if the minikube command run with the --force flag and can
35+
// perform possibly dangerous operations.
36+
Force bool
3337
}

0 commit comments

Comments
 (0)