Kubectl: Cannot autocomplete kubectl bash aliases

Created on 2 Nov 2017  路  33Comments  路  Source: kubernetes/kubectl

The default kubectl complete function __start_kubectl does not work with bash aliases:

# ~/.aliases
alias k="kubectl"
complete -o default -F __start_kubectl k

__Actual output:__

~$ k [TAB]
k kubectl

~$ k k [TAB]
k kubectl

__Expected output:__

~$ k [TAB]
annotate        convert         expose          rolling-update
api-versions    cordon          get             rollout
apply           cp              label           run
attach          create          logs            scale
auth            delete          options         set
autoscale       describe        patch           taint
certificate     drain           plugin          top
cluster-info    edit            port-forward    uncordon
completion      exec            proxy           version
config          explain         replace
help wanted

Most helpful comment

I don't know if this is the best/right solution, but I made k autocomplete by doing source <(kubectl completion bash | sed 's/kubectl/k/g').

All 33 comments

I can confirm the same behavior on my end

I don't know if this is the best/right solution, but I made k autocomplete by doing source <(kubectl completion bash | sed 's/kubectl/k/g').

https://github.com/cykerway/complete-alias is kind of amazing. With this, there is no need to modify the original completion functions, and it works with lazily-loaded completions.

Same issue on my end in OS X.

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

/remove-lifecycle stale

Can someone help to update the vendor to pick up the fix in cobra?

/assign

With Zsh, completion works with aliases as well, but with Bash not. Probably the best workaround is to use complete-alias. Install it by sourcing the main file in your ~/.bashrc. Then, for every alias for which you want to enable completion, add the following to your ~/.bashrc file:

~bash
complete -F _complete_alias my-alias
~

EDIT: if you use macOS, you have to install Bash 4.1+ (brew install bash) and bash-completion 2+ (brew install bash-completion@2) to make complete-alias work.

@weibeld complete-alias doesn't work on Mac. I've spent the last hour trying to make it work, no dice. The author ignored a patch to make it work. I gave it props in a stack exchange discussion, it does work on Linux. But it's not a generalized solution. The more reliable solution is in
https://github.com/spf13/cobra/pull/638.

@briantopping complete-alias works on Mac, but not with Bash 3.2, which is the default on Mac. You can install Bash 5.0 (see for example here) and it will work. It also depends on bash-completion 2.0+ (current version is 2.8).

So, if you don't use outdated tools, like the default Bash on Mac, complete-alias is a general solution too, the only dependencies are Bash 4.1+ and bash-completion 2.0+. I didn't yet try the Cobra solution, but it looks good too.

@weibeld it doesn't work on mac. I followed your instructions here as well as your very elaborated posts on itnext.io but as @briantopping said, no dice.

@nir-logzio what is your version of Bash and bash-completion?

@weibeld
bash: 5.0.2(1)-release
completion: stable 2.8 (bottled)

@weibeld
bash: 5.0.2(1)-release
completion: stable 2.8 (bottled)

So what doesn't work? It's just that the completion of the aliases doesn't work, or is there an error message?

The completion of aliases doesn't work. It starts by adding the original command to the completion instead of its options.

Did you try with different aliases and commands? Maybe you can create a screencast and share it on asciinema.

@weibeld The original description from Nov 2017 describes it perfectly. No need to bother with a screencast

@nir-logzio You did complete -F _complete_alias k (if k is your alias for kubectl) and the output is still like in the original post?

@weibeld Yes. Where _complete_alias is __start_kubectl

@nir-logzio: Give a try to the solution @amalucelli provided above. That's what I am using. Upgrading bash is an option, but one I personally prefer to avoid for every new developer.

@briantopping yes I've considered this option but as it is quite a hack I'm currently planning on trying zsh and perhaps gaining a few more advantages at the same time

"Perfect is the enemy of the good..." :D

@nir-logzio if you do complete -F __start_kubectl k, then obviously it doesn't work, because complete-alias is not used at all. You have to do complete -F _complete_alias k for every alias for which you want to enable completion.

@briantopping with this solution you can only have aliases for the base command, e.g. k for kubectl. But with complete-alias, you can have aliases like alias kg='kubectl get' and completion will propose options and arguments for kubectl get (not the sub-commands of kubectl).

@weibeld: That doesn't matter much if complete-alias doesn't work. Thanks tho.

@briantopping it just doesn't work on Bash 3.2 (which is more than 11 years old), but on every Bash version of 4.1 or greater, it should work perfectly, no matter if it's on Linux or Mac.

/unsubscribe

I don't know if this is the best/right solution, but I made k autocomplete by doing source <(kubectl completion bash | sed 's/kubectl/k/g').

I added this into ~/.bash_profile

I followed official guide and the issue is gone.

https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

What I was missing is

echo 'complete -F __start_kubectl k' >>~/.bashrc

The official instructions are not working for me with bash 4.3 and 4.4. After adding the commands below, completing with kubectl works fine, but k doesn't. If I type 'k', 'space' and then press tab is expands to 'k kubectl' and then if I press tab again it starts completing kubectl commands. Do the official approach only work with bash 5.x?

This does not work, k completes to k kubectl:

source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k

Using complete_alias does work:

source <(kubectl completion bash)
alias k=kubectl
complete -F _complete_alias k

And this workaround of duplicating the completions does also work:

source <(kubectl completion bash)
source <(kubectl completion bash | sed 's/kubectl/k/g')

It should work with any Bash version 4.1+... @whereisaaron do you use macOS or Linux, and which version of bash-completion do you have installed?

@whereisaaron Does this return anything for you?

declare -F | grep __start_kubectl

EDIT: Actually, what is your output for this?

declare -f __start_kubectl

I came up with this to autocomplete common aliases kgp kl kex ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adambro picture adambro  路  3Comments

fiksn picture fiksn  路  3Comments

drpaneas picture drpaneas  路  3Comments

alkar picture alkar  路  6Comments

cbluth picture cbluth  路  6Comments