Skip to content

Instantly share code, notes, and snippets.

@sirhopcount
Forked from StephanX/kube-bash-prompt.sh
Created August 1, 2017 07:45
Show Gist options
  • Save sirhopcount/15289a3225368fac35f56e5988187bfc to your computer and use it in GitHub Desktop.
Save sirhopcount/15289a3225368fac35f56e5988187bfc to your computer and use it in GitHub Desktop.
#!/bin/bash
# configures bash prompt based on current kubernetes cluster context and namespace
# based on https://pracucci.com/display-the-current-kubelet-context-in-the-bash-prompt.html
__kube_ps1()
{
CONTEXT=$(kubectl config view | grep current-context | awk '{ print $NF }' | awk -F '.' '{ print $1 }')
NS=$(kubectl config view $(kubectl config current-context) | grep namespace | awk '{ print $2 }')
if [ -n "$CONTEXT" ]; then
echo "[${CONTEXT}][${NS}]"
fi
}
NORMAL="\[\e[00m\]" # Normal
BLACK="\[\e[0;30m\]" # Black
RED="\[\e[1;31m\]" # Red
GREEN="\[\e[0;32m\]" # Green
YELLOW="\[\e[0;33m\]" # Yellow
BLUE="\[\e[0;34m\]" # Blue
PURPLE="\[\e[0;35m\]" # Purple
CYAN="\[\e[0;36m\]" # Cyan
WHITE="\[\e[0;37m\]" # White
export PS1="${BLUE}\h:${PURPLE}\$(__kube_ps1) ${BLUE}\W> ${NORMAL}"
# results:
# Cronos:[dev1][qa1] mydirectory>
# || ||
# Hostname:[cluster][namespace] currentdir>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment