The kubectl completion
command in Kubernetes is used to generate shell completion code for the specified shell (bash or zsh). This feature helps in enhancing the usability of the kubectl
command-line tool by enabling auto-completion of commands, options, and arguments, thereby improving efficiency and reducing errors during command entry.
Here are several examples demonstrating how to use kubectl completion
:
Example 1: Generating bash completion
To generate bash completion code for kubectl
, use the following command:
kubectl completion bash
This command outputs the bash completion script to standard output. To enable it permanently, you can save the script to a file and source it in your shell’s profile (e.g., .bashrc
or .bash_profile
).
Example 2: Generating zsh completion
To generate zsh completion code for kubectl
, execute:
kubectl completion zsh
Similar to bash, this command outputs the zsh completion script. To activate it permanently, add the script to your .zshrc
file.
Example 3: Applying bash completion directly
To enable bash completion for kubectl
without saving to a file, use:
source <(kubectl completion bash)
This command directly applies the completion script to your current bash session.
Example 4: Checking bash completion status
You can verify if bash completion for kubectl
is enabled by typing part of a command and pressing the tab key. If completion is working, you should see suggestions for completing the command or its options.
Example 5: Applying zsh completion directly
To apply zsh completion directly to your current shell session, use:
source <(kubectl completion zsh)
This command loads the zsh completion script inline, enabling completion immediately.
Example 6: Verifying zsh completion status
To check if zsh completion for kubectl
is active, type part of a command and press tab. Zsh should provide completion suggestions if it’s working correctly.
Example 7: Generating completion for a specific kubectl version
To generate completion code tailored for a specific version of kubectl
, specify the version with the --kubernetes-version
flag:
kubectl completion bash --kubernetes-version=v1.22.0
This command ensures that the generated completion script is compatible with the specified Kubernetes version.
Example 8: Using with kubectl plugins
If you have installed any kubectl plugins, you can generate completion code that includes commands from those plugins by adding the --include-cs-plugins
flag:
kubectl completion bash --include-cs-plugins
This command incorporates completion for both core kubectl commands and any installed plugins.
Example 9: Generating completion for a specific plugin
To generate completion code specifically for a kubectl plugin (here assumed as my-plugin
), use:
kubectl completion bash --plugin=my-plugin
This command generates completion code that includes commands and options from the specified plugin.
Example 10: Checking completion for a plugin
You can verify if completion for a specific plugin (e.g., my-plugin
) is active by typing part of a plugin command and pressing tab. Zsh or bash should suggest completions relevant to the plugin.
These examples illustrate the flexibility of kubectl completion
in enhancing shell interaction with Kubernetes commands, ensuring efficient and accurate command-line usage.
Also check similar articles.
Managing Kubernetes Annotations with kubectl annotate
Updating Kubernetes Labels with kubectl label
Building Kubernetes Customizations with kubectl kustomize
Waiting for Kubernetes Resources with kubectl wait
Replace Kubernetes Resources with kubectl replace
Discussion about this post