Last active
August 13, 2020 10:31
-
-
Save koenbollen/342c138b6a77d3de6869aad68c193617 to your computer and use it in GitHub Desktop.
kns - Quick kubectl namespace switcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# kns lets you select a kubernetes namespace in seconds. Autocomplete, | |
# type-ahead, everything! (tested in bash and zsh) | |
kns() { | |
local current | |
local namespace | |
local selected | |
if [[ ! -x "$(which fzf 2>/dev/null)" ]]; then | |
echo "please install: github.com/junegunn/fzf" >&2 | |
return 1 | |
fi | |
current="$(kubectl config current-context)" | |
namespace="$(kubectl config get-contexts "$current" | awk "/$current/ {print \$5}")" | |
if [[ -z "$namespace" ]]; then | |
namespace="default" | |
fi | |
selected=$( (kubectl get namespaces -o name | sed "s-namespaces/--"; echo $namespace ) | fzf -0 -1 --tac --prompt "$current> ") | |
if [[ ! -z "$selected" ]]; then | |
kubectl config set-context "$current" "--namespace=$selected" | |
echo "Set to \"$selected\"." | |
fi | |
} | |
# I love this alias: | |
#alias k=kubectl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For it to work with Kubectl 1.6 you will need to update this line https://gist.github.com/koenbollen/342c138b6a77d3de6869aad68c193617#file-kns-bash-L18 to
Remove the
s
fromnamespaces
when yoused
👍