kubectl-plugin_completion
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.0/plugin_completion.yaml
-
Zip file containing binary can be download 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 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: krew
completionFunctionName: _krew
description: A kubectl plugin called krew
- name: hello
completionFunctionName: ""
description: A kubectl plugin called hello
- name: plugin_completion
completionFunctionName: _plugin_completion
description: A kubectl plugin called plugin_completion
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 the completionFunctionName
field will automatically be field in. The description for each plugin will default to: "A kubectl plugin called $plugin_name".
If a plugin was not created by using cobra 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=custom_plugin --completion-function="_completion_function_name"
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.