kube-ps1.plugin.zsh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #!/bin/bash
  2. # Kubernetes prompt helper for bash/zsh
  3. # Displays current context and namespace
  4. # Copyright 2019 Jon Mosco
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # Debug
  18. [[ -n $DEBUG ]] && set -x
  19. # Default values for the prompt
  20. # Override these values in ~/.zshrc or ~/.bashrc
  21. KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
  22. KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
  23. KUBE_PS1_SYMBOL_DEFAULT=${KUBE_PS1_SYMBOL_DEFAULT:-$'\u2388 '}
  24. KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
  25. KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
  26. KUBE_PS1_CONTEXT_ENABLE="${KUBE_PS1_CONTEXT_ENABLE:-true}"
  27. KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
  28. KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
  29. KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
  30. KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
  31. KUBE_PS1_SYMBOL_COLOR="${KUBE_PS1_SYMBOL_COLOR-blue}"
  32. KUBE_PS1_CTX_COLOR="${KUBE_PS1_CTX_COLOR-red}"
  33. KUBE_PS1_NS_COLOR="${KUBE_PS1_NS_COLOR-cyan}"
  34. KUBE_PS1_BG_COLOR="${KUBE_PS1_BG_COLOR}"
  35. KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
  36. KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
  37. KUBE_PS1_LAST_TIME=0
  38. KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}"
  39. KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"
  40. # Determine our shell
  41. if [ "${ZSH_VERSION-}" ]; then
  42. KUBE_PS1_SHELL="zsh"
  43. elif [ "${BASH_VERSION-}" ]; then
  44. KUBE_PS1_SHELL="bash"
  45. fi
  46. _kube_ps1_init() {
  47. [[ -f "${KUBE_PS1_DISABLE_PATH}" ]] && KUBE_PS1_ENABLED=off
  48. case "${KUBE_PS1_SHELL}" in
  49. "zsh")
  50. _KUBE_PS1_OPEN_ESC="%{"
  51. _KUBE_PS1_CLOSE_ESC="%}"
  52. _KUBE_PS1_DEFAULT_BG="%k"
  53. _KUBE_PS1_DEFAULT_FG="%f"
  54. setopt PROMPT_SUBST
  55. autoload -U add-zsh-hook
  56. add-zsh-hook precmd _kube_ps1_update_cache
  57. zmodload -F zsh/stat b:zstat
  58. zmodload zsh/datetime
  59. ;;
  60. "bash")
  61. _KUBE_PS1_OPEN_ESC=$'\001'
  62. _KUBE_PS1_CLOSE_ESC=$'\002'
  63. _KUBE_PS1_DEFAULT_BG=$'\033[49m'
  64. _KUBE_PS1_DEFAULT_FG=$'\033[39m'
  65. [[ $PROMPT_COMMAND =~ _kube_ps1_update_cache ]] || PROMPT_COMMAND="_kube_ps1_update_cache;${PROMPT_COMMAND:-:}"
  66. ;;
  67. esac
  68. }
  69. _kube_ps1_color_fg() {
  70. local KUBE_PS1_FG_CODE
  71. case "${1}" in
  72. black) KUBE_PS1_FG_CODE=0;;
  73. red) KUBE_PS1_FG_CODE=1;;
  74. green) KUBE_PS1_FG_CODE=2;;
  75. yellow) KUBE_PS1_FG_CODE=3;;
  76. blue) KUBE_PS1_FG_CODE=4;;
  77. magenta) KUBE_PS1_FG_CODE=5;;
  78. cyan) KUBE_PS1_FG_CODE=6;;
  79. white) KUBE_PS1_FG_CODE=7;;
  80. # 256
  81. [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}";;
  82. *) KUBE_PS1_FG_CODE=default
  83. esac
  84. if [[ "${KUBE_PS1_FG_CODE}" == "default" ]]; then
  85. KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_FG}"
  86. return
  87. elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
  88. KUBE_PS1_FG_CODE="%F{$KUBE_PS1_FG_CODE}"
  89. elif [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
  90. if tput setaf 1 &> /dev/null; then
  91. KUBE_PS1_FG_CODE="$(tput setaf ${KUBE_PS1_FG_CODE})"
  92. elif [[ $KUBE_PS1_FG_CODE -ge 0 ]] && [[ $KUBE_PS1_FG_CODE -le 256 ]]; then
  93. KUBE_PS1_FG_CODE="\033[38;5;${KUBE_PS1_FG_CODE}m"
  94. else
  95. KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_FG}"
  96. fi
  97. fi
  98. echo ${_KUBE_PS1_OPEN_ESC}${KUBE_PS1_FG_CODE}${_KUBE_PS1_CLOSE_ESC}
  99. }
  100. _kube_ps1_color_bg() {
  101. local KUBE_PS1_BG_CODE
  102. case "${1}" in
  103. black) KUBE_PS1_BG_CODE=0;;
  104. red) KUBE_PS1_BG_CODE=1;;
  105. green) KUBE_PS1_BG_CODE=2;;
  106. yellow) KUBE_PS1_BG_CODE=3;;
  107. blue) KUBE_PS1_BG_CODE=4;;
  108. magenta) KUBE_PS1_BG_CODE=5;;
  109. cyan) KUBE_PS1_BG_CODE=6;;
  110. white) KUBE_PS1_BG_CODE=7;;
  111. # 256
  112. [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}";;
  113. *) KUBE_PS1_BG_CODE=$'\033[0m';;
  114. esac
  115. if [[ "${KUBE_PS1_BG_CODE}" == "default" ]]; then
  116. KUBE_PS1_FG_CODE="${_KUBE_PS1_DEFAULT_BG}"
  117. return
  118. elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
  119. KUBE_PS1_BG_CODE="%K{$KUBE_PS1_BG_CODE}"
  120. elif [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
  121. if tput setaf 1 &> /dev/null; then
  122. KUBE_PS1_BG_CODE="$(tput setab ${KUBE_PS1_BG_CODE})"
  123. elif [[ $KUBE_PS1_BG_CODE -ge 0 ]] && [[ $KUBE_PS1_BG_CODE -le 256 ]]; then
  124. KUBE_PS1_BG_CODE="\033[48;5;${KUBE_PS1_BG_CODE}m"
  125. else
  126. KUBE_PS1_BG_CODE="${DEFAULT_BG}"
  127. fi
  128. fi
  129. echo ${OPEN_ESC}${KUBE_PS1_BG_CODE}${CLOSE_ESC}
  130. }
  131. _kube_ps1_binary_check() {
  132. command -v $1 >/dev/null
  133. }
  134. _kube_ps1_symbol() {
  135. [[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
  136. case "${KUBE_PS1_SHELL}" in
  137. bash)
  138. if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388 ' != "\\u2388 " ]]; then
  139. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
  140. # KUBE_PS1_SYMBOL=$'\u2388 '
  141. KUBE_PS1_SYMBOL_IMG=$'\u2638 '
  142. else
  143. KUBE_PS1_SYMBOL=$'\xE2\x8E\x88 '
  144. KUBE_PS1_SYMBOL_IMG=$'\xE2\x98\xB8 '
  145. fi
  146. ;;
  147. zsh)
  148. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
  149. KUBE_PS1_SYMBOL_IMG="\u2638 ";;
  150. *)
  151. KUBE_PS1_SYMBOL="k8s"
  152. esac
  153. if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
  154. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
  155. fi
  156. echo "${KUBE_PS1_SYMBOL}"
  157. }
  158. _kube_ps1_split() {
  159. type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
  160. local IFS=$1
  161. echo $2
  162. }
  163. _kube_ps1_file_newer_than() {
  164. local mtime
  165. local file=$1
  166. local check_time=$2
  167. if [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
  168. mtime=$(zstat -L +mtime "${file}")
  169. elif stat -c "%s" /dev/null &> /dev/null; then
  170. # GNU stat
  171. mtime=$(stat -L -c %Y "${file}")
  172. else
  173. # BSD stat
  174. mtime=$(stat -L -f %m "$file")
  175. fi
  176. [[ "${mtime}" -gt "${check_time}" ]]
  177. }
  178. _kube_ps1_update_cache() {
  179. local return_code=$?
  180. [[ "${KUBE_PS1_ENABLED}" == "off" ]] && return $return_code
  181. if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
  182. # No ability to fetch context/namespace; display N/A.
  183. KUBE_PS1_CONTEXT="BINARY-N/A"
  184. KUBE_PS1_NAMESPACE="N/A"
  185. return
  186. fi
  187. if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
  188. # User changed KUBECONFIG; unconditionally refetch.
  189. KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
  190. _kube_ps1_get_context_ns
  191. return
  192. fi
  193. # kubectl will read the environment variable $KUBECONFIG
  194. # otherwise set it to ~/.kube/config
  195. local conf
  196. for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
  197. [[ -r "${conf}" ]] || continue
  198. if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
  199. _kube_ps1_get_context_ns
  200. return
  201. fi
  202. done
  203. return $return_code
  204. }
  205. _kube_ps1_get_context() {
  206. if [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
  207. KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
  208. # Set namespace to 'N/A' if it is not defined
  209. KUBE_PS1_CONTEXT="${KUBE_PS1_CONTEXT:-N/A}"
  210. if [[ ! -z "${KUBE_PS1_CLUSTER_FUNCTION}" ]]; then
  211. KUBE_PS1_CONTEXT=$($KUBE_PS1_CLUSTER_FUNCTION $KUBE_PS1_CONTEXT)
  212. fi
  213. fi
  214. }
  215. _kube_ps1_get_ns() {
  216. if [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
  217. KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
  218. # Set namespace to 'default' if it is not defined
  219. KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
  220. if [[ ! -z "${KUBE_PS1_NAMESPACE_FUNCTION}" ]]; then
  221. KUBE_PS1_NAMESPACE=$($KUBE_PS1_NAMESPACE_FUNCTION $KUBE_PS1_NAMESPACE)
  222. fi
  223. fi
  224. }
  225. _kube_ps1_get_context_ns() {
  226. # Set the command time
  227. if [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
  228. if ((BASH_VERSINFO[0] >= 4 && BASH_VERSINFO[1] >= 2)); then
  229. KUBE_PS1_LAST_TIME=$(printf '%(%s)T')
  230. else
  231. KUBE_PS1_LAST_TIME=$(date +%s)
  232. fi
  233. elif [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
  234. KUBE_PS1_LAST_TIME=$EPOCHSECONDS
  235. fi
  236. _kube_ps1_get_context
  237. _kube_ps1_get_ns
  238. }
  239. # Set kube-ps1 shell defaults
  240. _kube_ps1_init
  241. _kubeon_usage() {
  242. cat <<"EOF"
  243. Toggle kube-ps1 prompt on
  244. Usage: kubeon [-g | --global] [-h | --help]
  245. With no arguments, turn off kube-ps1 status for this shell instance (default).
  246. -g --global turn on kube-ps1 status globally
  247. -h --help print this message
  248. EOF
  249. }
  250. _kubeoff_usage() {
  251. cat <<"EOF"
  252. Toggle kube-ps1 prompt off
  253. Usage: kubeoff [-g | --global] [-h | --help]
  254. With no arguments, turn off kube-ps1 status for this shell instance (default).
  255. -g --global turn off kube-ps1 status globally
  256. -h --help print this message
  257. EOF
  258. }
  259. kubeon() {
  260. if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
  261. _kubeon_usage
  262. elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
  263. rm -f -- "${KUBE_PS1_DISABLE_PATH}"
  264. elif [[ "$#" -ne 0 ]]; then
  265. echo -e "error: unrecognized flag ${1}\\n"
  266. _kubeon_usage
  267. return
  268. fi
  269. KUBE_PS1_ENABLED=on
  270. }
  271. kubeoff() {
  272. if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
  273. _kubeoff_usage
  274. elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
  275. mkdir -p -- "$(dirname "${KUBE_PS1_DISABLE_PATH}")"
  276. touch -- "${KUBE_PS1_DISABLE_PATH}"
  277. elif [[ $# -ne 0 ]]; then
  278. echo "error: unrecognized flag ${1}" >&2
  279. _kubeoff_usage
  280. return
  281. fi
  282. KUBE_PS1_ENABLED=off
  283. }
  284. # Build our prompt
  285. kube_ps1() {
  286. [[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
  287. [[ -z "${KUBE_PS1_CONTEXT}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]] && return
  288. local KUBE_PS1
  289. local KUBE_PS1_RESET_COLOR="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_FG}${_KUBE_PS1_CLOSE_ESC}"
  290. # Background Color
  291. [[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg ${KUBE_PS1_BG_COLOR})"
  292. # Prefix
  293. [[ -n "${KUBE_PS1_PREFIX}" ]] && KUBE_PS1+="${KUBE_PS1_PREFIX}"
  294. # Symbol
  295. KUBE_PS1+="$(_kube_ps1_color_fg $KUBE_PS1_SYMBOL_COLOR)$(_kube_ps1_symbol)${KUBE_PS1_RESET_COLOR}"
  296. if [[ -n "${KUBE_PS1_SEPARATOR}" ]] && [[ "${KUBE_PS1_SYMBOL_ENABLE}" == true ]]; then
  297. KUBE_PS1+="${KUBE_PS1_SEPARATOR}"
  298. fi
  299. # Context
  300. if [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
  301. KUBE_PS1+="$(_kube_ps1_color_fg $KUBE_PS1_CTX_COLOR)${KUBE_PS1_CONTEXT}${KUBE_PS1_RESET_COLOR}"
  302. fi
  303. # Namespace
  304. if [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
  305. if [[ -n "${KUBE_PS1_DIVIDER}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then
  306. KUBE_PS1+="${KUBE_PS1_DIVIDER}"
  307. fi
  308. KUBE_PS1+="$(_kube_ps1_color_fg ${KUBE_PS1_NS_COLOR})${KUBE_PS1_NAMESPACE}${KUBE_PS1_RESET_COLOR}"
  309. fi
  310. # Suffix
  311. [[ -n "${KUBE_PS1_SUFFIX}" ]] && KUBE_PS1+="${KUBE_PS1_SUFFIX}"
  312. # Close Background color if defined
  313. [[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_BG}${_KUBE_PS1_CLOSE_ESC}"
  314. echo "${KUBE_PS1}"
  315. }