When working with multiple Kubernetes clusters, you need to know which cluster you're operating on. Accidentally issuing a command to the wrong cluster can be disastrous!

Instead of remembering what's currently set in KUBECTL_CONTEXT, I inject it into my shell's prompt using this function:

1# ~/.zshrc
2__kube_ps1()
3{
4    KUBECTL_CONTEXT=$(kubectl config current-context)
5    if [ -n "$KUBECTL_CONTEXT" ]; then
6        export PS1="(kubectl: ${KUBECTL_CONTEXT}) "
7    fi
8}
9alias kubectx=__kube_ps1

I can then invoke that function to change my session's PS1 to the current context set in KUBECTL_CONTEXT:

1$ kubectx
2(kubectl: do-sfo3-k8s-1-24-4-do-0-sfo3-...) kubectl get nodes
3NAME                   STATUS   ROLES    AGE   VERSION
4pool-j0w2l5hgr-mcggw   Ready    <none>   8m    v1.24.4