Skip to content

Commit 5d4d0a6

Browse files
committed
feat: add environment indicator to the prompt layout
1 parent e19c9ee commit 5d4d0a6

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ The default prompt layout is:
127127
```sh
128128
(<symbol>|<context>:<namespace>)
129129
```
130+
You can enable kube environment context (like dev/staging/test/prod) via `KUBE_PS1_ENV_CTX_ENABLE=true`. In this case, the layout will be:
131+
```sh
132+
[<kube-environment>] (<symbol>|<context>:<namespace>)
133+
```
130134

131135
If the current-context is not set, kube-ps1 will return the following:
132136

@@ -204,6 +208,23 @@ the following variables:
204208
| `KUBE_PS1_CTX_COLOR_FUNCTION` | No default, must be user supplied | Function to customize context color based on context name |
205209
| `KUBE_PS1_HIDE_IF_NOCONTEXT` | `false` | Hide the kube-ps1 prompt if no context is set |
206210

211+
For more control over the Kubernetes environment context, you can adjust:
212+
213+
| Variable | Default | Meaning |
214+
| :------- | :-----: | ------- |
215+
| `KUBE_PS1_ENV_CTX_ENABLE` | `false` | Extract environment identifiers from context and display them as a separate block in square brackets, e.g `testing-mycluster` becomes `[test] mycluster` |
216+
| `KUBE_PS1_ENV_PADDING` | one space | Padding (spaces or characters) added around the environment block |
217+
| `KUBE_PS1_ENV_OPEN_SYMBOL` | `[` | Opening symbol used for the environment block |
218+
| `KUBE_PS1_ENV_CLOSE_SYMBOL` | `]` | Closing symbol used for the environment block |
219+
| `KUBE_PS1_ENV_PROD_LABEL` | `prod` | Set default production label |
220+
| `KUBE_PS1_ENV_STG_LABEL` | `stag` | Set default staging label |
221+
| `KUBE_PS1_ENV_TEST_LABEL` | `test` | Set default testing label |
222+
| `KUBE_PS1_ENV_DEV_LABEL` | `dev` | Set default developing label |
223+
| `KUBE_PS1_ENV_PROD_RE` | `(production\|prod)-` | Regex used to detect production in the context name |
224+
| `KUBE_PS1_ENV_STG_RE` | `(staging\|stg)-` | Regex used to detect staging in the context name |
225+
| `KUBE_PS1_ENV_TEST_RE` | `(testing\|test)-` | Regex used to detect test in the context name |
226+
| `KUBE_PS1_ENV_DEV_RE` | `dev(elop(ment)?)?-` | Regex used to detect development in the context name |
227+
207228
To disable a feature, set it to an empty string:
208229

