Using the power of fzf you can easily build your own version of the kubectx + kubens tools. If you’re not familiar with kubectx and kubens, they are just little utilities that help you change K8s contexts or namespaces without having to type kubectl config set-context <target-context> to change contexts or kubectl config set-context --current --namespace=<target-namespace>.

Admittedly, neither of those two commands is all that complex or hard to remember. But typing kubectx <context> or kubens <namespace> is even easier! Plus, if you have fzf installed it’ll give you a fuzzy-finding search list that helps you select a namespace or context when you have hundreds to choose from.

But sometimes you are in environments where installing them is either impossible or just a pain. This is where a little Shell Wizardry can come and save the day. Or at least make things a little easier.

kubectx

alias kctx="kubectl config get-contexts -o name | fzf | xargs -r kubectl config use-context"

And just like that, you can type kctx and you’ll have your own knockoff kubectx.

kubens

alias kns="kubectl get namespace --output=custom-columns=:metadata.name | grep -v '^$' | fzf | xargs -r kubectl config set-context --current --namespace"

This one is a bit uglier since if you do the same -o name you’ll get items like namespace/default which it will complain about later own the chain.