Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes kubectl bash completion with alias

Tags:

kubernetes

I am using kubectl with bash completion , but I prefer to use a shorter alias for kubectl such as ks , what changes I need to make to get the bash completion work with alias ks

like image 212
Ijaz Ahmad Avatar asked Oct 20 '18 12:10

Ijaz Ahmad


People also ask

How do I enable autocomplete on kubectl?

Type 'echo 'source <(kubectl completion bash)' >>~/. bashrc' command in your command line tool, and you are done.

How do you autocomplete in bash?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

How do I know if bash completion is installed?

If the autocomplete results contain directories only (no files), then Bash Completion is installed. If the autocomplete results include files, then Bash Completion is not installed.


4 Answers

from the official docs

# after installing bash completion

alias k=kubectl
complete -F __start_kubectl k

https://kubernetes.io/docs/reference/kubectl/cheatsheet/#bash

like image 160
endline Avatar answered Oct 12 '22 23:10

endline


A more recent update from the docs:

  • Source the completion script in your ~/.bashrc file:
    echo 'source <(kubectl completion bash)' >>~/.bashrc
    
  • Add the completion script to the /etc/bash_completion.d directory:
    kubectl completion bash >/etc/bash_completion.d/kubectl
    

If you have an alias for kubectl, you can extend shell completion to work with that alias:

echo 'alias ks=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc

You can basically do this:

$ echo "source <(kubectl completion bash | sed 's|__start_kubectl kubectl|__start_kubectl ks|g') >> ~/.bashrc

Out of date:

A slight change from what is described here.

In essence, you are substituting the following in the kubectl completion bash output:

if [[ $(type -t compopt) = "builtin" ]]; then
    complete -o default -F __start_kubectl kubectl
else
    complete -o default -o nospace -F __start_kubectl kubectl
fi

With this:

if [[ $(type -t compopt) = "builtin" ]]; then
    complete -o default -F __start_kubectl ks
else
    complete -o default -o nospace -F __start_kubectl ks
fi
like image 34
Rico Avatar answered Oct 12 '22 23:10

Rico


I have this in my .bashrc to get alias and auto completion.

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

like image 24
Praveen Sripati Avatar answered Oct 13 '22 00:10

Praveen Sripati


Just to complement the awnser of endline (his solution works just works in active session of shell, if you close, you have to reexecute) and maybe help someone with the same trouble I was getting.

You can add it to the shell permanently using the structure above from the documentation

echo 'alias k=kubectl' >>~/.bashrc (add alias to shell)

echo 'source <(kubectl completion bash)' >>~/.bashrc (add completion)

echo 'complete -F __start_kubectl k' >>~/.bashrc (make them work together)

So, now you can execute commands like 'k get logs -n my-namespace -f my-p[TAB]' (to complete name of pod for example) even if close and open a new session.

like image 27
WyllianNeo Avatar answered Oct 12 '22 23:10

WyllianNeo