Skip to content

Commit

Permalink
Run dashboard with internal kubectl if not in path
Browse files Browse the repository at this point in the history
If "kubectl" is not in the PATH, then use the same cached binary
as with the "minikube kubectl" command (version matching cluster).
  • Loading branch information
afbjorklund committed Mar 28, 2020
1 parent 1e70286 commit 6d53c00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
18 changes: 11 additions & 7 deletions cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ 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/")
}
var err error

// Check dashboard status before enabling it
dashboardAddon := assets.Addons["dashboard"]
Expand All @@ -90,7 +87,7 @@ var dashboardCmd = &cobra.Command{
}

out.ErrT(out.Launch, "Launching proxy ...")
p, hostPort, err := kubectlProxy(kubectl, cname)
p, hostPort, err := kubectlProxy(co, cname)
if err != nil {
exit.WithError("kubectl proxy", err)
}
Expand Down Expand Up @@ -124,10 +121,17 @@ var dashboardCmd = &cobra.Command{
}

// kubectlProxy runs "kubectl proxy", returning host:port
func kubectlProxy(path string, contextName string) (*exec.Cmd, string, error) {
func kubectlProxy(co mustload.ClusterController, 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(co, kubectlArgs...); err != nil {
return nil, "", err
}

stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
Expand Down
25 changes: 16 additions & 9 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,11 @@ 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(co, 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
Expand All @@ -70,3 +62,18 @@ minikube kubectl -- get pods --namespace kube-system`,
}
},
}

// KubectlCommand will return kubectl command with a version matching the cluster
func KubectlCommand(co mustload.ClusterController, args ...string) (*exec.Cmd, error) {
version := co.Config.KubernetesConfig.KubernetesVersion
if version == "" {
version = constants.DefaultKubernetesVersion
}

path, err := node.CacheKubectlBinary(version)
if err != nil {
return nil, err
}

return exec.Command(path, args...), nil
}

0 comments on commit 6d53c00

Please sign in to comment.