Created
June 21, 2017 06:11
-
-
Save StephanX/1f64658c736be6f9144b5b936e63fb4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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> |
Perhaps
# Get current context
__kube_ps1() {
# Make colors available
NORMAL="\[\033[00m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\e[1;33m\]"
GREEN="\[\e[1;32m\]"
CONTEXT=$(kubectl config current-context)
NAMESPACE=$(kubectl config view --minify --output 'jsonpath={..namespace}')
if [ -n "$CONTEXT" ]; then
# Parse off user from context, adjust if this is not desired
IFS='@' read -ra ADDR <<< $CONTEXT
# Set prompt
export PS1="[${GREEN}${ADDR[0]}${NORMAL}: ${BLUE}${NAMESPACE}${NORMAL}]\n[\w]\n[\u@\H]\$ "
else
# Set prompt
export PS1="[\w]\n[\u@\H]\$ "
fi
}
PROMPT_COMMAND="__kube_ps1"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have to re-source the script every time you switch to another context ?