forked from extern/ohmyzsh
kube-ps1: update to latest upstream version (c685ac8)
Closes #8009 Co-authored-by: Jon Mosco <jonny.mosco@gmail.com>
This commit is contained in:
parent
b9d5ee7913
commit
93a2ba6b5f
@ -1,106 +1,246 @@
|
||||
# Kubernetes prompt for zsh
|
||||
kube-ps1: Kubernetes prompt for bash and zsh
|
||||
============================================
|
||||
|
||||
A Kubernetes zsh prompt that displays the current cluster cluster
|
||||
and the namespace.
|
||||
A script that lets you add the current Kubernetes context and namespace
|
||||
configured on `kubectl` to your Bash/Zsh prompt strings (i.e. the `$PS1`).
|
||||
|
||||
Inspired by several tools used to simplify usage of kubectl
|
||||
Inspired by several tools used to simplify usage of `kubectl`.
|
||||
|
||||
NOTE: If you are not using zsh, check out [kube-ps1](https://github.com/jonmosco/kube-ps1)
|
||||
designed for bash as well as zsh.
|
||||
![prompt](img/screenshot2.png)
|
||||
|
||||
![prompt_sol_light](img/screenshot-sol-light.png)
|
||||
|
||||
![prompt_img](img/screenshot-img.png)
|
||||
|
||||
![prompt demo](img/kube-ps1.gif)
|
||||
|
||||
## Installing
|
||||
|
||||
### MacOS
|
||||
|
||||
Homebrew package manager:
|
||||
|
||||
```
|
||||
$ brew update
|
||||
$ brew install kube-ps1
|
||||
```
|
||||
### From Source
|
||||
|
||||
1. Clone this repository
|
||||
2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`
|
||||
|
||||
### Arch Linux
|
||||
AUR Package available at [https://aur.archlinux.org/packages/kube-ps1/](https://aur.archlinux.org/packages/kube-ps1/).
|
||||
|
||||
#### Zsh
|
||||
```sh
|
||||
source /path/to/kube-ps1.sh
|
||||
PROMPT='$(kube_ps1)'$PROMPT
|
||||
```
|
||||
#### Bash
|
||||
```sh
|
||||
source /path/to/kube-ps1.sh
|
||||
PS1='[\u@\h \W $(kube_ps1)]\$ '
|
||||
```
|
||||
|
||||
### Zsh Plugin Managers
|
||||
|
||||
#### Using [zplugin](https://github.com/zdharma/zplugin)
|
||||
|
||||
Update `.zshrc` with:
|
||||
```sh
|
||||
zplugin light jonmosco/kube-ps1
|
||||
PROMPT='$(kube_ps1)'$PROMPT
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
The default prompt assumes you have the kubectl command line utility installed. It
|
||||
can be obtained here:
|
||||
The default prompt assumes you have the `kubectl` command line utility installed.
|
||||
Official installation instructions and binaries are available:
|
||||
|
||||
[Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
|
||||
|
||||
If using this with OpenShift, the oc tool needs installed. It can be obtained from here:
|
||||
If using this with OpenShift, the `oc` tool needs installed. It can be obtained
|
||||
from brew ports:
|
||||
|
||||
```
|
||||
brew install openshift-cli
|
||||
```
|
||||
or the source can be downloaded:
|
||||
|
||||
[OC Client Tools](https://www.openshift.org/download.html)
|
||||
|
||||
Set the binary to `oc` with the following environment variable:
|
||||
|
||||
```
|
||||
KUBE_PS1_BINARY=oc
|
||||
```
|
||||
|
||||
If neither binary is available, the prompt will print the following:
|
||||
|
||||
```
|
||||
(<symbol>|BINARY-N/A:N/A)
|
||||
```
|
||||
|
||||
## Helper utilities
|
||||
|
||||
There are several great tools that make using kubectl very enjoyable.
|
||||
There are several great tools that make using kubectl very enjoyable:
|
||||
|
||||
[kubectx and kubenx](https://github.com/ahmetb/kubectx) are great for
|
||||
- [`kubectx` and `kubens`](https://github.com/ahmetb/kubectx) are great for
|
||||
fast switching between clusters and namespaces.
|
||||
|
||||
## Tmux port
|
||||
|
||||
I have begun porting kube-ps1 to tmux as a status line plugin. If you prefer
|
||||
tmux, and like the functionality provided by kube-ps1, checkout the
|
||||
[kube-tmux](https://github.com/jonmosco/kube-tmux) project
|
||||
|
||||
## Prompt Structure
|
||||
|
||||
The prompt layout is:
|
||||
The default prompt layout is:
|
||||
|
||||
```
|
||||
(<symbol>|<cluster>:<namespace>)
|
||||
(<symbol>|<context>:<namespace>)
|
||||
```
|
||||
|
||||
## Enabling
|
||||
If the current-context is not set, kube-ps1 will return the following:
|
||||
|
||||
In order to use kube-ps1 with Oh My Zsh, you'll need to enable them in the
|
||||
.zshrc file. You'll find the zshrc file in your $HOME directory. Open it with
|
||||
your favorite text editor and you'll see a spot to list all the plugins you
|
||||
want to load.
|
||||
|
||||
```shell
|
||||
vim $HOME/.zshrc
|
||||
```
|
||||
(<symbol>|N/A:N/A)
|
||||
```
|
||||
|
||||
Add kube-ps1 to the list of enabled plugins and enable it on the prompt:
|
||||
## Enabling/Disabling
|
||||
|
||||
```shell
|
||||
plugins=(
|
||||
git
|
||||
kube-ps1
|
||||
)
|
||||
If you want to stop showing Kubernetes status on your prompt string temporarily
|
||||
run `kubeoff`. To disable the prompt for all shell sessions, run `kubeoff -g`.
|
||||
You can enable it again in the current shell by running `kubeon`, and globally
|
||||
with `kubeon -g`.
|
||||
|
||||
# After the "source Oh My Zsh" line
|
||||
PROMPT=$PROMPT'$(kube_ps1) '
|
||||
# Or in double quotes, don't forget to escape
|
||||
PROMPT=$PROMPT"\$(kube_ps1) "
|
||||
```
|
||||
|
||||
Note: The `PROMPT` example above was tested with the theme `robbyrussell`.
|
||||
|
||||
## Enabling / Disabling on the current shell
|
||||
|
||||
Sometimes the kubernetes information can be anoying, you can easily
|
||||
switch it on and off with the following commands:
|
||||
|
||||
```shell
|
||||
kubeon
|
||||
kubeon : turn on kube-ps1 status for this shell. Takes precedence over
|
||||
global setting for current session
|
||||
kubeon -g : turn on kube-ps1 status globally
|
||||
kubeoff : turn off kube-ps1 status for this shell. Takes precedence over
|
||||
global setting for current session
|
||||
kubeoff -g : turn off kube-ps1 status globally
|
||||
```
|
||||
|
||||
```shell
|
||||
kubeoff
|
||||
```
|
||||
|
||||
## Colors
|
||||
|
||||
Blue was used as the prefix to match the Kubernetes color as closely as
|
||||
possible. Red was chosen as the cluster name to stand out, and cyan
|
||||
for the namespace. Check the customization section for changing them.
|
||||
|
||||
## Customization
|
||||
|
||||
The default settings can be overridden in ~/.zshrc
|
||||
The default settings can be overridden in `~/.bashrc` or `~/.zshrc` by setting
|
||||
the following environment variables:
|
||||
|
||||
| Variable | Default | Meaning |
|
||||
| :------- | :-----: | ------- |
|
||||
| `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
|
||||
| `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` |
|
||||
| `KUBE_PS1_PREFIX` | `(` | Prompt opening character |
|
||||
| `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` |
|
||||
| `KUBE_PS1_SYMBOL_DEFAULT` | `⎈ ` | Default prompt symbol. Unicode `\u2388` |
|
||||
| `KUBE_PS1_SYMBOL_USE_IMG` | `false` | ☸️ , Unicode `\u2638` as the prompt symbol |
|
||||
| `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` |
|
||||
| `KUBE_PS1_SEPERATOR` | `\|` | Separator between symbol and cluster name |
|
||||
| `KUBE_PS1_DIVIDER` | `:` | Separator between cluster and namespace |
|
||||
| `KUBE_PS1_SEPARATOR` | | | Separator between symbol and context name |
|
||||
| `KUBE_PS1_DIVIDER` | `:` | Separator between context and namespace |
|
||||
| `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
|
||||
| `KUBE_PS1_COLOR_SYMBOL` | `"%F{blue}"` | Custom color for the symbol |
|
||||
| `KUBE_PS1_COLOR_CONTEXT` | `"%F{red}"` | Custom color for the context |
|
||||
| `KUBE_PS1_COLOR_NS` | `"%F{cyan}"` | Custom color for the namespace |
|
||||
| `KUBE_PS1_ENABLED` | `true` | Set to false to start disabled on any new shell, `kubeon`/`kubeoff` will flip this value on the current shell |
|
||||
| `KUBE_PS1_CLUSTER_FUNCTION` | No default, must be user supplied | Function to customize how cluster is displayed |
|
||||
| `KUBE_PS1_NAMESPACE_FUNCTION` | No default, must be user supplied | Function to customize how namespace is displayed |
|
||||
|
||||
For terminals that do not support UTF-8, the symbol will be replaced with the
|
||||
string `k8s`.
|
||||
|
||||
To disable a feature, set it to an empty string:
|
||||
|
||||
```
|
||||
KUBE_PS1_SEPARATOR=''
|
||||
```
|
||||
|
||||
## Colors
|
||||
|
||||
The default colors are set with the following environment variables:
|
||||
|
||||
| Variable | Default | Meaning |
|
||||
| :------- | :-----: | ------- |
|
||||
| `KUBE_PS1_SYMBOL_COLOR` | `blue` | Set default color of the Kubernetes symbol |
|
||||
| `KUBE_PS1_CTX_COLOR` | `red` | Set default color of the context |
|
||||
| `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace |
|
||||
| `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background |
|
||||
|
||||
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
|
||||
namespace.
|
||||
|
||||
Set the variable to an empty string if you do not want color for each
|
||||
prompt section:
|
||||
|
||||
```
|
||||
KUBE_PS1_CTX_COLOR=''
|
||||
```
|
||||
|
||||
Names are usable for the following colors:
|
||||
|
||||
```
|
||||
black, red, green, yellow, blue, magenta, cyan
|
||||
```
|
||||
|
||||
256 colors are available by specifying the numerical value as the variable
|
||||
argument.
|
||||
|
||||
## Customize display of cluster name and namespace
|
||||
|
||||
You can change how the cluster name and namespace are displayed using the
|
||||
`KUBE_PS1_CLUSTER_FUNCTION` and `KUBE_PS1_NAMESPACE_FUNCTION` variables
|
||||
respectively.
|
||||
|
||||
For the following examples let's assume the following:
|
||||
|
||||
cluster name: `sandbox.k8s.example.com`
|
||||
namespace: `alpha`
|
||||
|
||||
If you're using domain style cluster names, your prompt will get quite long
|
||||
very quickly. Let's say you only want to display the first portion of the
|
||||
cluster name (`sandbox`), you could do that by adding the following:
|
||||
|
||||
```sh
|
||||
function get_cluster_short() {
|
||||
echo "$1" | cut -d . -f1
|
||||
}
|
||||
|
||||
KUBE_PS1_CLUSTER_FUNCTION=get_cluster_short
|
||||
```
|
||||
|
||||
The same pattern can be followed to customize the display of the namespace.
|
||||
Let's say you would prefer the namespace to be displayed in all uppercase
|
||||
(`ALPHA`), here's one way you could do that:
|
||||
|
||||
```sh
|
||||
function get_namespace_upper() {
|
||||
echo "$1" | tr '[:lower:]' '[:upper:]'
|
||||
}
|
||||
|
||||
export KUBE_PS1_NAMESPACE_FUNCTION=get_namespace_upper
|
||||
```
|
||||
|
||||
In both cases, the variable is set to the name of the function, and you must have defined the function in your shell configuration before kube_ps1 is called. The function must accept a single parameter and echo out the final value.
|
||||
|
||||
### Bug Reports and shell configuration
|
||||
|
||||
Due to the vast ways of customizing the shell, please try the prompt with a
|
||||
minimal configuration before submitting a bug report.
|
||||
|
||||
This can be done as follows for each shell before loading kube-ps1:
|
||||
|
||||
Bash:
|
||||
```bash
|
||||
bash --norc
|
||||
```
|
||||
|
||||
Zsh:
|
||||
```bash
|
||||
zsh -f
|
||||
or
|
||||
zsh --no-rcs
|
||||
```
|
||||
|
||||
## Contributors
|
||||
|
||||
- Jared Yanovich
|
||||
- Pedro Moranga
|
||||
* [Ahmet Alp Balkan](https://github.com/ahmetb)
|
||||
* Jared Yanovich
|
||||
|
@ -1,16 +1,15 @@
|
||||
#!/bin/zsh
|
||||
#!/bin/bash
|
||||
|
||||
# Kubernetes prompt helper for bash/zsh
|
||||
# ported to oh-my-zsh
|
||||
# Displays current context and namespace
|
||||
|
||||
# Copyright 2018 Jon Mosco
|
||||
# Copyright 2019 Jon Mosco
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@ -21,39 +20,150 @@
|
||||
# Debug
|
||||
[[ -n $DEBUG ]] && set -x
|
||||
|
||||
setopt PROMPT_SUBST
|
||||
autoload -U add-zsh-hook
|
||||
add-zsh-hook precmd _kube_ps1_update_cache
|
||||
zmodload zsh/stat
|
||||
zmodload zsh/datetime
|
||||
|
||||
# Default values for the prompt
|
||||
# Override these values in ~/.zshrc
|
||||
# Override these values in ~/.zshrc or ~/.bashrc
|
||||
KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
|
||||
KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
|
||||
KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
|
||||
KUBE_PS1_SYMBOL_DEFAULT=${KUBE_PS1_SYMBOL_DEFAULT:-$'\u2388 '}
|
||||
KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
|
||||
KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
|
||||
KUBE_PS1_CONTEXT_ENABLE="${KUBE_PS1_CONTEXT_ENABLE:-true}"
|
||||
KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
|
||||
KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
|
||||
KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
|
||||
KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
|
||||
KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
|
||||
KUBE_PS1_SYMBOL_COLOR="${KUBE_PS1_SYMBOL_COLOR-blue}"
|
||||
KUBE_PS1_CTX_COLOR="${KUBE_PS1_CTX_COLOR-red}"
|
||||
KUBE_PS1_NS_COLOR="${KUBE_PS1_NS_COLOR-cyan}"
|
||||
KUBE_PS1_BG_COLOR="${KUBE_PS1_BG_COLOR}"
|
||||
KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
|
||||
KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
|
||||
KUBE_PS1_LAST_TIME=0
|
||||
KUBE_PS1_ENABLED=true
|
||||
KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}"
|
||||
KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"
|
||||
|
||||
KUBE_PS1_COLOR_SYMBOL="%{$fg[blue]%}"
|
||||
KUBE_PS1_COLOR_CONTEXT="%{$fg[red]%}"
|
||||
KUBE_PS1_COLOR_NS="%{$fg[cyan]%}"
|
||||
# Determine our shell
|
||||
if [ "${ZSH_VERSION-}" ]; then
|
||||
KUBE_PS1_SHELL="zsh"
|
||||
elif [ "${BASH_VERSION-}" ]; then
|
||||
KUBE_PS1_SHELL="bash"
|
||||
fi
|
||||
|
||||
_kube_ps1_init() {
|
||||
[[ -f "${KUBE_PS1_DISABLE_PATH}" ]] && KUBE_PS1_ENABLED=off
|
||||
|
||||
case "${KUBE_PS1_SHELL}" in
|
||||
"zsh")
|
||||
_KUBE_PS1_OPEN_ESC="%{"
|
||||
_KUBE_PS1_CLOSE_ESC="%}"
|
||||
_KUBE_PS1_DEFAULT_BG="%k"
|
||||
_KUBE_PS1_DEFAULT_FG="%f"
|
||||
setopt PROMPT_SUBST
|
||||
autoload -U add-zsh-hook
|
||||
add-zsh-hook precmd _kube_ps1_update_cache
|
||||
zmodload -F zsh/stat b:zstat
|
||||
zmodload zsh/datetime
|
||||
;;
|
||||
"bash")
|
||||
_KUBE_PS1_OPEN_ESC=$'\001'
|
||||
_KUBE_PS1_CLOSE_ESC=$'\002'
|
||||
_KUBE_PS1_DEFAULT_BG=$'\033[49m'
|
||||
_KUBE_PS1_DEFAULT_FG=$'\033[39m'
|
||||
[[ $PROMPT_COMMAND =~ _kube_ps1_update_cache ]] || PROMPT_COMMAND="_kube_ps1_update_cache;${PROMPT_COMMAND:-:}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_kube_ps1_color_fg() {
|
||||
local KUBE_PS1_FG_CODE
|
||||
case "${1}" in
|
||||
black) KUBE_PS1_FG_CODE=0;;
|
||||
red) KUBE_PS1_FG_CODE=1;;
|
||||
green) KUBE_PS1_FG_CODE=2;;
|
||||
yellow) KUBE_PS1_FG_CODE=3;;
|
||||
blue) KUBE_PS1_FG_CODE=4;;
|
||||
magenta) KUBE_PS1_FG_CODE=5;;
|
||||
cyan) KUBE_PS1_FG_CODE=6;;
|
||||
white) KUBE_PS1_FG_CODE=7;;
|
||||
# 256
|
||||
[0-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-6]) KUBE_PS1_FG_CODE="${1}";;
|
||||
*) KUBE_PS1_FG_CODE=default
|
||||
esac
|
||||
|
||||
if [[ "${KUBE_PS1_FG_CODE}" == "default" ]]; then
|
||||
KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_FG}"
|
||||
return
|
||||
elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
|
||||
KUBE_PS1_FG_CODE="%F{$KUBE_PS1_FG_CODE}"
|
||||
elif [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
|
||||
if tput setaf 1 &> /dev/null; then
|
||||
KUBE_PS1_FG_CODE="$(tput setaf ${KUBE_PS1_FG_CODE})"
|
||||
elif [[ $KUBE_PS1_FG_CODE -ge 0 ]] && [[ $KUBE_PS1_FG_CODE -le 256 ]]; then
|
||||
KUBE_PS1_FG_CODE="\033[38;5;${KUBE_PS1_FG_CODE}m"
|
||||
else
|
||||
KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_FG}"
|
||||
fi
|
||||
fi
|
||||
echo ${_KUBE_PS1_OPEN_ESC}${KUBE_PS1_FG_CODE}${_KUBE_PS1_CLOSE_ESC}
|
||||
}
|
||||
|
||||
_kube_ps1_color_bg() {
|
||||
local KUBE_PS1_BG_CODE
|
||||
case "${1}" in
|
||||
black) KUBE_PS1_BG_CODE=0;;
|
||||
red) KUBE_PS1_BG_CODE=1;;
|
||||
green) KUBE_PS1_BG_CODE=2;;
|
||||
yellow) KUBE_PS1_BG_CODE=3;;
|
||||
blue) KUBE_PS1_BG_CODE=4;;
|
||||
magenta) KUBE_PS1_BG_CODE=5;;
|
||||
cyan) KUBE_PS1_BG_CODE=6;;
|
||||
white) KUBE_PS1_BG_CODE=7;;
|
||||
# 256
|
||||
[0-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-6]) KUBE_PS1_BG_CODE="${1}";;
|
||||
*) KUBE_PS1_BG_CODE=$'\033[0m';;
|
||||
esac
|
||||
|
||||
if [[ "${KUBE_PS1_BG_CODE}" == "default" ]]; then
|
||||
KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_BG}"
|
||||
return
|
||||
elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
|
||||
KUBE_PS1_BG_CODE="%K{$KUBE_PS1_BG_CODE}"
|
||||
elif [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
|
||||
if tput setaf 1 &> /dev/null; then
|
||||
KUBE_PS1_BG_CODE="$(tput setab ${KUBE_PS1_BG_CODE})"
|
||||
elif [[ $KUBE_PS1_BG_CODE -ge 0 ]] && [[ $KUBE_PS1_BG_CODE -le 256 ]]; then
|
||||
KUBE_PS1_BG_CODE="\033[48;5;${KUBE_PS1_BG_CODE}m"
|
||||
else
|
||||
KUBE_PS1_BG_CODE="${DEFAULT_BG}"
|
||||
fi
|
||||
fi
|
||||
echo ${OPEN_ESC}${KUBE_PS1_BG_CODE}${CLOSE_ESC}
|
||||
}
|
||||
|
||||
_kube_ps1_binary_check() {
|
||||
command -v "$1" >/dev/null
|
||||
command -v $1 >/dev/null
|
||||
}
|
||||
|
||||
_kube_ps1_symbol() {
|
||||
[[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
|
||||
|
||||
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
|
||||
KUBE_PS1_SYMBOL_IMG="\u2638 "
|
||||
case "${KUBE_PS1_SHELL}" in
|
||||
bash)
|
||||
if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388 ' != "\\u2388 " ]]; then
|
||||
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
|
||||
# KUBE_PS1_SYMBOL=$'\u2388 '
|
||||
KUBE_PS1_SYMBOL_IMG=$'\u2638 '
|
||||
else
|
||||
KUBE_PS1_SYMBOL=$'\xE2\x8E\x88 '
|
||||
KUBE_PS1_SYMBOL_IMG=$'\xE2\x98\xB8 '
|
||||
fi
|
||||
;;
|
||||
zsh)
|
||||
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
|
||||
KUBE_PS1_SYMBOL_IMG="\u2638 ";;
|
||||
*)
|
||||
KUBE_PS1_SYMBOL="k8s"
|
||||
esac
|
||||
|
||||
if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
|
||||
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
|
||||
@ -73,22 +183,24 @@ _kube_ps1_file_newer_than() {
|
||||
local file=$1
|
||||
local check_time=$2
|
||||
|
||||
zmodload -e "zsh/stat"
|
||||
if [[ "$?" -eq 0 ]]; then
|
||||
mtime=$(stat +mtime "${file}")
|
||||
if [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
|
||||
mtime=$(zstat -L +mtime "${file}")
|
||||
elif stat -c "%s" /dev/null &> /dev/null; then
|
||||
# GNU stat
|
||||
mtime=$(stat -c %Y "${file}")
|
||||
mtime=$(stat -L -c %Y "${file}")
|
||||
else
|
||||
# BSD stat
|
||||
mtime=$(stat -f %m "$file")
|
||||
mtime=$(stat -L -f %m "$file")
|
||||
fi
|
||||
|
||||
[[ "${mtime}" -gt "${check_time}" ]]
|
||||
}
|
||||
|
||||
_kube_ps1_update_cache() {
|
||||
KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
|
||||
local return_code=$?
|
||||
|
||||
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return $return_code
|
||||
|
||||
if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
|
||||
# No ability to fetch context/namespace; display N/A.
|
||||
KUBE_PS1_CONTEXT="BINARY-N/A"
|
||||
@ -113,47 +225,147 @@ _kube_ps1_update_cache() {
|
||||
return
|
||||
fi
|
||||
done
|
||||
|
||||
return $return_code
|
||||
}
|
||||
|
||||
_kube_ps1_get_context_ns() {
|
||||
_kube_ps1_get_context() {
|
||||
if [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
|
||||
KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
|
||||
# Set namespace to 'N/A' if it is not defined
|
||||
KUBE_PS1_CONTEXT="${KUBE_PS1_CONTEXT:-N/A}"
|
||||
|
||||
# Set the command time
|
||||
KUBE_PS1_LAST_TIME=$EPOCHSECONDS
|
||||
|
||||
KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
|
||||
if [[ -z "${KUBE_PS1_CONTEXT}" ]]; then
|
||||
KUBE_PS1_CONTEXT="N/A"
|
||||
KUBE_PS1_NAMESPACE="N/A"
|
||||
return
|
||||
elif [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
|
||||
KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
|
||||
# Set namespace to 'default' if it is not defined
|
||||
KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
|
||||
if [[ ! -z "${KUBE_PS1_CLUSTER_FUNCTION}" ]]; then
|
||||
KUBE_PS1_CONTEXT=$($KUBE_PS1_CLUSTER_FUNCTION $KUBE_PS1_CONTEXT)
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# function to disable the prompt on the current shell
|
||||
kubeon(){
|
||||
KUBE_PS1_ENABLED=true
|
||||
_kube_ps1_get_ns() {
|
||||
if [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
|
||||
KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
|
||||
# Set namespace to 'default' if it is not defined
|
||||
KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
|
||||
|
||||
if [[ ! -z "${KUBE_PS1_NAMESPACE_FUNCTION}" ]]; then
|
||||
KUBE_PS1_NAMESPACE=$($KUBE_PS1_NAMESPACE_FUNCTION $KUBE_PS1_NAMESPACE)
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# function to disable the prompt on the current shell
|
||||
kubeoff(){
|
||||
KUBE_PS1_ENABLED=false
|
||||
_kube_ps1_get_context_ns() {
|
||||
# Set the command time
|
||||
if [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
|
||||
if ((BASH_VERSINFO[0] >= 4 && BASH_VERSINFO[1] >= 2)); then
|
||||
KUBE_PS1_LAST_TIME=$(printf '%(%s)T')
|
||||
else
|
||||
KUBE_PS1_LAST_TIME=$(date +%s)
|
||||
fi
|
||||
elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
|
||||
KUBE_PS1_LAST_TIME=$EPOCHSECONDS
|
||||
fi
|
||||
|
||||
_kube_ps1_get_context
|
||||
_kube_ps1_get_ns
|
||||
}
|
||||
|
||||
# Set kube-ps1 shell defaults
|
||||
_kube_ps1_init
|
||||
|
||||
_kubeon_usage() {
|
||||
cat <<"EOF"
|
||||
Toggle kube-ps1 prompt on
|
||||
|
||||
Usage: kubeon [-g | --global] [-h | --help]
|
||||
|
||||
With no arguments, turn off kube-ps1 status for this shell instance (default).
|
||||
|
||||
-g --global turn on kube-ps1 status globally
|
||||
-h --help print this message
|
||||
EOF
|
||||
}
|
||||
|
||||
_kubeoff_usage() {
|
||||
cat <<"EOF"
|
||||
Toggle kube-ps1 prompt off
|
||||
|
||||
Usage: kubeoff [-g | --global] [-h | --help]
|
||||
|
||||
With no arguments, turn off kube-ps1 status for this shell instance (default).
|
||||
|
||||
-g --global turn off kube-ps1 status globally
|
||||
-h --help print this message
|
||||
EOF
|
||||
}
|
||||
|
||||
kubeon() {
|
||||
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||
_kubeon_usage
|
||||
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
|
||||
rm -f -- "${KUBE_PS1_DISABLE_PATH}"
|
||||
elif [[ "$#" -ne 0 ]]; then
|
||||
echo -e "error: unrecognized flag ${1}\\n"
|
||||
_kubeon_usage
|
||||
return
|
||||
fi
|
||||
|
||||
KUBE_PS1_ENABLED=on
|
||||
}
|
||||
|
||||
kubeoff() {
|
||||
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||
_kubeoff_usage
|
||||
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
|
||||
mkdir -p -- "$(dirname "${KUBE_PS1_DISABLE_PATH}")"
|
||||
touch -- "${KUBE_PS1_DISABLE_PATH}"
|
||||
elif [[ $# -ne 0 ]]; then
|
||||
echo "error: unrecognized flag ${1}" >&2
|
||||
_kubeoff_usage
|
||||
return
|
||||
fi
|
||||
|
||||
KUBE_PS1_ENABLED=off
|
||||
}
|
||||
|
||||
# Build our prompt
|
||||
kube_ps1 () {
|
||||
local reset_color="%{$reset_color%}"
|
||||
[[ ${KUBE_PS1_ENABLED} != 'true' ]] && return
|
||||
kube_ps1() {
|
||||
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
|
||||
[[ -z "${KUBE_PS1_CONTEXT}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]] && return
|
||||
|
||||
KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
|
||||
KUBE_PS1+="${KUBE_PS1_COLOR_SYMBOL}$(_kube_ps1_symbol)"
|
||||
KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
|
||||
KUBE_PS1+="${KUBE_PS1_COLOR_CONTEXT}$KUBE_PS1_CONTEXT${reset_color}"
|
||||
KUBE_PS1+="$KUBE_PS1_DIVIDER"
|
||||
KUBE_PS1+="${KUBE_PS1_COLOR_NS}$KUBE_PS1_NAMESPACE${reset_color}"
|
||||
KUBE_PS1+="$KUBE_PS1_SUFFIX"
|
||||
local KUBE_PS1
|
||||
local KUBE_PS1_RESET_COLOR="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_FG}${_KUBE_PS1_CLOSE_ESC}"
|
||||
|
||||
# Background Color
|
||||
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg ${KUBE_PS1_BG_COLOR})"
|
||||
|
||||
# Prefix
|
||||
[[ -n "${KUBE_PS1_PREFIX}" ]] && KUBE_PS1+="${KUBE_PS1_PREFIX}"
|
||||
|
||||
# Symbol
|
||||
KUBE_PS1+="$(_kube_ps1_color_fg $KUBE_PS1_SYMBOL_COLOR)$(_kube_ps1_symbol)${KUBE_PS1_RESET_COLOR}"
|
||||
|
||||
if [[ -n "${KUBE_PS1_SEPARATOR}" ]] && [[ "${KUBE_PS1_SYMBOL_ENABLE}" == true ]]; then
|
||||
KUBE_PS1+="${KUBE_PS1_SEPARATOR}"
|
||||
fi
|
||||
|
||||
# Context
|
||||
if [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
|
||||
KUBE_PS1+="$(_kube_ps1_color_fg $KUBE_PS1_CTX_COLOR)${KUBE_PS1_CONTEXT}${KUBE_PS1_RESET_COLOR}"
|
||||
fi
|
||||
|
||||
# Namespace
|
||||
if [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
|
||||
if [[ -n "${KUBE_PS1_DIVIDER}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
|
||||
KUBE_PS1+="${KUBE_PS1_DIVIDER}"
|
||||
fi
|
||||
KUBE_PS1+="$(_kube_ps1_color_fg ${KUBE_PS1_NS_COLOR})${KUBE_PS1_NAMESPACE}${KUBE_PS1_RESET_COLOR}"
|
||||
fi
|
||||
|
||||
# Suffix
|
||||
[[ -n "${KUBE_PS1_SUFFIX}" ]] && KUBE_PS1+="${KUBE_PS1_SUFFIX}"
|
||||
|
||||
# Close Background color if defined
|
||||
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_BG}${_KUBE_PS1_CLOSE_ESC}"
|
||||
|
||||
echo "${KUBE_PS1}"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user