From 276ad6c7c740d7a5193b41feadaa216431d0d929 Mon Sep 17 00:00:00 2001 From: hetong07 Date: Sun, 31 Jan 2021 12:34:05 -0800 Subject: [PATCH 1/5] First step to address #10311: disallow running minikube.exe insided WSL. --- cmd/minikube/main.go | 9 +++++++++ pkg/minikube/reason/reason.go | 1 + 2 files changed, 10 insertions(+) diff --git a/cmd/minikube/main.go b/cmd/minikube/main.go index 648a2656548c..8cb320ce3444 100644 --- a/cmd/minikube/main.go +++ b/cmd/minikube/main.go @@ -24,9 +24,13 @@ import ( "os" "regexp" "strconv" + "path/filepath" "github.com/spf13/pflag" "k8s.io/klog/v2" + "k8s.io/minikube/pkg/minikube/driver" + "k8s.io/minikube/pkg/minikube/exit" + "k8s.io/minikube/pkg/minikube/reason" // Register drivers _ "k8s.io/minikube/pkg/minikube/registry/drvs" @@ -72,6 +76,11 @@ func main() { } out.SetOutFile(os.Stdout) out.SetErrFile(os.Stderr) + + if filepath.Ext(os.Args[0]) == ".exe" && driver.IsMicrosoftWSL() { + exit.Message(reason.WslExeConflict, "Cannot run windows binary inside WSL.") + } + cmd.Execute() } diff --git a/pkg/minikube/reason/reason.go b/pkg/minikube/reason/reason.go index ee406919d95b..e1496ad00f97 100644 --- a/pkg/minikube/reason/reason.go +++ b/pkg/minikube/reason/reason.go @@ -286,6 +286,7 @@ var ( EnvMultiConflict = Kind{ID: "ENV_MULTINODE_CONFLICT", ExitCode: ExGuestConflict} EnvDockerUnavailable = Kind{ID: "ENV_DOCKER_UNAVAILABLE", ExitCode: ExRuntimeUnavailable} EnvPodmanUnavailable = Kind{ID: "ENV_PODMAN_UNAVAILABLE", ExitCode: ExRuntimeUnavailable} + WslExeConflict = Kind{ID: "WSL_EXE_CONFLICT", ExitCode: ExGuestConflict} AddonUnsupported = Kind{ID: "SVC_ADDON_UNSUPPORTED", ExitCode: ExSvcUnsupported} AddonNotEnabled = Kind{ID: "SVC_ADDON_NOT_ENABLED", ExitCode: ExProgramConflict} From ad08087b448c8a9b43765fea408f256501f96efd Mon Sep 17 00:00:00 2001 From: hetong07 Date: Tue, 2 Feb 2021 23:33:24 -0800 Subject: [PATCH 2/5] Allow user to use --force to run windows binary inside WSL. --- cmd/minikube/cmd/root.go | 12 ++++++++++++ cmd/minikube/main.go | 9 --------- pkg/minikube/reason/reason.go | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index a29886b5b207..0ba39bc59e4a 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -35,6 +35,7 @@ import ( "k8s.io/minikube/pkg/minikube/audit" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/constants" + "k8s.io/minikube/pkg/minikube/driver" "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/out" @@ -98,6 +99,17 @@ func Execute() { } else { os.Args = append([]string{RootCmd.Use, callingCmd, "--"}, os.Args[1:]...) } + } else if filepath.Ext(callingCmd) == ".exe" && driver.IsMicrosoftWSL() { + var found = false + for _, a := range os.Args { + if a == "--force" { + found = true + break + } + } + if !found { + exit.Message(reason.WrongBinary, "Cannot run Windows binary inside WSL, please download linux binary from https://minikube.sigs.k8s.io/docs/start/. Or you can use '--force' to force execution which would be at your own risk.") + } } for _, c := range RootCmd.Commands() { c.Short = translate.T(c.Short) diff --git a/cmd/minikube/main.go b/cmd/minikube/main.go index 8cb320ce3444..648a2656548c 100644 --- a/cmd/minikube/main.go +++ b/cmd/minikube/main.go @@ -24,13 +24,9 @@ import ( "os" "regexp" "strconv" - "path/filepath" "github.com/spf13/pflag" "k8s.io/klog/v2" - "k8s.io/minikube/pkg/minikube/driver" - "k8s.io/minikube/pkg/minikube/exit" - "k8s.io/minikube/pkg/minikube/reason" // Register drivers _ "k8s.io/minikube/pkg/minikube/registry/drvs" @@ -76,11 +72,6 @@ func main() { } out.SetOutFile(os.Stdout) out.SetErrFile(os.Stderr) - - if filepath.Ext(os.Args[0]) == ".exe" && driver.IsMicrosoftWSL() { - exit.Message(reason.WslExeConflict, "Cannot run windows binary inside WSL.") - } - cmd.Execute() } diff --git a/pkg/minikube/reason/reason.go b/pkg/minikube/reason/reason.go index e1496ad00f97..f9ccbdf7e7fb 100644 --- a/pkg/minikube/reason/reason.go +++ b/pkg/minikube/reason/reason.go @@ -190,6 +190,7 @@ var ( RsrcInsufficientStorage = Kind{ID: "RSRC_INSUFFICIENT_STORAGE", ExitCode: ExInsufficientStorage, Style: style.UnmetRequirement} + WrongBinary = Kind{ID: "WRONG_BINARY", ExitCode: ExHostError} HostHomeMkdir = Kind{ID: "HOST_HOME_MKDIR", ExitCode: ExHostPermission} HostHomeChown = Kind{ID: "HOST_HOME_CHOWN", ExitCode: ExHostPermission} HostBrowser = Kind{ID: "HOST_BROWSER", ExitCode: ExHostError} @@ -286,7 +287,6 @@ var ( EnvMultiConflict = Kind{ID: "ENV_MULTINODE_CONFLICT", ExitCode: ExGuestConflict} EnvDockerUnavailable = Kind{ID: "ENV_DOCKER_UNAVAILABLE", ExitCode: ExRuntimeUnavailable} EnvPodmanUnavailable = Kind{ID: "ENV_PODMAN_UNAVAILABLE", ExitCode: ExRuntimeUnavailable} - WslExeConflict = Kind{ID: "WSL_EXE_CONFLICT", ExitCode: ExGuestConflict} AddonUnsupported = Kind{ID: "SVC_ADDON_UNSUPPORTED", ExitCode: ExSvcUnsupported} AddonNotEnabled = Kind{ID: "SVC_ADDON_NOT_ENABLED", ExitCode: ExProgramConflict} From 547f506701fd0a0fba8dbebc2ed6fe0fd919642c Mon Sep 17 00:00:00 2001 From: hetong07 Date: Wed, 3 Feb 2021 18:01:30 -0800 Subject: [PATCH 3/5] Rephrase the adivce, use the runtime.GOOS to check whether it is .exe binary and modify the error name to address comments in pull request. --- cmd/minikube/cmd/root.go | 2 +- pkg/minikube/reason/reason.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index dce3116cc778..5dff94480824 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -108,7 +108,7 @@ func Execute() { } } if !found { - exit.Message(reason.WrongBinary, "Cannot run Windows binary inside WSL, please download linux binary from https://minikube.sigs.k8s.io/docs/start/. Or you can use '--force' to force execution which would be at your own risk.") + exit.Message(reason.WrongBinaryWSL, "You are trying to run windows .exe binary inside WSL, for better integration please use Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force" } } for _, c := range RootCmd.Commands() { diff --git a/pkg/minikube/reason/reason.go b/pkg/minikube/reason/reason.go index f9ccbdf7e7fb..18f7ffd2b6fa 100644 --- a/pkg/minikube/reason/reason.go +++ b/pkg/minikube/reason/reason.go @@ -190,7 +190,7 @@ var ( RsrcInsufficientStorage = Kind{ID: "RSRC_INSUFFICIENT_STORAGE", ExitCode: ExInsufficientStorage, Style: style.UnmetRequirement} - WrongBinary = Kind{ID: "WRONG_BINARY", ExitCode: ExHostError} + WrongBinaryWSL = Kind{ID: "WRONG_BINARY_WSL", ExitCode: ExHostError} HostHomeMkdir = Kind{ID: "HOST_HOME_MKDIR", ExitCode: ExHostPermission} HostHomeChown = Kind{ID: "HOST_HOME_CHOWN", ExitCode: ExHostPermission} HostBrowser = Kind{ID: "HOST_BROWSER", ExitCode: ExHostError} From 6d8804e10ec61473344891e47a79db0d87100be0 Mon Sep 17 00:00:00 2001 From: hetong07 Date: Wed, 3 Feb 2021 18:21:10 -0800 Subject: [PATCH 4/5] Use the 'ExProgramUnsupported' as the error code for MK_WRONG_BINARY_WSL error. --- cmd/minikube/cmd/root.go | 2 +- pkg/minikube/reason/reason.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 5dff94480824..b01d22659243 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -108,7 +108,7 @@ func Execute() { } } if !found { - exit.Message(reason.WrongBinaryWSL, "You are trying to run windows .exe binary inside WSL, for better integration please use Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force" + exit.Message(reason.WrongBinaryWSL, "You are trying to run windows .exe binary inside WSL, for better integration please use Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force") } } for _, c := range RootCmd.Commands() { diff --git a/pkg/minikube/reason/reason.go b/pkg/minikube/reason/reason.go index 18f7ffd2b6fa..bb033bd405ad 100644 --- a/pkg/minikube/reason/reason.go +++ b/pkg/minikube/reason/reason.go @@ -75,6 +75,8 @@ var ( Usage = Kind{ID: "MK_USAGE", ExitCode: ExProgramUsage} Interrupted = Kind{ID: "MK_INTERRUPTED", ExitCode: ExProgramConflict} + WrongBinaryWSL = Kind{ID: "MK_WRONG_BINARY_WSL", ExitCode: ExProgramUnsupported} + NewAPIClient = Kind{ID: "MK_NEW_APICLIENT", ExitCode: ExProgramError} InternalAddonEnable = Kind{ID: "MK_ADDON_ENABLE", ExitCode: ExProgramError} InternalAddConfig = Kind{ID: "MK_ADD_CONFIG", ExitCode: ExProgramError} @@ -190,7 +192,6 @@ var ( RsrcInsufficientStorage = Kind{ID: "RSRC_INSUFFICIENT_STORAGE", ExitCode: ExInsufficientStorage, Style: style.UnmetRequirement} - WrongBinaryWSL = Kind{ID: "WRONG_BINARY_WSL", ExitCode: ExHostError} HostHomeMkdir = Kind{ID: "HOST_HOME_MKDIR", ExitCode: ExHostPermission} HostHomeChown = Kind{ID: "HOST_HOME_CHOWN", ExitCode: ExHostPermission} HostBrowser = Kind{ID: "HOST_BROWSER", ExitCode: ExHostError} From bdac73d2fd9cd7bf4e252ba5bf5f29e379e648cc Mon Sep 17 00:00:00 2001 From: hetong07 Date: Sun, 7 Feb 2021 17:37:01 -0800 Subject: [PATCH 5/5] Put the environment check as a separate if-else clause, and make this check as early as possible. --- cmd/minikube/cmd/root.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index b01d22659243..8b03ab50867f 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -79,6 +79,20 @@ var RootCmd = &cobra.Command{ func Execute() { defer audit.Log(time.Now()) + // Check whether this is a windows binary (.exe) running inisde WSL. + if runtime.GOOS == "windows" && driver.IsMicrosoftWSL() { + var found = false + for _, a := range os.Args { + if a == "--force" { + found = true + break + } + } + if !found { + exit.Message(reason.WrongBinaryWSL, "You are trying to run windows .exe binary inside WSL, for better integration please use Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force") + } + } + _, callingCmd := filepath.Split(os.Args[0]) if callingCmd == "kubectl" { @@ -99,18 +113,8 @@ func Execute() { } else { os.Args = append([]string{RootCmd.Use, callingCmd, "--"}, os.Args[1:]...) } - } else if runtime.GOOS == "windows" && driver.IsMicrosoftWSL() { - var found = false - for _, a := range os.Args { - if a == "--force" { - found = true - break - } - } - if !found { - exit.Message(reason.WrongBinaryWSL, "You are trying to run windows .exe binary inside WSL, for better integration please use Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force") - } } + for _, c := range RootCmd.Commands() { c.Short = translate.T(c.Short) c.Long = translate.T(c.Long)