-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from 3 commits
f172fec
fe205c2
7fa21b8
41c6dc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,10 @@ package cmd | |
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"syscall" | ||
|
||
|
@@ -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", | ||
|
@@ -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 | ||
|
@@ -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") | ||
kubectlCmd.Flags().BoolVar(&kubectlPathMode, "path", false, "Display kubectl path instead of running it") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without reading the help command I would consider that what do you think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that is one idea. I copied There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think |
||
RootCmd.AddCommand(kubectlCmd) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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