Awesome
kubectl-plugin_completion
:warning: Notice
- This has been tested on for kubectl v1.23 and has not been tested on the kubectl v1.24.
- Not all kubectl plugins have been tested. You can find a listed of tested plugins at here. Please note if a plugin hasn't been tested it might cause unwanted errors such as your shell hanging.
- Plugin only supports zsh and bash shells (for now).
Auto completion saves a lot of time when typing in commands. The kubectl tool has a way to allow for completion for it's sub-commands, however it doesn't not accomodate for when these sub-commands are plugin commands. This repo contains the code for a kubectl plugin that generates scripts for extending the kubectl tool in order to accomodate for completions for kubectl plugin sub-commands.
Sections
Install plugin
Plugin can be installed in 3 ways:
- Via krew
$ kubectl krew install --manifest-url https://github.com/MartinSimango/kubectl-plugin_completion/releases/download/v0.1.2/plugin_completion.yaml
-
Zip file containing binary can be downloaded on the release page
-
Manually by cloning the repo and running make install - (This requires you having go installed)
How it works
The plugin works by reading config files for specfic shells (so far only bash and zsh). These config files in a folder located at $HOME/.kube/plugin-completion-config
and are named $SHELL_NAME.yaml
. So for example the zsh config file will be located at $HOME/.kube/plugin-completion-config/zsh.yaml
.
The config files contain information about the available kubectl plugins along with some additional information such as the plugin's description and the plugin's completion function name. Below is an example of what a config file for a zsh terminal will look like:
shell: zsh
shellLocation: /bin/zsh
plugins:
- name: access_matrix
completionFunctionName: ""
description: 'Show an RBAC access matrix for server resources '
supportsCobraCompletion: false
- name: accurate
completionFunctionName: _accurate
description: 'Manage Accurate, a multi-tenancy controller '
supportsCobraCompletion: true
- name: allctx
completionFunctionName: ""
description: 'Run commands on contexts in your kubeconfig '
supportsCobraCompletion: false
- name: plugin_completion
completionFunctionName: _plugin_completion
description: A kubectl plugin for allowing shell completions for kubectl plugins
supportsCobraCompletion: true
kubectlOverridePlugins:
- allctx
The above file can be generated by running:
$ kubectl plugin_completion config generate
This will generate config files for all supported shells (zsh and bash). For plugins that were created using cobra and have that have a cobra completion command the completionFunctionName
field will automatically be field in. The description for each plugin will default to: "A kubectl plugin called $plugin_name" if the plugin is not found in the krew repository.
If a plugin was not created by using cobra or if the plugin was created using cobra but does not have a completion subcommand, it will be up to you to manually edit the config file and provide the name of the plugin's completion function name. This can also be done by running the kubectl plugin_completion config edit
command as shown below:
$ kubectl plugin_completion config edit zsh --plugin-name=plugin_name --completion-function="_completion_function_name"
Plugins who's completion script override the default kubectl completion function such as the allctx plugin can be added to the kubectlOverridePlugins array. Thus typing kubectl allctx <TAB><TAB>
will still give completions for the kubectl command.
The plugin_completion uses config files to generate completions specific shells in order to allow for completions for kubectl plugins. The generated completion scripts overwrite the behaviour of the completion function for the kubectl tool and extend it to allow for kubectl plugin completions.
How to use
To start of you will need to generate your shell plugin config files. This can be done by running the below command:
$ kubectl plugin_completion config generate
Once the config files are generated you can source the plugin completion script by running:
$ source <(kubectl plugin_completion plugin-completion $SHELL)
where $SHELL
is the shell you want to generate the completion script for.
To make the changes permanent you can add source <(kubectl plugin_completion plugin-completion zsh)
to your ~/.zshrc
file or source <(kubectl plugin_completion plugin-completion bash)
to your ~/.bashrc (or ~/.bash_profile)
file.
Please note if you change the config files for a shell you will need to rerun the source <(kubectl plugin_completion plugin-completion $SHELL)
command as the plugin completion script is generated from using information from the config files.
Once the above is done completions for your plugins should work as displayed in the below Example section.