Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A PS1 with Docker context and Kubernetes context (including namespace)
PS1='(k:$(kubernetes-context) | d:$(docker-context "%s") | g:$(__git_ps1 "%s"))\n\W $ '
function docker-context()
{
if [ -z "$DOCKER_HOST" ]; then
printf "localhost"
else
local HOSTNAME_REGEX=".*//(.*):.*"
[[ $DOCKER_HOST =~ $HOSTNAME_REGEX ]]
printf ${BASH_REMATCH[1]}
fi
}
function kubernetes-context()
{
local KUBERNETES_CONTEXT=$(kubectl config current-context)
local HAS_NAMESPACE=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"$KUBERNETES_CONTEXT"'")].context.namespace}')
local NAMESPACE=${HAS_NAMESPACE:-default}
printf $KUBERNETES_CONTEXT[$NAMESPACE]
}
# __git_ps1 is just the standard __git_ps1 from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment