Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 22, 2024
kubectl is the widely-used command-line client for interacting with a Kubernetes cluster. Like other shell commands, its daily use can be laborious without autocompletion due to its numerous commands and complex resources. Autocompletion is a shell feature that helps users save time while reducing the amount of information they need to type.
In this tutorial, we’ll explore how kubectl supports autocompletion and its specific configuration in Zsh.
Using a command-line tool becomes significantly easier with autocompletion. This feature allows to type part of a command, and the shell attempts to complete it based on expected patterns associated with the tool. For instance, when we type kubectl get and press the [Tab] key twice, the shell suggests a list of possibilities such as pods or services.
Autocompletion works through completion scripts written in a shell scripting language. These scripts define the rules used to process user input and the possible outcomes. Tools like kubectl generate these scripts and include them in the tool’s installation files.
Completion scripts always run alongside shell environments like Zsh or Bash. Each shell has its own framework for executing these scripts and suggesting possible commands.
Configuring autocompletion for kubectl provides several benefits:
These advantages make autocompletion an essential tool for Kubernetes administrators and developers.
As mentioned in the previous section, kubectl supports autocompletion in various shells, including Zsh. Zsh is a popular alternative to Bash, optimized for features like autocompletion, along with support for themes and plugins.
For Zsh specifically, kubectl autocompletion is supported only in version 5.2 or later. We can check the installed Zsh version using the following command:
zsh --version
zsh 5.8 (x86_64-ubuntu-linux-gnu)
To enable kubectl autocompletion, we simply generate and load the autocompletion script provided by the tool:
source <(kubectl completion zsh)
This generates the kubectl autocompletion script and dynamically executes it using process substitution syntax. However, this activation is session-specific, meaning the configuration is lost when the shell is closed.
To make the configuration persistent, we can add the same command to the .zshrc configuration file:
echo "source <(kubectl completion zsh)" >> ~/.zshrc
This ensures that each time we open a terminal, the script is automatically generated and loaded into the shell. However, changes made to the ~/.zshrc file don’t immediately take effect in the current session. To apply the updates immediately, we can reload the ~/.zshrc file:
source ~/.zshrc
For users who have installed the Oh My Zsh framework, a popular Zsh configuration framework, an alternative consists of activating the kubectl plugin included in Oh My Zsh.
To do this, we locate the plugins section in the ~/.zshrc file and add kubectl to the list of plugins as follows:
plugins=(other plugins ... kubectl)
This provides the same autocompletion support as in our earlier attempt.
After enabling kubectl autocompletion, we can verify its expected behavior.
In the terminal, we start typing kubectl followed by a space, then press Tab twice. A list of available commands should appear:
kubectl [TAB][TAB]
annotate -- Update the annotations on a resource
api-resources -- Print the supported API resources on the server
api-versions -- Print the supported API versions on the server, in the form of "group/version"
apply -- Apply a configuration to a resource by file name or stdin
attach -- Attach to a running container
auth -- Inspect authorization
autoscale -- Auto-scale a deployment, replica set, stateful set, or replication controller
...
We can perform an additional test by typing a partial command, such as kubectl create, and pressing the Tab key. The shell suggests available resource types such as services, deployments, or configmap:
kubectl create [TAB]
clusterrolebinding -- Create a cluster role binding for a particular cluster role
clusterrole -- Create a cluster role
configmap -- Create a config map from a local file, directory or literal value
cronjob -- Create a cron job with the specified name
deployment -- Create a deployment with the specified name
ingress -- Create an ingress with the specified name
job -- Create a job with the specified name
...
If a beep sound occurs, it indicates no command matches the entered characters. In general, this may also mean that the command execution requires administrator privileges.
If an alias for kubectl is set, autocompletion will work correctly with the alias; for example, if the alias k=kubectl is set, typing k create [TAB] will autocomplete as expected.
Sometimes, we may encounter the error 2: command not found: compdef in the shell because the autocompletion system isn’t properly initialized.
To resolve this, we ensure that the following lines are included anywhere in the ~/.zshrc file to enable autocompletion in Zsh:
autoload -Uz compinit
compinit
This configures and initializes the autocompletion system for the shell.
In this article, we learned how to enable kubectl autocompletion for Zsh using two approaches. By following one of these methods, we can quickly allow the shell to suggest the logical continuation of a kubectl command.
Enabling this shell feature for tools like kubectl simplifies their usage and boosts productivity.