-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Allow zsh completion to be autoloaded by compinit #50561
Conversation
/sig cli |
/retest |
@kubernetes/sig-cli-pr-reviews |
@cblecker lgtm. Can you add an example to the kubectl help text how to autoload it? |
@sttts Updated. PTAL! |
/retest |
/assign @juanvallejo |
/retest |
brew install bash-completion@2 | ||
## If kubectl is installed via homebrew, this should start working immediately. | ||
## If you've installed via other means, you may need add the completion to your completion directory | ||
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl |
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.
this is a bad hint. Can we update it to: echo 'source <(kubectl completion bash)' > $(brew --prefix)/etc/bash_completion.d/kubectl
?
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.
this actually isn't a bad hint.. if you pipe the output to this file, it will automatically be loaded by bash_completion on start
ref: https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion
## Load the kubectl completion code for bash into the current shell | ||
source <(kubectl completion bash) | ||
## Write bash completion code to a file and source if from .bash_profile | ||
kubectl completion bash > ~/.kube/completion.bash.inc |
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.
same here. This is a bad hint.
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.
This isn't a bad hint either. You're piping the output to a file, and then sourcing it. This is actually just reformatting what was already there.
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.
We shouldn't store the output, but call source <(kubectl completion bash)
. Otherwise, people will complain on the next kubectl update that things break.
source <(kubectl completion zsh)`)) | ||
source <(kubectl completion zsh) | ||
# Set the kubectl completion code for zsh[1] to autoload on startup | ||
kubectl completion zsh > "${fpath[1]}/_kubectl"`)) |
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.
and again.
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.
This is actually the step you do to load the kubectl completion on startup in zsh. zsh looks for a compdef in it's fpath
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 like this compdef approach. Just wondering whether we can combine it with source <(kubectl completion zsh)
, i.e. not storing the output of kubectl, but sourcing it again when zsh loads the kubectl completion. The usecase I have in mind is that of kubectl which comes either via some packaging system or via the gcloud cli. I would bet people forget to update their completion when a new kubectl is installed.
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.
My concern is that both the bash completion FAQ and the zsh docs suggest that the preferred thing to do is to store them in a file. I agree that sourcing directly from the application keeps it up to date, but it could also be a performance issue loading the functions into memory instead of loading them as needed from files dynamically.
lgtm pending @sttts 's comments |
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.
Minor comments
|
||
$ source $(brew --prefix)/etc/bash_completion | ||
Detailed instructions on how to do this are available here: | ||
https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion |
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.
+1 to having a single source of truth here.
pkg/kubectl/cmd/completion.go
Outdated
func runCompletionZsh(out io.Writer, kubectl *cobra.Command) error { | ||
func runCompletionZsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) error { | ||
zsh_head := `#compdef kubectl | ||
` |
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.
Why not replace this line with this?
zshHead := "#compdef kubectl\n"
Easier to read and won't annoy the linter.
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.
Great suggestion. Fixed.
/retest |
/retest |
How should we proceed on this @sttts? I agree with your concern about keeping the definition up to date, and with the compdef change of this PR both will work.. but I'm concerned about making a recommendation that does against the docs of the completion frameworks themselves. I'm unsure what the best balance of the two concerns is. |
@cblecker quick benchmark: Let's go forward with this. /lgtm |
Heh, yeah that's a pretty large difference. That work for you too, @juanvallejo? |
/lgtm |
/assign @dchen1107 |
/assign @fabianofranz |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: cblecker, fabianofranz, juanvallejo Associated issue: 50560 The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these OWNERS Files:
You can indicate your approval by writing |
/test pull-kubernetes-verify |
/retest |
/retest Review the full test history for this PR. |
/retest |
Automatic merge from submit-queue (batch tested with PRs 51471, 50561, 50435, 51473, 51436) |
What this PR does / why we need it:
Allows the kubectl zsh autocompletion to be auto loaded by compinit. Had to move the the boilerplate down into the specific shell functions as the compdef needs to be the first line in the definition file.
Which issue this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close that issue when PR gets merged): fixes #50560Special notes for your reviewer:
Release note: