A simpler Kubernetes context awareness helper for bash and zsh
Complements the kubectx and kubens tools by ahmetb
Usage: kube[un]aware [-g | --global] [-h | --help]
With no arguments, turn on/off kubeaware for this shell instance instance (default).
-g --global turn on kubeawareness globally
-h --help print this message
source kubeaware.sh
export PS1="[your prompt] \$(kubeaware_prompt)$ "
source kubeaware.sh
PROMPT='$(kubeaware_prompt) '$PROMPT
Include this in your ~/.bashrc
or ~/.zshrc
to load each time you start a new shell
Add the following line after source
, if you want to remove the Kubernetes-logo:
unset KUBE_SYMBOL PRE_SYMBOL POST_SYMBOL
When kubeaware.sh is sourced, mainly two things happen:
- You load a set of helper functions, most importantly
kubeaware
which is used in your$PS1
environment variable - The environment variable
$PROMPT_COMMAND
in bash is patched with the function that gets the context info from Kubernetes. For ZSH the functionality is added via add-zsh-hook precmd.
The function(s) included in $PROMPT_COMMAND
is executed each time your shell runs a command.
The function included by kubeaware will fetch the information from the KUBECONFIG
file (via kubectl config
). This will only happen if KUBECONFIG
has changed since last time it was checked.
Heavily inspired by kube-ps1 by jonmosco, and aims to contain only a subset of the features
Has not been tested versions of bash < 4.x, so there might be compatability issues if you are running an older version