From 5b423abed7fe1fba085ba907db66369cd4b0b671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sat, 28 Mar 2020 14:19:11 +0100 Subject: [PATCH] Run dashboard with internal kubectl if not in path If "kubectl" is not in the PATH, then use the same cached binary as with the "minikube kubectl" command (version matching cluster). --- cmd/minikube/cmd/dashboard.go | 19 ++++++++++++------- cmd/minikube/cmd/kubectl.go | 23 +++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cmd/minikube/cmd/dashboard.go b/cmd/minikube/cmd/dashboard.go index c235adbab27e..e3c9e93b6be3 100644 --- a/cmd/minikube/cmd/dashboard.go +++ b/cmd/minikube/cmd/dashboard.go @@ -63,10 +63,8 @@ var dashboardCmd = &cobra.Command{ } } - kubectl, err := exec.LookPath("kubectl") - if err != nil { - exit.WithCodeT(exit.NoInput, "kubectl not found in PATH, but is required for the dashboard. Installation guide: https://kubernetes.io/docs/tasks/tools/install-kubectl/") - } + kubectlVersion := co.Config.KubernetesConfig.KubernetesVersion + var err error // Check dashboard status before enabling it dashboardAddon := assets.Addons["dashboard"] @@ -90,7 +88,7 @@ var dashboardCmd = &cobra.Command{ } out.ErrT(out.Launch, "Launching proxy ...") - p, hostPort, err := kubectlProxy(kubectl, cname) + p, hostPort, err := kubectlProxy(kubectlVersion, cname) if err != nil { exit.WithError("kubectl proxy", err) } @@ -124,10 +122,17 @@ var dashboardCmd = &cobra.Command{ } // kubectlProxy runs "kubectl proxy", returning host:port -func kubectlProxy(path string, contextName string) (*exec.Cmd, string, error) { +func kubectlProxy(kubectlVersion string, contextName string) (*exec.Cmd, string, error) { // port=0 picks a random system port - cmd := exec.Command(path, "--context", contextName, "proxy", "--port=0") + kubectlArgs := []string{"--context", contextName, "proxy", "--port=0"} + + var cmd *exec.Cmd + if kubectl, err := exec.LookPath("kubectl"); err == nil { + cmd = exec.Command(kubectl, kubectlArgs...) + } else if cmd, err = KubectlCommand(kubectlVersion, kubectlArgs...); err != nil { + return nil, "", err + } stdoutPipe, err := cmd.StdoutPipe() if err != nil { diff --git a/cmd/minikube/cmd/kubectl.go b/cmd/minikube/cmd/kubectl.go index f4867b45e0ea..8dba01a4e333 100644 --- a/cmd/minikube/cmd/kubectl.go +++ b/cmd/minikube/cmd/kubectl.go @@ -41,19 +41,12 @@ minikube kubectl -- --help minikube kubectl -- get pods --namespace kube-system`, Run: func(cmd *cobra.Command, args []string) { co := mustload.Healthy(ClusterFlagValue()) - version := co.Config.KubernetesConfig.KubernetesVersion - if version == "" { - version = constants.DefaultKubernetesVersion - } - - path, err := node.CacheKubectlBinary(version) + c, err := KubectlCommand(version, args...) if err != nil { out.ErrLn("Error caching kubectl: %v", err) } - glog.Infof("Running %s %v", path, args) - c := exec.Command(path, args...) c.Stdin = os.Stdin c.Stdout = os.Stdout c.Stderr = os.Stderr @@ -70,3 +63,17 @@ minikube kubectl -- get pods --namespace kube-system`, } }, } + +// KubectlCommand will return kubectl command with a version matching the cluster +func KubectlCommand(version string, args ...string) (*exec.Cmd, error) { + if version == "" { + version = constants.DefaultKubernetesVersion + } + + path, err := node.CacheKubectlBinary(version) + if err != nil { + return nil, err + } + + return exec.Command(path, args...), nil +}