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

Allow zsh completion to be autoloaded by compinit #50561

Merged
merged 2 commits into from
Aug 29, 2017

Conversation

cblecker
Copy link
Member

@cblecker cblecker commented Aug 12, 2017

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 #50560

Special notes for your reviewer:

Release note:

kubectl zsh autocompletion will work with compinit

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 12, 2017
@cblecker
Copy link
Member Author

/sig cli

@k8s-ci-robot k8s-ci-robot added the sig/cli Categorizes an issue or PR as relevant to SIG CLI. label Aug 12, 2017
@k8s-github-robot k8s-github-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 12, 2017
@cblecker
Copy link
Member Author

/retest

@cblecker
Copy link
Member Author

@kubernetes/sig-cli-pr-reviews

@sttts
Copy link
Contributor

sttts commented Aug 14, 2017

@cblecker lgtm. Can you add an example to the kubectl help text how to autoload it?

@cblecker
Copy link
Member Author

@sttts Updated. PTAL!

@k8s-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 14, 2017
@cblecker
Copy link
Member Author

/retest

@fabianofranz
Copy link
Contributor

/assign @juanvallejo

@cblecker
Copy link
Member Author

/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
Copy link
Contributor

@sttts sttts Aug 15, 2017

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 ?

Copy link
Member Author

@cblecker cblecker Aug 15, 2017

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
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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"`))
Copy link
Contributor

Choose a reason for hiding this comment

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

and again.

Copy link
Member Author

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

Copy link
Contributor

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.

Copy link
Member Author

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.

@juanvallejo
Copy link
Contributor

lgtm pending @sttts 's comments

Copy link
Contributor

@alexandercampbell alexandercampbell left a 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
Copy link
Contributor

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.

func runCompletionZsh(out io.Writer, kubectl *cobra.Command) error {
func runCompletionZsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) error {
zsh_head := `#compdef kubectl
`
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great suggestion. Fixed.

@cblecker
Copy link
Member Author

/retest

@cblecker
Copy link
Member Author

/retest

@cblecker
Copy link
Member Author

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.

@sttts
Copy link
Contributor

sttts commented Aug 23, 2017

@cblecker quick benchmark: source <(kubectl completion bash) 140ms, source stored-completion 36ms. Clear winner.

Let's go forward with this. /lgtm

@cblecker
Copy link
Member Author

Heh, yeah that's a pretty large difference. That work for you too, @juanvallejo?

@juanvallejo
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 23, 2017
@cblecker
Copy link
Member Author

/assign @dchen1107
for approval

@cblecker
Copy link
Member Author

/assign @fabianofranz
for approval if you have time

@fabianofranz
Copy link
Contributor

/approve

@k8s-github-robot
Copy link

[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 /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 28, 2017
@cblecker
Copy link
Member Author

/test pull-kubernetes-verify
godep download failed. trying again

@cblecker
Copy link
Member Author

/retest

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to @fejta).

Review the full test history for this PR.

@cblecker
Copy link
Member Author

/retest

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 51471, 50561, 50435, 51473, 51436)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kubectl zsh autocompletion isn't loaded on first tab by compinit
10 participants