209230
```sh
@@ -227,6 +248,15 @@ Blue was used for the default symbol to match the Kubernetes color as closely
227248
as possible. Red was chosen as the context name to stand out, and cyan for the
228249
namespace.
229250

251+
If `KUBE_PS1_ENV_CTX_ENABLE` is set to `true`, you can also modify:
252+
253+
| Variable | Default | Meaning |
254+
| :------- | :-----: | ------- |
255+
| `KUBE_PS1_ENV_PROD_COLOR` | `red` | Set default color of the production environment |
256+
| `KUBE_PS1_ENV_STG_COLOR` | `yellow` | Set default color of the staging environment |
257+
| `KUBE_PS1_ENV_TEST_COLOR` | `green` | Set default color of the testing environment |
258+
| `KUBE_PS1_ENV_DEV_COLOR` | `blue` | Set default color of the development environment |
259+
230260
Set the variable to an empty string if you do not want color for each
231261
prompt section:
232262

kube-ps1.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
3636

3737
KUBE_PS1_HIDE_IF_NOCONTEXT="${KUBE_PS1_HIDE_IF_NOCONTEXT:-false}"
3838

39+
# Kube environment variables
40+
KUBE_PS1_ENV_CTX_ENABLE="${KUBE_PS1_ENV_CTX_ENABLE:-false}"
41+
KUBE_PS1_ENV_PADDING="${KUBE_PS1_ENV_PADDING:- }"
42+
43+
KUBE_PS1_ENV_OPEN_SYMBOL="${KUBE_PS1_ENV_OPEN_SYMBOL:-[}"
44+
KUBE_PS1_ENV_CLOSE_SYMBOL="${KUBE_PS1_ENV_CLOSE_SYMBOL:-]}"
45+
46+
declare -Ag KUBE_PS1_ENV=(
47+
[prod_color]="${KUBE_PS1_ENV_PROD_COLOR:-red}"
48+
[prod_label]="${KUBE_PS1_ENV_PROD_LABEL:-prod}"
49+
[prod_re]="${KUBE_PS1_ENV_PROD_RE:-(production|prod)-}"
50+
51+
[stg_color]="${KUBE_PS1_ENV_STG_COLOR:-yellow}"
52+
[stg_label]="${KUBE_PS1_ENV_STG_LABEL:-stag}"
53+
[stg_re]="${KUBE_PS1_ENV_STG_RE:-(staging|stg)-}"
54+
55+
[test_color]="${KUBE_PS1_ENV_TEST_COLOR:-green}"
56+
[test_label]="${KUBE_PS1_ENV_TEST_LABEL:-test}"
57+
[test_re]="${KUBE_PS1_ENV_TEST_RE:-(testing|test)-}"
58+
59+
[dev_color]="${KUBE_PS1_ENV_DEV_COLOR:-blue}"
60+
[dev_label]="${KUBE_PS1_ENV_DEV_LABEL:-dev}"
61+
[dev_re]="${KUBE_PS1_ENV_DEV_RE:-dev(elop(ment)?)?-}"
62+
)
63+
3964
_KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
4065
_KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
4166
_KUBE_PS1_LAST_TIME=0
@@ -165,7 +190,7 @@ _kube_ps1_symbol() {
165190

166191
local symbol=""
167192
local symbol_default=$'\u2388'
168-
local symbol_img="☸️"
193+
local symbol_img="☸️"
169194
local k8s_glyph=$'\Uf10fe'
170195
local k8s_symbol_color=blue
171196
local oc_glyph=$'\ue7b7'
@@ -326,6 +351,21 @@ _kube_ps1_get_context_ns() {
326351
_kube_ps1_get_ns
327352
}
328353

354+
_kube_ps1_set_env_ctx() {
355+
local ctx_color ctx_label
356+
357+
for ctx in prod test dev stg; do
358+
if grep -qE "${KUBE_PS1_ENV[${ctx}_re]}" <<< "${KUBE_PS1_CONTEXT}"; then
359+
sed -E "s/${KUBE_PS1_ENV[${ctx}_re]}//g" <<< "${KUBE_PS1_CONTEXT}" > /dev/null
360+
ctx_color="$(_kube_ps1_color_fg "${KUBE_PS1_ENV[${ctx}_color]}")"
361+
ctx_label="${KUBE_PS1_ENV[${ctx}_label]}"
362+
fi
363+
done
364+
365+
KUBE_PS1+="${KUBE_PS1_ENV_PADDING}${KUBE_PS1_ENV_OPEN_SYMBOL}"${ctx_color}${ctx_label}${KUBE_PS1_RESET_COLOR}"${KUBE_PS1_ENV_CLOSE_SYMBOL}${KUBE_PS1_ENV_PADDING}"
366+
}
367+
368+
329369
# Set kube-ps1 shell defaults
330370
_kube_ps1_init
331371

@@ -402,6 +442,11 @@ kube_ps1() {
402442
# Background Color
403443
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg "${KUBE_PS1_BG_COLOR}")"
404444

445+
# Context Env
446+
if [[ -n "${KUBE_PS1_ENV_CTX_ENABLE}" ]] && [[ "${KUBE_PS1_ENV_CTX_ENABLE}" == true ]]; then
447+
_kube_ps1_set_env_ctx
448+
fi
449+
405450
# Prefix
406451
if [[ -z "${KUBE_PS1_PREFIX_COLOR:-}" ]] && [[ -n "${KUBE_PS1_PREFIX}" ]]; then
407452
KUBE_PS1+="${KUBE_PS1_PREFIX}"

0 commit comments

Comments
 (0)