kube-ps1.plugin.zsh 4.31 KB
Newer Older
Jon Mosco's avatar
Jon Mosco committed
1
2
3
#!/bin/zsh

# Kubernetes prompt helper for bash/zsh
4
# ported to oh-my-zsh
Jon Mosco's avatar
Jon Mosco committed
5
6
# Displays current context and namespace

7
# Copyright 2018 Jon Mosco
Jon Mosco's avatar
Jon Mosco committed
8
9
10
11
12
#
#  Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
Janosch Schwalm's avatar
Janosch Schwalm committed
13
#     https://www.apache.org/licenses/LICENSE-2.0
Jon Mosco's avatar
Jon Mosco committed
14
15
16
17
18
19
20
21
22
23
24
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Debug
[[ -n $DEBUG ]] && set -x

setopt PROMPT_SUBST
25
26
autoload -U add-zsh-hook
add-zsh-hook precmd _kube_ps1_update_cache
Jon Mosco's avatar
Jon Mosco committed
27
zmodload zsh/stat
28
zmodload zsh/datetime
Jon Mosco's avatar
Jon Mosco committed
29
30

# Default values for the prompt
31
32
33
34
35
36
37
38
39
40
# Override these values in ~/.zshrc
KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
Jon Mosco's avatar
Jon Mosco committed
41
KUBE_PS1_LAST_TIME=0
42
43
44
45
46
KUBE_PS1_ENABLED=true

KUBE_PS1_COLOR_SYMBOL="%F{blue}"
KUBE_PS1_COLOR_CONTEXT="%F{red}"
KUBE_PS1_COLOR_NS="%F{cyan}"
Jon Mosco's avatar
Jon Mosco committed
47

48
49
50
_kube_ps1_binary_check() {
  command -v "$1" >/dev/null
}
Jon Mosco's avatar
Jon Mosco committed
51

52
53
_kube_ps1_symbol() {
  [[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
Jon Mosco's avatar
Jon Mosco committed
54

55
56
  KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
  KUBE_PS1_SYMBOL_IMG="\u2638 "
Jon Mosco's avatar
Jon Mosco committed
57

58
59
60
  if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
    KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
  fi
Jon Mosco's avatar
Jon Mosco committed
61

62
  echo "${KUBE_PS1_SYMBOL}"
Jon Mosco's avatar
Jon Mosco committed
63
64
65
66
67
68
69
70
71
72
73
74
75
}

_kube_ps1_split() {
  type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
  local IFS=$1
  echo $2
}

_kube_ps1_file_newer_than() {
  local mtime
  local file=$1
  local check_time=$2

76
77
78
79
80
81
82
83
84
85
  zmodload -e "zsh/stat"
  if [[ "$?" -eq 0 ]]; then
    mtime=$(stat +mtime "${file}")
  elif stat -c "%s" /dev/null &> /dev/null; then
    # GNU stat
    mtime=$(stat -c %Y "${file}")
  else
    # BSD stat
    mtime=$(stat -f %m "$file")
  fi
Jon Mosco's avatar
Jon Mosco committed
86

87
  [[ "${mtime}" -gt "${check_time}" ]]
Jon Mosco's avatar
Jon Mosco committed
88
89
}

90
_kube_ps1_update_cache() {
Jon Mosco's avatar
Jon Mosco committed
91
  KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
92
93
94
95
96
97
  if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
    # No ability to fetch context/namespace; display N/A.
    KUBE_PS1_CONTEXT="BINARY-N/A"
    KUBE_PS1_NAMESPACE="N/A"
    return
  fi
Jon Mosco's avatar
Jon Mosco committed
98

99
100
101
102
103
104
105
106
107
108
109
110
  if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
    # User changed KUBECONFIG; unconditionally refetch.
    KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
    _kube_ps1_get_context_ns
    return
  fi

  # kubectl will read the environment variable $KUBECONFIG
  # otherwise set it to ~/.kube/config
  local conf
  for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
    [[ -r "${conf}" ]] || continue
Jon Mosco's avatar
Jon Mosco committed
111
112
113
114
115
116
117
118
119
120
    if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
      _kube_ps1_get_context_ns
      return
    fi
  done
}

_kube_ps1_get_context_ns() {

  # Set the command time
121
122
123
124
125
126
127
128
129
130
131
  KUBE_PS1_LAST_TIME=$EPOCHSECONDS

  KUBE_PS1_CONTEXT="$(${KUBE_PS1_BINARY} config current-context 2>/dev/null)"
  if [[ -z "${KUBE_PS1_CONTEXT}" ]]; then
    KUBE_PS1_CONTEXT="N/A"
    KUBE_PS1_NAMESPACE="N/A"
    return
  elif [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then
    KUBE_PS1_NAMESPACE="$(${KUBE_PS1_BINARY} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
    # Set namespace to 'default' if it is not defined
    KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
Jon Mosco's avatar
Jon Mosco committed
132
133
134
  fi
}

135
136
137
138
139
140
141
142
143
144
# function to disable the prompt on the current shell
kubeon(){
  KUBE_PS1_ENABLED=true
}

# function to disable the prompt on the current shell
kubeoff(){
  KUBE_PS1_ENABLED=false
}

Jon Mosco's avatar
Jon Mosco committed
145
146
# Build our prompt
kube_ps1 () {
147
148
  local reset_color="%{$reset_color%}"
  [[ ${KUBE_PS1_ENABLED} != 'true' ]] && return
Jon Mosco's avatar
Jon Mosco committed
149
150

  KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
151
  KUBE_PS1+="${KUBE_PS1_COLOR_SYMBOL}$(_kube_ps1_symbol)"
Jon Mosco's avatar
Jon Mosco committed
152
  KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
153
  KUBE_PS1+="${KUBE_PS1_COLOR_CONTEXT}$KUBE_PS1_CONTEXT${reset_color}"
Jon Mosco's avatar
Jon Mosco committed
154
  KUBE_PS1+="$KUBE_PS1_DIVIDER"
155
  KUBE_PS1+="${KUBE_PS1_COLOR_NS}$KUBE_PS1_NAMESPACE${reset_color}"
Jon Mosco's avatar
Jon Mosco committed
156
157
158
159
  KUBE_PS1+="$KUBE_PS1_SUFFIX"

  echo "${KUBE_PS1}"
}