-
-
Save nicolaballotta/cab3e389e5fb541222677e5b4ec90c7f to your computer and use it in GitHub Desktop.
# INSTALLATION | |
# - Add the following function to your .oh-my-zsh/themes/agnoster.zsh-theme | |
# - Call prompt_kubecontext from build_prompt() as showed in the example below | |
# | |
# | |
# build_prompt() { | |
# prompt_kubecontext | |
# } | |
prompt_kubecontext() { | |
if [[ $(kubectl config current-context) == *"testing"* ]]; then | |
prompt_segment green black "(`kubectl config current-context`)" | |
elif [[ $(kubectl config current-context) == *"staging"* ]]; then | |
prompt_segment yellow black "(`kubectl config current-context`)" | |
elif [[ $(kubectl config current-context) == *"production"* ]]; then | |
prompt_segment red yellow "(`kubectl config current-context`)" | |
fi | |
} |
FWIW I said that because the code you got above will likely add 300-500 ms to each of your prompts because you're calling kubectl
so many times and not caching the result. kubectl is very slow and ideally you shouldn't run it on each prompt. kube-ps1 directly reads the config file and therefore is pretty darn fast.
but if you have to modify the prompt kube-ps1 is not useful, you can use below :
prompt_kubecontext() {
CTX=$(kubectx -c)
CNS=$(kubens -c)
if [[ $CTX == *"tooling"* ]]; then
prompt_segment green black "${CTX}:${CNS}"
elif [[ $CTX == *"nonprod"* ]]; then
prompt_segment yellow black "${CTX}:${CNS}"
elif [[ $CTX == *"prod"* ]]; then
prompt_segment red yellow "${CTX}:${CNS}"
fi
}
FWIW I said that because the code you got above will likely add 300-500 ms to each of your prompts because you're calling
kubectl
so many times and not caching the result. kubectl is very slow and ideally you shouldn't run it on each prompt. kube-ps1 directly reads the config file and therefore is pretty darn fast.
That might be outdated ...
~ » time kubectl config current-context
aks1
kubectl config current-context 0.13s user 0.05s system 137% cpu 0.129 total
EDIT: Ah. I see it now ... the many conditionals in this particular example from OP definitely will benefit from running kubectl
only ONCE
I recommend using https://github.com/jonmosco/kube-ps1/.