Simpler Cluster/User/Namespace switching for Kubernetes
(featuring interactive mode and wildcard/fuzzy matching (among other things)).
In short, instead of
kubectl config get-clusters
kubectl config view -o=go-template --template=$'{{range $u := .users}}{{$u.name}}\n{{end}}'
kubectl get --cluster=<cluster> --user=<user> namespaces
kubectl config set-context <context_name> --cluster=<cluster> --user=<user> --namespace=<namespace>
kubectl config use-context <context_name>
context can be changed with kubensx use user:cluster/namespace
or simply kubensx use
(interactive).
curl -sSL https://github.com/shyiko/kubensx/releases/download/0.2.0/kubensx-0.2.0-$(
bash -c '[[ $OSTYPE == darwin* ]] && echo darwin || echo linux'
)-amd64 -o kubensx && chmod a+x kubensx && sudo mv kubensx /usr/local/bin/
Verify PGP signature (optional but recommended):
curl -sSL https://github.com/shyiko/kubensx/releases/download/0.2.0/kubensx-0.2.0-$(
bash -c '[[ $OSTYPE == darwin* ]] && echo darwin || echo linux'
)-amd64.asc -o kubensx.asc
curl -sS https://keybase.io/shyiko/pgp_keys.asc | gpg --import
gpg --verify kubensx.asc /usr/local/bin/kubensx
macOS:
gpg
can be installed withbrew install gnupg
Download executable from the Releases page.
# change <user>:<cluster>/<namespace> (interactive)
$ kubensx use
# change <namespace> only (interactive)
$ kubensx use -n
# switch to <user>:<cluster>/<namespace>
$ kubensx use minikube:minikube/default
# switch to a different <namespace> within current <cluster> (<user> stays the same)
$ kubensx use kube-public
# context matching is wildcard-ish by default, which means you don't have to type the whole thing
# if there are two or more options available - you'll be asked to select one
$ kubensx use west/def
Switched to account@possibly-gmail.com:us-west1/default
# prefer fuzzy?
$ kubensx use -z us1/dfl
Switched to account@possibly-gmail.com:us-west1/default
# switch to previous context
$ kubensx use -
# print current context
$ kubensx current
minikube:minikube/default
# list <user>s
$ kubensx ls -u
# list <cluster>s
$ kubensx ls -c
# list <namespace>s (inside current <cluster>)
$ kubensx ls -n
(for more information see
kubensx --help
)
By default, any <user>
can be used with any <cluster>
.
If you want to restrict (assoc[iate]) certain user(s) to some of the clusters use
$ kubensx assoc
For example: if you have a "minikube" user which you only use in the context of local "minikube" cluster,
you may want to kubensx assoc minikube:minikube
(<user>:<cluster>
) so that "minikube"
wouldn't be shown among the users for any cluster other than "minikube" (when kubesec use
ing).
If a user is not allowed to list namespaces, you can either provide a list of namespaces known to that user with ns-list
$ kubensx ns-list
# make default, kube-system and kube-public namespaces known to current user in us-west1 cluster
$ kubensx ns-list us-west1/{default,kube-system,kube-public}
$ kubensx use west/def
Switched to account@possibly-gmail.com:us-west1/default
or use --force(-f) to suppress namespace validation (namespace will have to be provided --exact|ly)
$ kubensx use west/default --force
Switched to account@possibly-gmail.com:us-west1/default
# bash
$ source <(kubensx completion bash)
# zsh
$ source <(kubensx completion zsh)
PREREQUISITE: go1.9+.
git clone https://github.com/shyiko/kubensx $GOPATH/src/github.com/shyiko/kubensx
cd $GOPATH/src/github.com/shyiko/kubensx
make fetch
go run kubensx.go
All code, unless specified otherwise, is licensed under the MIT license.
Copyright (c) 2018 Stanley Shyiko.