Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an install option to kubectl command #4698

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"

Expand All @@ -32,6 +34,14 @@ import (
"k8s.io/minikube/pkg/minikube/machine"
)

var (
kubectlInstallMode bool
kubectlInstallPrefix string
kubectlPathMode bool
)

const defaultPrefix = "/usr/local"

// kubectlCmd represents the kubectl command
var kubectlCmd = &cobra.Command{
Use: "kubectl",
Expand Down Expand Up @@ -65,6 +75,16 @@ var kubectlCmd = &cobra.Command{
exit.WithError("Failed to download kubectl", err)
}

if kubectlInstallMode {
installKubectl(binary, version, path, kubectlInstallPrefix)
return
}

if kubectlPathMode {
console.OutLn(path)
return
}

glog.Infof("Running %s %v", path, args)
c := exec.Command(path, args...)
c.Stdin = os.Stdin
Expand All @@ -84,6 +104,33 @@ var kubectlCmd = &cobra.Command{
},
}

func installKubectl(binary, version, path, prefix string) {
bindir := filepath.Join(prefix, "bin")
installpath := filepath.Join(bindir, binary)
glog.Infof("Installing %s (%s)", installpath, version)

data, err := ioutil.ReadFile(path)
if err != nil {
exit.WithError("Failed to read kubectl", err)
}
err = os.MkdirAll(bindir, 0755)
if err != nil {
exit.WithError("Failed to create directory", err)
}
err = ioutil.WriteFile(installpath, data, 0755)
if err != nil {
if os.IsPermission(err) {
// Permission denied (common error) is *not* a crash...
console.Fatal("%s: %v", "Failed to write kubectl", err)
os.Exit(exit.Software)
}
exit.WithError("Failed to write kubectl", err)
}
}

func init() {
kubectlCmd.Flags().BoolVar(&kubectlInstallMode, "install", false, "Install kubectl and exit")
kubectlCmd.Flags().StringVar(&kubectlInstallPrefix, "prefix", defaultPrefix, "Installation prefix")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this default prefix be a problem for windows?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but haven't used anything but MSYS myself.

Copy link
Member

@josedonizetti josedonizetti Jul 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not a windows user as well. It worries me that downloading kubeclt currently works on all platforms, and now considering this default prefix it will break in windows. Does it make sense to only use the prefix when in non-windows systems? It basically does the previous behavior for windows, and we add an issue for someone else to implement the windows support.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add another prefix (already have a suffix...), just not sure what it would be

kubectlCmd.Flags().BoolVar(&kubectlPathMode, "path", false, "Display kubectl path instead of running it")
Copy link
Member

@josedonizetti josedonizetti Jul 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without reading the help command I would consider that path would be the place to install, pretty much like prefix.

what do you think about --which cos it does the same thing as which kubectl, right? probably wouldn't make the same sense for a windows user.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is one idea. I copied --url (from dashboard), and then it became --path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think --which can be misleading, since it is normally associated with PATH (which it is not in). Will go for something like --print, and hope that there is no confusion.

RootCmd.AddCommand(kubectlCmd)
}