Skip to content

Blazing fast kubectl completion

You can use cache_builder from kubectl-fzf to speed-up completion and use a systemd unit to start it on login.

The premise

At the time of the writing, my .kube/config has a dozen of clusters configured; some of them are on slow networks.

Striking the tab key with kubectl can lead to very slow completion and freeze the terminal till the completion functions finish.

The solution

A simple web search on kubectl cache led me to kubectl-fzf , which uses fzf to search from a cache.

kubectl-fzf can be installed locally or on the target cluster. Since I'm dealing with plenty of ephemeral clusters I opted for the local approach.

1. Load kubectl-fzf as an oh-my-zsh plugin

Copy kubectl_fzf.plugin.zsh under ~/.oh-my-zsh/plugins/kubectl_fzf.

Replace the first line to put the cache in the home directory:

sed -i '1 s@export KUBECTL_FZF_CACHE@export KUBECTL_FZF_CACHE=$HOME/tmp/kubectl_fzf_cache@' \
  ~/.oh-my-zsh/plugins/kubectl_fzf

2. Add a systemd service for the local user

Add the following config to $HOME/.local/share/systemd/user/cache-builder.service:

[Unit]
Description=kubectl-fzf cache builder
After=network.target

[Service]
Type=simple
ExecStart=%h/go/bin/cache_builder -dir %h/tmp/kubectl_fzf_cache
Restart=always

[Install]
WantedBy=default.target

Enable the service:

systemctl --user enable cache-builder.service
systemctl --user restart cache-builder.service
systemctl --user status cache-builder.service

systemctl --user restart cache-builder is useful to force rebuilding the cache when you switch kubeconfig.

Conclusion

The cache_builder from kubectl-fzf, fzf itself & systemd are magic!

There is still plenty to tune like: