Skip to content

Instantly share code, notes, and snippets.

@naingyeminn
Last active August 2, 2022 02:46
Show Gist options
  • Save naingyeminn/902d586138f6400d3c4ef5d357758067 to your computer and use it in GitHub Desktop.
Save naingyeminn/902d586138f6400d3c4ef5d357758067 to your computer and use it in GitHub Desktop.
Switch K8S cluster context easily
#!/bin/bash
# run `kubeswitch` to list the contexts from .kube/config and set current context
# run `source <(kubeswitch)` to use the selected context in active shell environment only
# or add `alias kswitch='source <(kubeswitch)'` in ~/.bashrc
unset KUBECONFIG
select context in $(kubectl config get-contexts -o name); do
kubectl config use-context $context &> /dev/null
kubectl config view --minify --context $context --flatten > ${HOME}/.kube/$context.conf
chmod 600 ${HOME}/.kube/$context.conf
echo "export KUBECONFIG=${HOME}/.kube/$context.conf"
break
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment