feat(kube-ps1): add environment indicator to the prompt layout

This commit is contained in:
Oleksandr Molchanov 2026-03-13 20:27:33 +02:00
commit 259ee26071
2 changed files with 86 additions and 0 deletions

View file

@ -127,6 +127,10 @@ The default prompt layout is:
```sh ```sh
(<symbol>|<context>:<namespace>) (<symbol>|<context>:<namespace>)
``` ```
You can enable kube environment context (like dev/staging/test/prod) via `KUBE_ENV_CTX_ENABLE=true`. In this case, the layout will be:
```sh
[<kube-environment>] (<symbol>|<context>:<namespace>)
```
If the current-context is not set, kube-ps1 will return the following: If the current-context is not set, kube-ps1 will return the following:
@ -204,6 +208,23 @@ the following variables:
| `KUBE_PS1_CTX_COLOR_FUNCTION` | No default, must be user supplied | Function to customize context color based on context name | | `KUBE_PS1_CTX_COLOR_FUNCTION` | No default, must be user supplied | Function to customize context color based on context name |
| `KUBE_PS1_HIDE_IF_NOCONTEXT` | `false` | Hide the kube-ps1 prompt if no context is set | | `KUBE_PS1_HIDE_IF_NOCONTEXT` | `false` | Hide the kube-ps1 prompt if no context is set |
In case you would like to have more control over kube environment context, you can tackle:
| Variable | Default | Meaning |
| :------- | :-----: | ------- |
| `KUBE_ENV_CTX_ENABLE` | `false` | Extract environment identifiers from `kube_ps1` context and display them as a separate block in square brackets |
| `KUBE_ENV_PADDING` | ` ` | Padding (spaces or characters) added around the environment block |
| `KUBE_ENV_OPEN_SYMBOL` | `[` | Opening symbol used for the environment block |
| `KUBE_ENV_CLOSE_SYMBOL` | `]${KUBE_ENV_PADDING}` | Closing symbol used for the environment block |
| `KUBE_ENV_PROD` | `prod` | Set default production label |
| `KUBE_ENV_STG` | `stag` | Set default staging label |
| `KUBE_ENV_TEST` | `test` | Set default testing label |
| `KUBE_ENV_DEV` | `dev` | Set default developing label |
| `KUBE_ENV_PROD_RE` | `(production|prod)-` | Regex used to detect production in the context name |
| `KUBE_ENV_STG_RE` | `(staging|stg)-` | Regex used to detect staging in the context name |
| `KUBE_ENV_TEST_RE` | `(testing|test)-` | Regex used to detect test in the context name |
| `KUBE_ENV_DEV_RE` | `develop-` | Regex used to detect development in the context name|
To disable a feature, set it to an empty string: To disable a feature, set it to an empty string:
```sh ```sh
@ -223,6 +244,15 @@ The default colors are set with the following variables:
| `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace | | `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace |
| `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background | | `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background |
If `KUBE_ENV_CTX_ENABLE` set to `true`, you can also adjust:
| Variable | Default | Meaning |
| :------- | :-----: | ------- |
| `KUBE_ENV_PROD_COLOR` | `red` | Set default color of the production environment |
| `KUBE_ENV_STG_COLOR` | `yellow` | Set default color of the staging environment |
| `KUBE_ENV_TEST_COLOR` | `green` | Set default color of the testing environment |
| `KUBE_ENV_DEV_COLOR` | `blue` | Set default color of the development environment |
Blue was used for the default symbol to match the Kubernetes color as closely Blue was used for the default symbol to match the Kubernetes color as closely
as possible. Red was chosen as the context name to stand out, and cyan for the as possible. Red was chosen as the context name to stand out, and cyan for the
namespace. namespace.

View file

