kube-ps1.plugin.zsh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/zsh
  2. # Kubernetes prompt helper for bash/zsh
  3. # ported to oh-my-zsh
  4. # Displays current context and namespace
  5. # Copyright 2018 Jon Mosco
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # https://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # Debug
  19. [[ -n $DEBUG ]] && set -x
  20. setopt PROMPT_SUBST
  21. autoload -U add-zsh-hook
  22. add-zsh-hook precmd _kube_ps1_update_cache
  23. zmodload zsh/stat
  24. zmodload zsh/datetime
  25. # Default values for the prompt
  26. # Override these values in ~/.zshrc
  27. KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
  28. KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
  29. KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
  30. KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
  31. KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
  32. KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
  33. KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
  34. KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
  35. KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
  36. KUBE_PS1_LAST_TIME=0
  37. _kube_ps1_binary_check() {
  38. command -v "$1" >/dev/null
  39. }
  40. _kube_ps1_symbol() {
  41. [[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
  42. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
  43. KUBE_PS1_SYMBOL_IMG="\u2638 "
  44. if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
  45. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
  46. fi
  47. echo "${KUBE_PS1_SYMBOL}"
  48. }
  49. _kube_ps1_split() {
  50. type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
  51. local IFS=$1
  52. echo $2
  53. }
  54. _kube_ps1_file_newer_than() {
  55. local mtime
  56. local file=$1
  57. local check_time=$2
  58. zmodload -e "zsh/stat"
  59. if [[ "$?" -eq 0 ]]; then
  60. mtime=$(stat +mtime "${file}")
  61. elif stat -c "%s" /dev/null &> /dev/null; then
  62. # GNU stat
  63. mtime=$(stat -c %Y "${file}")
  64. else
  65. # BSD stat
  66. mtime=$(stat -f %m "$file")
  67. fi
  68. [[ "${mtime}" -gt "${check_time}" ]]
  69. }
  70. _kube_ps1_update_cache() {
  71. KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
  72. if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
  73. # No ability to fetch context/namespace; display N/A.
  74. KUBE_PS1_CONTEXT="BINARY-N/A"
  75. KUBE_PS1_NAMESPACE="N/A"
  76. return
  77. fi
  78. if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
  79. # User changed KUBECONFIG; unconditionally refetch.
  80. KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
  81. _kube_ps1_get_context_ns
  82. return
  83. fi
  84. # kubectl will read the environment variable $KUBECONFIG
  85. # otherwise set it to ~/.kube/config
  86. local conf
  87. for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
  88. [[ -r "${conf}" ]] || continue
  89. if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
  90. _kube_ps1_get_context_ns
  91. return
  92. fi
  93. done
  94. }
  95. _kube_ps1_get_context_ns() {
  96. # Set the command time
  97. KUBE_PS1_LAST_TIME=$EPOCHSECONDS
  98. KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
  99. if [[ -z "${KUBE_PS1_CONTEXT}" ]]; then
  100. KUBE_PS1_CONTEXT="N/A"
  101. KUBE_PS1_NAMESPACE="N/A"
  102. return
  103. elif [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
  104. KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
  105. # Set namespace to 'default' if it is not defined
  106. KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
  107. fi
  108. }
  109. # Build our prompt
  110. kube_ps1 () {
  111. local reset_color="%f"
  112. local blue="%F{blue}"
  113. local red="%F{red}"
  114. local cyan="%F{cyan}"
  115. KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
  116. KUBE_PS1+="${blue}$(_kube_ps1_symbol)"
  117. KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
  118. KUBE_PS1+="${red}$KUBE_PS1_CONTEXT${reset_color}"
  119. KUBE_PS1+="$KUBE_PS1_DIVIDER"
  120. KUBE_PS1+="${cyan}$KUBE_PS1_NAMESPACE${reset_color}"
  121. KUBE_PS1+="$KUBE_PS1_SUFFIX"
  122. echo "${KUBE_PS1}"
  123. }