e6e2b3d42fc7e88d293d380f7861fa87e0856e62

Author
Jon Mosco <1970496+jonmosco@users.noreply.github.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

kube-ps1 zsh prompt helper (#6507)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/kube-ps1/README.md b/plugins/kube-ps1/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..a572773a3f891a1859b671763f5f0c86d97a80df
  4--- /dev/null
  5+++ b/plugins/kube-ps1/README.md
  6@@ -0,0 +1,76 @@
  7+Kubernetes prompt for zsh
  8+=========================
  9+
 10+A Kubernetes (k8s) zsh prompt that displays the current cluster cluster
 11+and the namespace.
 12+
 13+Inspired by several tools used to simplify usage of kubectl
 14+
 15+NOTE: If you are not using zsh, check out [kube-ps1](https://github.com/jonmosco/kube-ps1) designed for bash
 16+as well as zsh.
 17+
 18+## Requirements
 19+
 20+The default prompt assumes you have the kubectl command line utility installed.  It
 21+can be obtained here:
 22+
 23+[Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
 24+
 25+If using this with OpenShift, the oc tool needs installed.  It can be obtained from here:
 26+
 27+[OC Client Tools](https://www.openshift.org/download.html)
 28+
 29+## Helper utilities
 30+
 31+There are several great tools that make using kubectl very enjoyable.
 32+
 33+[kubectx and kubenx](https://github.com/ahmetb/kubectx) are great for
 34+fast switching between clusters and namespaces.
 35+
 36+## Prompt Structure
 37+
 38+The prompt layout is:
 39+
 40+```
 41+(<logo>|<cluster>:<namespace>)
 42+```
 43+
 44+Supported platforms:
 45+* k8s - Kubernetes
 46+* ocp - OpenShift
 47+
 48+## Install
 49+
 50+1. Clone this repository
 51+2. Source the kube-ps1.zsh in your ~./.zshrc
 52+
 53+ZSH:
 54+```
 55+source path/kube-ps1.sh
 56+PROMPT='$(kube_ps1) '
 57+```
 58+
 59+## Colors
 60+
 61+The colors are of my opinion. Blue was used as the prefix to match the Kubernetes
 62+color as closely as possible. Red was chosen as the cluster name to stand out, and cyan
 63+for the namespace.  These can of course be changed.
 64+
 65+## Customization
 66+
 67+The default settings can be overridden in ~/.zshrc
 68+
 69+| Variable | Default | Meaning |
 70+| :------- | :-----: | ------- |
 71+| `KUBE_PS1_DEFAULT` | `true` | Default settings for the prompt |
 72+| `KUBE_PS1_PREFIX` | `(` | Prompt opening character  |
 73+| `KUBE_PS1_DEFAULT_LABEL` | `⎈ ` | Default prompt symbol |
 74+| `KUBE_PS1_SEPERATOR` | `\|` | Separator between symbol and cluster name |
 75+| `KUBE_PS1_PLATFORM` | `kubectl` | Cluster type and binary to use |
 76+| `KUBE_PS1_DIVIDER` | `:` | Separator between cluster and namespace |
 77+| `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
 78+| `KUBE_PS1_DEFAULT_LABEL_IMG` | `false` | Use Kubernetes img as the label: ☸️  |
 79+
 80+## Contributors
 81+
 82+Jared Yanovich
 83diff --git a/plugins/kube-ps1/kube-ps1.zsh b/plugins/kube-ps1/kube-ps1.zsh
 84new file mode 100644
 85index 0000000000000000000000000000000000000000..e1cb4339de6dbf5f1d0e3e80486991253b9c108f
 86--- /dev/null
 87+++ b/plugins/kube-ps1/kube-ps1.zsh
 88@@ -0,0 +1,123 @@
 89+#!/bin/zsh
 90+
 91+# Kubernetes prompt helper for bash/zsh
 92+# Displays current context and namespace
 93+
 94+# Copyright 2017 Jon Mosco
 95+#
 96+#  Licensed under the Apache License, Version 2.0 (the "License");
 97+# you may not use this file except in compliance with the License.
 98+# You may obtain a copy of the License at
 99+#
100+#     http://www.apache.org/licenses/LICENSE-2.0
101+#
102+# Unless required by applicable law or agreed to in writing, software
103+# distributed under the License is distributed on an "AS IS" BASIS,
104+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
105+# See the License for the specific language governing permissions and
106+# limitations under the License.
107+
108+# Debug
109+[[ -n $DEBUG ]] && set -x
110+
111+setopt PROMPT_SUBST
112+add-zsh-hook precmd _kube_ps1_load
113+zmodload zsh/stat
114+
115+# Default values for the prompt
116+# Override these values in ~/.zshrc or ~/.bashrc
117+KUBE_PS1_DEFAULT="${KUBE_PS1_DEFAULT:=true}"
118+KUBE_PS1_PREFIX="("
119+KUBE_PS1_DEFAULT_LABEL="${KUBE_PS1_DEFAULT_LABEL:="⎈ "}"
120+KUBE_PS1_DEFAULT_LABEL_IMG="${KUBE_PS1_DEFAULT_LABEL_IMG:=false}"
121+KUBE_PS1_SEPERATOR="|"
122+KUBE_PS1_PLATFORM="${KUBE_PS1_PLATFORM:="kubectl"}"
123+KUBE_PS1_DIVIDER=":"
124+KUBE_PS1_SUFFIX=")"
125+KUBE_PS1_UNAME=$(uname)
126+KUBE_PS1_LAST_TIME=0
127+
128+kube_ps1_label () {
129+
130+  [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == false ]] && return
131+
132+  if [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == true ]]; then
133+    local KUBE_LABEL="☸️ "
134+  fi
135+
136+  KUBE_PS1_DEFAULT_LABEL="${KUBE_LABEL}"
137+
138+}
139+
140+_kube_ps1_split() {
141+  type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
142+  local IFS=$1
143+  echo $2
144+}
145+
146+_kube_ps1_file_newer_than() {
147+
148+  local mtime
149+  local file=$1
150+  local check_time=$2
151+  mtime=$(stat +mtime "${file}")
152+
153+  [ "${mtime}" -gt "${check_time}" ]
154+
155+}
156+
157+_kube_ps1_load() {
158+  # kubectl will read the environment variable $KUBECONFIG
159+  # otherwise set it to ~/.kube/config
160+  KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
161+
162+  for conf in $(_kube_ps1_split : "${KUBECONFIG}"); do
163+    # TODO: check existence of $conf
164+    if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
165+      _kube_ps1_get_context_ns
166+      return
167+    fi
168+  done
169+}
170+
171+_kube_ps1_get_context_ns() {
172+
173+  # Set the command time
174+  KUBE_PS1_LAST_TIME=$(date +%s)
175+
176+  if [[ "${KUBE_PS1_DEFAULT}" == true ]]; then
177+    local KUBE_BINARY="${KUBE_PS1_PLATFORM}"
178+  elif [[ "${KUBE_PS1_DEFAULT}" == false ]] && [[ "${KUBE_PS1_PLATFORM}" == "kubectl" ]];then
179+    local KUBE_BINARY="kubectl"
180+  elif [[ "${KUBE_PS1_PLATFORM}" == "oc" ]]; then
181+    local KUBE_BINARY="oc"
182+  fi
183+
184+  KUBE_PS1_CONTEXT="$(${KUBE_BINARY} config current-context)"
185+  KUBE_PS1_NAMESPACE="$(${KUBE_BINARY} config view --minify --output 'jsonpath={..namespace}')"
186+  # Set namespace to default if it is not defined
187+  KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"