I recently wrote an article about command line completion for Docker. This is the parallel article for Kubernetes. The TLDR for command line completion in Docker was that it probably already works just fine. The same is not true for Kubernetes but is easily fixed.
Overall, the installation process for Kubernetes is still slightly complicated and there are many vendors making their own Kubernetes packages. Unfortunately the majority of the effort is focussed on installing and configuring the server-side components of Kubernetes. This makes sense given the complexity involved, but it leaves users of the command line client out of luck. There’s good news though as it’s very easy to install the pieces needed for command line completion for Kubernetes.
We can divide the process of getting command line completion for Kubernetes working into three steps.
Step #1: Install bash-completion package
The first step is to install the command line completion package. This differs by operating system.
Mac OS X
Firstly you must install the Homebrew package manager, if you haven’t done this already on your Mac. Install the bash-completion package to install the pieces necessary to make completion work:
$ brew install bash-completion
For it to actually work you need to add a snippet of code to your local bash configuration. Add the following text to the end of your $HOME/.bash_profile file:
if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi
It’s very likely that the bash-completion package for Linux operating systems is already installed.
$ sudo apt-get install bash-completion
There’s no need to modify your $HOME/.bash_profile either as it’s automatically configured, but you may need to log out and log in again for completion to start working.
Luckily the package is identically named for RPM-based distributions as it is for Debian. Install the bash-completion package using yum:
$ sudo yum install bash-completion
Again there is no need to modify your $HOME/.bash_profile as it’s automatically configured, but you may need to log out and log in again for completion to start working.
Step #2: Add kubectl configuration
The kubectl executable generates its own completions file dynamically via the “kubectl completions” command instead of having a pre-generated completions file like most other packages. There are several ways of adding these completions to your system.
To add completions just for yourself add the following to your $HOME/.bash_profile file. This is useful if you don’t have root access to the system you are using.
$ cat >> $HOME/.bash_profile << __EOF__ source <(kubectl completion bash) __EOF__
To add completions for everyone on your system create the following file in /etc/bash_completion.d/kubectl:
# cat > /etc/bash_completion.d/kubectl << __EOF__ source <(kubectl completion bash) __EOF__
Note that you might have to remove this file if you ever uninstall the kubectl executable.
Step #3 : Test
Testing is a simple step. Run “kubectl” and hit TAB to see whether commands are completed:
$ kubectl <TAB> annotate convert expose rollout api-versions cordon get run apply cp l abel scale [...]
Command line completions for Kubernetes are a little more complicated than for Docker but they’re worth putting in a bit of effort to configure. Hopefully this extra configuration won’t be necessary in the future as vendors and Linux distributions improve their packaging of Kubernetes.