1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-22 13:50:09 +00:00

updating kube-ps1 to align with upstream changes (#6995)

This commit is contained in:
Jon Mosco 2018-07-19 10:32:31 -04:00 committed by Marc Cornellà
parent d302e3eebe
commit a3ab45db8b
2 changed files with 94 additions and 65 deletions

View file

@ -1,13 +1,12 @@
Kubernetes prompt for zsh # Kubernetes prompt for zsh
=========================
A Kubernetes (k8s) zsh prompt that displays the current cluster cluster A Kubernetes zsh prompt that displays the current cluster cluster
and the namespace. and the namespace.
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 NOTE: If you are not using zsh, check out [kube-ps1](https://github.com/jonmosco/kube-ps1)
as well as zsh. designed for bash as well as zsh.
## Requirements ## Requirements
@ -32,28 +31,33 @@ fast switching between clusters and namespaces.
The prompt layout is: The prompt layout is:
``` ```
(<logo>|<cluster>:<namespace>) (<symbol>|<cluster>:<namespace>)
``` ```
Supported platforms: ## Enabling
* k8s - Kubernetes
* ocp - OpenShift
## Install 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.
1. Clone this repository ```shell
2. Source the kube-ps1.zsh in your ~./.zshrc vim $HOME/.zshrc
ZSH:
``` ```
source path/kube-ps1.sh
PROMPT='$(kube_ps1) ' Add kube-ps1 to the list of enabled plugins:
```shell
plugins=(
git
kube-ps1
)
``` ```
## Colors ## Colors
The colors are of my opinion. Blue was used as the prefix to match the Kubernetes Blue was used as the prefix to match the Kubernetes color as closely as
color as closely as possible. Red was chosen as the cluster name to stand out, and cyan possible. Red was chosen as the cluster name to stand out, and cyan
for the namespace. These can of course be changed. for the namespace. These can of course be changed.
## Customization ## Customization
@ -62,14 +66,15 @@ The default settings can be overridden in ~/.zshrc
| Variable | Default | Meaning | | Variable | Default | Meaning |
| :------- | :-----: | ------- | | :------- | :-----: | ------- |
| `KUBE_PS1_DEFAULT` | `true` | Default settings for the prompt | | `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
| `KUBE_PS1_PREFIX` | `(` | Prompt opening character | | `KUBE_PS1_PREFIX` | `(` | Prompt opening character |
| `KUBE_PS1_DEFAULT_LABEL` | `⎈ ` | Default prompt symbol | | `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_SEPERATOR` | `\|` | Separator between symbol and cluster name |
| `KUBE_PS1_PLATFORM` | `kubectl` | Cluster type and binary to use |
| `KUBE_PS1_DIVIDER` | `:` | Separator between cluster and namespace | | `KUBE_PS1_DIVIDER` | `:` | Separator between cluster and namespace |
| `KUBE_PS1_SUFFIX` | `)` | Prompt closing character | | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
| `KUBE_PS1_DEFAULT_LABEL_IMG` | `false` | Use Kubernetes img as the label: ☸️ |
## Contributors ## Contributors

View file

@ -1,9 +1,10 @@
#!/bin/zsh #!/bin/zsh
# Kubernetes prompt helper for bash/zsh # Kubernetes prompt helper for bash/zsh
# ported to oh-my-zsh
# Displays current context and namespace # Displays current context and namespace
# Copyright 2017 Jon Mosco # Copyright 2018 Jon Mosco
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -21,32 +22,39 @@
[[ -n $DEBUG ]] && set -x [[ -n $DEBUG ]] && set -x
setopt PROMPT_SUBST setopt PROMPT_SUBST
add-zsh-hook precmd _kube_ps1_load autoload -U add-zsh-hook
add-zsh-hook precmd _kube_ps1_update_cache
zmodload zsh/stat zmodload zsh/stat
zmodload zsh/datetime
# Default values for the prompt # Default values for the prompt
# Override these values in ~/.zshrc or ~/.bashrc # Override these values in ~/.zshrc
KUBE_PS1_DEFAULT="${KUBE_PS1_DEFAULT:=true}" KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
KUBE_PS1_PREFIX="(" KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
KUBE_PS1_DEFAULT_LABEL="${KUBE_PS1_DEFAULT_LABEL:="⎈ "}" KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
KUBE_PS1_DEFAULT_LABEL_IMG="${KUBE_PS1_DEFAULT_LABEL_IMG:=false}" KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
KUBE_PS1_SEPERATOR="|" KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
KUBE_PS1_PLATFORM="${KUBE_PS1_PLATFORM:="kubectl"}" KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
KUBE_PS1_DIVIDER=":" KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
KUBE_PS1_SUFFIX=")" KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
KUBE_PS1_UNAME=$(uname) KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
KUBE_PS1_LAST_TIME=0 KUBE_PS1_LAST_TIME=0
kube_ps1_label () { _kube_ps1_binary_check() {
command -v "$1" >/dev/null
}
[[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == false ]] && return _kube_ps1_symbol() {
[[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
if [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == true ]]; then KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
local KUBE_LABEL="☸️ " KUBE_PS1_SYMBOL_IMG="\u2638 "
if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
fi fi
KUBE_PS1_DEFAULT_LABEL="${KUBE_LABEL}" echo "${KUBE_PS1_SYMBOL}"
} }
_kube_ps1_split() { _kube_ps1_split() {
@ -56,23 +64,45 @@ _kube_ps1_split() {
} }
_kube_ps1_file_newer_than() { _kube_ps1_file_newer_than() {
local mtime local mtime
local file=$1 local file=$1
local check_time=$2 local check_time=$2
mtime=$(stat +mtime "${file}")
[ "${mtime}" -gt "${check_time}" ] zmodload -e "zsh/stat"
if [[ "$?" -eq 0 ]]; then
mtime=$(stat +mtime "${file}")
elif stat -c "%s" /dev/null &> /dev/null; then
# GNU stat
mtime=$(stat -c %Y "${file}")
else
# BSD stat
mtime=$(stat -f %m "$file")
fi
[[ "${mtime}" -gt "${check_time}" ]]
} }
_kube_ps1_load() { _kube_ps1_update_cache() {
KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
# No ability to fetch context/namespace; display N/A.
KUBE_PS1_CONTEXT="BINARY-N/A"
KUBE_PS1_NAMESPACE="N/A"
return
fi
if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
# User changed KUBECONFIG; unconditionally refetch.
KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
_kube_ps1_get_context_ns
return
fi
# kubectl will read the environment variable $KUBECONFIG # kubectl will read the environment variable $KUBECONFIG
# otherwise set it to ~/.kube/config # otherwise set it to ~/.kube/config
KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}" local conf
for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
for conf in $(_kube_ps1_split : "${KUBECONFIG}"); do [[ -r "${conf}" ]] || continue
# TODO: check existence of $conf
if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
_kube_ps1_get_context_ns _kube_ps1_get_context_ns
return return
@ -83,26 +113,20 @@ _kube_ps1_load() {
_kube_ps1_get_context_ns() { _kube_ps1_get_context_ns() {
# Set the command time # Set the command time
KUBE_PS1_LAST_TIME=$(date +%s) KUBE_PS1_LAST_TIME=$EPOCHSECONDS
if [[ "${KUBE_PS1_DEFAULT}" == true ]]; then KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
local KUBE_BINARY="${KUBE_PS1_PLATFORM}" if [[ -z "${KUBE_PS1_CONTEXT}" ]]; then
elif [[ "${KUBE_PS1_DEFAULT}" == false ]] && [[ "${KUBE_PS1_PLATFORM}" == "kubectl" ]];then KUBE_PS1_CONTEXT="N/A"
local KUBE_BINARY="kubectl" KUBE_PS1_NAMESPACE="N/A"
elif [[ "${KUBE_PS1_PLATFORM}" == "oc" ]]; then return
local KUBE_BINARY="oc" 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}"
fi fi
KUBE_PS1_CONTEXT="$(${KUBE_BINARY} config current-context)"
KUBE_PS1_NAMESPACE="$(${KUBE_BINARY} config view --minify --output 'jsonpath={..namespace}')"
# Set namespace to default if it is not defined
KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
} }
# source our symbol
kube_ps1_label
# Build our prompt # Build our prompt
kube_ps1 () { kube_ps1 () {
local reset_color="%f" local reset_color="%f"
@ -111,7 +135,7 @@ kube_ps1 () {
local cyan="%F{cyan}" local cyan="%F{cyan}"
KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX" KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
KUBE_PS1+="${blue}$KUBE_PS1_DEFAULT_LABEL" KUBE_PS1+="${blue}$(_kube_ps1_symbol)"
KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR" KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
KUBE_PS1+="${red}$KUBE_PS1_CONTEXT${reset_color}" KUBE_PS1+="${red}$KUBE_PS1_CONTEXT${reset_color}"
KUBE_PS1+="$KUBE_PS1_DIVIDER" KUBE_PS1+="$KUBE_PS1_DIVIDER"