summaryrefslogtreecommitdiff
path: root/plugins/kube-ps1
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/kube-ps1')
-rw-r--r--plugins/kube-ps1/README.md81
-rw-r--r--plugins/kube-ps1/kube-ps1.plugin.zsh147
2 files changed, 228 insertions, 0 deletions
diff --git a/plugins/kube-ps1/README.md b/plugins/kube-ps1/README.md
new file mode 100644
index 000000000..fcb73cd2d
--- /dev/null
+++ b/plugins/kube-ps1/README.md
@@ -0,0 +1,81 @@
+# Kubernetes prompt for zsh
+
+A Kubernetes zsh prompt that displays the current cluster cluster
+and the namespace.
+
+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.
+
+## Requirements
+
+The default prompt assumes you have the kubectl command line utility installed. It
+can be obtained here:
+
+[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:
+
+[OC Client Tools](https://www.openshift.org/download.html)
+
+## Helper utilities
+
+There are several great tools that make using kubectl very enjoyable.
+
+[kubectx and kubenx](https://github.com/ahmetb/kubectx) are great for
+fast switching between clusters and namespaces.
+
+## Prompt Structure
+
+The prompt layout is:
+
+```
+(<symbol>|<cluster>:<namespace>)
+```
+
+## Enabling
+
+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
+```
+
+Add kube-ps1 to the list of enabled plugins:
+
+```shell
+plugins=(
+ git
+ kube-ps1
+)
+```
+
+## 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. These can of course be changed.
+
+## Customization
+
+The default settings can be overridden in ~/.zshrc
+
+| Variable | Default | Meaning |
+| :------- | :-----: | ------- |
+| `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
+| `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_SUFFIX` | `)` | Prompt closing character |
+
+## Contributors
+
+Jared Yanovich
diff --git a/plugins/kube-ps1/kube-ps1.plugin.zsh b/plugins/kube-ps1/kube-ps1.plugin.zsh
new file mode 100644
index 000000000..df7277a26
--- /dev/null
+++ b/plugins/kube-ps1/kube-ps1.plugin.zsh
@@ -0,0 +1,147 @@
+#!/bin/zsh
+
+# Kubernetes prompt helper for bash/zsh
+# ported to oh-my-zsh
+# Displays current context and namespace
+
+# Copyright 2018 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# 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
+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_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
+KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
+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_LAST_TIME=0
+
+_kube_ps1_binary_check() {
+ 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 "
+
+ if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
+ KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
+ fi
+
+ echo "${KUBE_PS1_SYMBOL}"
+}
+
+_kube_ps1_split() {
+ type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
+ local IFS=$1
+ echo $2
+}
+
+_kube_ps1_file_newer_than() {
+ local mtime
+ local file=$1
+ local check_time=$2
+
+ 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_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
+ # otherwise set it to ~/.kube/config
+ local conf
+ for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
+ [[ -r "${conf}" ]] || continue
+ if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
+ _kube_ps1_get_context_ns
+ return
+ fi
+ done
+}
+
+_kube_ps1_get_context_ns() {
+
+ # 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}"
+ fi
+}
+
+# Build our prompt
+kube_ps1 () {
+ local reset_color="%f"
+ local blue="%F{blue}"
+ local red="%F{red}"
+ local cyan="%F{cyan}"
+
+ KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
+ KUBE_PS1+="${blue}$(_kube_ps1_symbol)"
+ KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
+ KUBE_PS1+="${red}$KUBE_PS1_CONTEXT${reset_color}"
+ KUBE_PS1+="$KUBE_PS1_DIVIDER"
+ KUBE_PS1+="${cyan}$KUBE_PS1_NAMESPACE${reset_color}"
+ KUBE_PS1+="$KUBE_PS1_SUFFIX"
+
+ echo "${KUBE_PS1}"
+
+}