@ -36,6 +36,28 @@ KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
KUBE_PS1_HIDE_IF_NOCONTEXT="${KUBE_PS1_HIDE_IF_NOCONTEXT:-false}" KUBE_PS1_HIDE_IF_NOCONTEXT="${KUBE_PS1_HIDE_IF_NOCONTEXT:-false}"
# Kube environment variables
KUBE_ENV_CTX_ENABLE="${KUBE_ENV_CTX_ENABLE:-false}"
KUBE_ENV_PADDING="${KUBE_ENV_PADDING:- }"
KUBE_ENV_OPEN_SYMBOL="${KUBE_ENV_OPEN_SYMBOL:-${KUBE_ENV_PADDING}[}"
KUBE_ENV_CLOSE_SYMBOL="${KUBE_ENV_CLOSE_SYMBOL:-]${KUBE_ENV_PADDING}}"
KUBE_ENV_PROD_COLOR="${KUBE_ENV_PROD_COLOR:-red}"
KUBE_ENV_STG_COLOR="${KUBE_ENV_STG_COLOR:-yellow}"
KUBE_ENV_TEST_COLOR="${KUBE_ENV_TEST_COLOR:-green}"
KUBE_ENV_DEV_COLOR="${KUBE_ENV_DEV_COLOR:-blue}"
KUBE_ENV_PROD="${KUBE_ENV_PROD:-prod}"
KUBE_ENV_STG="${KUBE_ENV_STG:-stag}"
KUBE_ENV_TEST="${KUBE_ENV_TEST:-test}"
KUBE_ENV_DEV="${KUBE_ENV_DEV:-dev}"
KUBE_ENV_PROD_RE="${KUBE_ENV_PROD_RE:-(production|prod)-}"
KUBE_ENV_STG_RE="${KUBE_ENV_STG_RE:-(staging|stg)-}"
KUBE_ENV_TEST_RE="${KUBE_ENV_TEST_RE:-(testing|test)-}"
KUBE_ENV_DEV_RE="${KUBE_ENV_DEV_RE:-develop-}"
_KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}" _KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
_KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled" _KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
_KUBE_PS1_LAST_TIME=0 _KUBE_PS1_LAST_TIME=0
@ -316,6 +338,35 @@ _kube_ps1_get_context_ns() {
_kube_ps1_get_ns _kube_ps1_get_ns
} }
_kube_ps1_cut_context() {
local pattern="$1"
KUBE_PS1_CONTEXT="$(sed -E "s/${pattern}//g" <<< "${KUBE_PS1_CONTEXT}")"
}
_kube_ps1_set_env_ctx() {
local ctx_color env_label
if grep -qE "${KUBE_ENV_PROD_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_ENV_PROD_RE}"
ctx_color=$(_kube_ps1_color_fg "${KUBE_ENV_PROD_COLOR}")
env_label=${KUBE_ENV_PROD}
elif grep -qE "${KUBE_ENV_STG_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_ENV_PROD_RE}"
ctx_color=$(_kube_ps1_color_fg "${KUBE_ENV_PROD_COLOR}")
env_label=${KUBE_ENV_PROD}
elif grep -qE "${KUBE_ENV_TEST_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_ENV_TEST_RE}"
ctx_color=$(_kube_ps1_color_fg "${KUBE_ENV_TEST_COLOR}")
env_label=${KUBE_ENV_TEST}
elif grep -qE "${KUBE_ENV_DEV_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_ENV_DEV_RE}"
ctx_color=$(_kube_ps1_color_fg "${KUBE_ENV_DEV_COLOR}")
env_label=${KUBE_ENV_DEV}
fi
KUBE_PS1+="${KUBE_ENV_OPEN_SYMBOL}${ctx_color}${env_label}${KUBE_PS1_RESET_COLOR}${KUBE_ENV_CLOSE_SYMBOL}"
}
# Set kube-ps1 shell defaults # Set kube-ps1 shell defaults
_kube_ps1_init _kube_ps1_init
@ -392,6 +443,11 @@ kube_ps1() {
# Background Color # Background Color
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg "${KUBE_PS1_BG_COLOR}")" [[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg "${KUBE_PS1_BG_COLOR}")"
# Context Env
if [[ -n "${KUBE_ENV_CTX_ENABLE}" ]] && [[ "${KUBE_ENV_CTX_ENABLE}" == true ]]; then
_kube_ps1_set_env_ctx
fi
# Prefix # Prefix
if [[ -z "${KUBE_PS1_PREFIX_COLOR:-}" ]] && [[ -n "${KUBE_PS1_PREFIX}" ]]; then if [[ -z "${KUBE_PS1_PREFIX_COLOR:-}" ]] && [[ -n "${KUBE_PS1_PREFIX}" ]]; then
KUBE_PS1+="${KUBE_PS1_PREFIX}" KUBE_PS1+="${KUBE_PS1_PREFIX}"