A fast kubectl autocompletion with fzf
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd/cache_builder Update version Feb 9, 2019
pkg
tests Remove python files Dec 16, 2018
.gitignore Update readme Dec 16, 2018
LICENSE Add license Nov 20, 2018
README.md Add oh-my-zsh suggested zstyle update Feb 11, 2019
go.mod Update go mod/sum Dec 20, 2018
go.sum Update go mod/sum Dec 20, 2018
kubectl_fzf.bash
kubectl_fzf.plugin.zsh
kubectl_fzf.sh Adding daemonset support Feb 15, 2019
setup.py Added tests Nov 24, 2018

README.md

Kubectl-fzf

kubectl-fzf provides a fast and powerful fzf autocompletion for kubectl.

asciicast

Table of Contents

Features

  • Seamless integration with kubectl autocompletion
  • Sub second completion
  • Label autocompletion
  • Automatic namespace switch

Requirements

fzf needs to be installed

Installation

Install cache_builder

go get -u github.com/bonnefoa/kubectl-fzf/cmd/cache_builder

Source the autocompletion functions

# kubectl_fzf.sh needs to be sourced after kubectl completion.

# bash version
echo "source <(kubectl completion bash)" >> ~/.bashrc
echo "source $GOPATH/src/github.com/bonnefoa/kubectl-fzf/kubectl_fzf.sh" >> ~/.bashrc

# zsh version
echo "source <(kubectl completion zsh)" >> ~/.zshrc
echo "source $GOPATH/src/github.com/bonnefoa/kubectl-fzf/kubectl_fzf.sh" >> ~/.zshrc

Using zplug

You can use zplug to install the autocompletion functions

zplug "plugins/kubectl", from:oh-my-zsh, defer:2
zplug "bonnefoa/kubectl-fzf", defer:3

Usage

cache_builder

cache_builder will watch cluster resources and keep the current state of the cluster in local files. By default, files are written in /tmp/kubectl_fzf_cache (defined by KUBECTL_FZF_CACHE) To create cache files necessary for kubectl_fzf, just run in a tmux or a screen

cache_builder

It will watch the cluster in the current context. If you switch context, cache_builder will detect and start watching the new cluster. The initial resource listing can be long on big clusters and autocompletion might need 30s+.

To launch with debug logs activated

cache_builder -logtostderr -v 14

Watch a specific namespace

To create cache for a specific namespace, just run

cache_builder -n mynamespace

kubectl_fzf

Once cache_builder is running, you will be able to use kubectl_fzf by calling the kubectl completion

kubectl get pod <TAB>

fzf options

You can control the options used for fzf with KUBECTL_FZF_OPTIONS variable.

For example, to turn down exact match in search:

export KUBECTL_FZF_OPTIONS=(-1 --header-lines=2 --layout reverse)

Caveats

With zsh, if the suggested completion doesn't match the start of the query, the completion will fail.

k get pod pr<TAB>
# result needs to starts with pr, otherwise, it will fail

If you're using an out-of-the-box oh-my-zsh configuration, the default matcher-list zstyle (zstyle ':completion:*' matcher-list 'r:|=*' 'l:|=* r:|=*') will interfere with the search. If fzf does not find any match, or if you interrupt it by pressing Esc or Ctrl-C/Cmd-C, zsh will see it as a failed completion and will restart it again.

Changing the zstyle to zstyle ':completion:*' matcher-list 'r:|=*' fixes the issue.