kube-ps1.plugin.zsh 11 KB

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