kube-ps1.plugin.zsh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_ENABLED=true
  38. KUBE_PS1_COLOR_SYMBOL="%F{blue}"
  39. KUBE_PS1_COLOR_CONTEXT="%F{red}"
  40. KUBE_PS1_COLOR_NS="%F{cyan}"
  41. _kube_ps1_binary_check() {
  42. command -v "$1" >/dev/null
  43. }
  44. _kube_ps1_symbol() {
  45. [[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
  46. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
  47. KUBE_PS1_SYMBOL_IMG="\u2638 "
  48. if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
  49. KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
  50. fi
  51. echo "${KUBE_PS1_SYMBOL}"
  52. }
  53. _kube_ps1_split() {
  54. type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
  55. local IFS=$1
  56. echo $2
  57. }
  58. _kube_ps1_file_newer_than() {
  59. local mtime
  60. local file=$1
  61. local check_time=$2
  62. zmodload -e "zsh/stat"
  63. if [[ "$?" -eq 0 ]]; then
  64. mtime=$(stat +mtime "${file}")
  65. elif stat -c "%s" /dev/null &> /dev/null; then
  66. # GNU stat
  67. mtime=$(stat -c %Y "${file}")
  68. else
  69. # BSD stat
  70. mtime=$(stat -f %m "$file")
  71. fi
  72. [[ "${mtime}" -gt "${check_time}" ]]
  73. }
  74. _kube_ps1_update_cache() {
  75. KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
  76. if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
  77. # No ability to fetch context/namespace; display N/A.
  78. KUBE_PS1_CONTEXT="BINARY-N/A"
  79. KUBE_PS1_NAMESPACE="N/A"
  80. return
  81. fi
  82. if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
  83. # User changed KUBECONFIG; unconditionally refetch.
  84. KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
  85. _kube_ps1_get_context_ns
  86. return
  87. fi
  88. # kubectl will read the environment variable $KUBECONFIG
  89. # otherwise set it to ~/.kube/config
  90. local conf
  91. for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
  92. [[ -r "${conf}" ]] || continue
  93. if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
  94. _kube_ps1_get_context_ns
  95. return
  96. fi
  97. done
  98. }
  99. _kube_ps1_get_context_ns() {
  100. # Set the command time
  101. KUBE_PS1_LAST_TIME=$EPOCHSECONDS
  102. KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
  103. if [[ -z "${KUBE_PS1_CONTEXT}" ]]; then
  104. KUBE_PS1_CONTEXT="N/A"
  105. KUBE_PS1_NAMESPACE="N/A"
  106. return
  107. elif [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
  108. KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
  109. # Set namespace to 'default' if it is not defined
  110. KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
  111. fi
  112. }
  113. # function to disable the prompt on the current shell
  114. kubeon(){
  115. KUBE_PS1_ENABLED=true
  116. }
  117. # function to disable the prompt on the current shell
  118. kubeoff(){
  119. KUBE_PS1_ENABLED=false
  120. }
  121. # Build our prompt
  122. kube_ps1 () {
  123. local reset_color="%{$reset_color%}"
  124. [[ ${KUBE_PS1_ENABLED} != 'true' ]] && return
  125. KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
  126. KUBE_PS1+="${KUBE_PS1_COLOR_SYMBOL}$(_kube_ps1_symbol)"
  127. KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
  128. KUBE_PS1+="${KUBE_PS1_COLOR_CONTEXT}$KUBE_PS1_CONTEXT${reset_color}"
  129. KUBE_PS1+="$KUBE_PS1_DIVIDER"
  130. KUBE_PS1+="${KUBE_PS1_COLOR_NS}$KUBE_PS1_NAMESPACE${reset_color}"
  131. KUBE_PS1+="$KUBE_PS1_SUFFIX"
  132. echo "${KUBE_PS1}"
  133. }