-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmux-kubecontext.tmux
executable file
·358 lines (293 loc) · 8.18 KB
/
tmux-kubecontext.tmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
init(){
tmux source-file ${SCRIPT_ROOT}/default.tmux
# Options for tmux_kubecontext state
tmux set-option -goq @tmux_kubecontext_status_updated_time 0
tmux set-option -goq @tmux_kubecontext_status_context "N/A"
tmux set-option -goq @tmux_kubecontext_status_namespace "N/A"
tmux set-option -goq @tmux_kubecontext_status_last_kubeconfig ""
tmux set-option -goq @tmux_kubecontext_status_error ""
}
set_tmux_option(){
tmux set-option -gq "$1" "$2"
}
get_tmux_option(){
tmux show-option -gqv $1
}
# getter for user options
get_kubeconfig() {
get_tmux_option "@tmux_kubecontext_kubeconfig_#{pane_id}-#{session_id}-#{window_id}"
}
get_kubectl_binary() {
get_tmux_option "@tmux_kubecontext_kubectl_binary"
}
symbol_enabled(){
local enabled=$(get_tmux_option "@tmux_kubecontext_symbol_enable")
[[ $enabled == true ]]
}
get_symbol() {
get_tmux_option "@tmux_kubecontext_symbol"
}
get_symbol_fg_color() {
get_tmux_option "@tmux_kubecontext_symbol_fg_color"
}
get_context_fg_color(){
get_tmux_option "@tmux_kubecontext_context_fg_color"
}
get_separator(){
get_tmux_option "@tmux_kubecontext_separator"
}
get_separator_fg_color(){
get_tmux_option "@tmux_kubecontext_separator_fg_color"
}
namespace_enabled(){
local enabled=$(get_tmux_option "@tmux_kubecontext_namespace_enable")
[[ $enabled == true ]]
}
get_namespace_fg_color(){
get_tmux_option "@tmux_kubecontext_namespace_fg_color"
}
error_enabled(){
local enabled=$(get_tmux_option "@tmux_kubecontext_error_enable")
[[ $enabled == true ]]
}
get_error_prefix(){
get_tmux_option "@tmux_kubecontext_error_prefix"
}
get_error_fg_color(){
get_tmux_option "@tmux_kubecontext_error_fg_color"
}
lock_enabled(){
local enabled=$(get_tmux_option "@tmux_kubecontext_lock_enable")
[[ $enabled == true ]]
}
# getter and setter for status options
set_status_context(){
set_tmux_option "@tmux_kubecontext_status_context" "$1"
}
get_status_context(){
get_tmux_option "@tmux_kubecontext_status_context"
}
set_status_namespace(){
set_tmux_option "@tmux_kubecontext_status_namespace" "$1"
}
get_status_namespace(){
get_tmux_option "@tmux_kubecontext_status_namespace"
}
set_status_updated_time(){
set_tmux_option @tmux_kubecontext_status_updated_time "${1}"
}
get_status_updated_time(){
get_tmux_option "@tmux_kubecontext_status_updated_time"
}
reset_status_updated_time(){
set_status_updated_time 0
}
update_status_updated_time_to_now(){
local updated_time=$(date +%s)
set_status_updated_time "${updated_time}"
}
set_status_last_kubeconfig(){
set_tmux_option "@tmux_kubecontext_status_last_kubeconfig" "$1"
}
get_status_last_kubeconfig(){
get_tmux_option "@tmux_kubecontext_status_last_kubeconfig"
}
get_status_error(){
get_tmux_option "@tmux_kubecontext_status_error"
}
set_status_error(){
set_tmux_option "@tmux_kubecontext_status_error" "$1"
}
clear_status_error(){
set_tmux_option "@tmux_kubecontext_status_error" ""
}
reset_context(){
set_status_context "N/A"
set_status_namespace "N/A"
}
try_to_lock(){
local lockfile
[[ -z $TMPDIR ]] && TMPDIR=${TMPDIR:-/tmp}
lockfile=${TMPDIR}/tmux_kubeconfig.lock
if ln -s /dev/null ${lockfile} >/dev/null 2>&1; then
trap "rm ${lockfile}" EXIT
return 0
fi
return 1
}
file_order_changed(){
local files1=${1//:/ } files2=${2//:/ }
for ((i=0; i<${#files1[@]}; i++)); do
[[ ${files1[${i}]} != ${files2[${i}]} ]] && return 0
done
return 1
}
should_update_context(){
local stat_cmd
case $OSTYPE in
linux*)
stat_cmd='stat -c %Y'
;;
darwin*)
stat_cmd='/usr/bin/stat -f %m'
;;
*)
set_status_error "unsupported platform"
# Update 'updated_time' to now not to run any more.
update_status_updated_time_to_now
return 1
;;
esac
# Check updated kubeconfig files
local f files t=0 exitcode=0 found=0 updated=0
local kubeconfig=$(get_kubeconfig)
local last_kubeconfig=$(get_status_last_kubeconfig)
local last_updated_time=$(get_status_updated_time)
# When KUBECONFIG order is changed, reset status updated time.
# When order changed, it has possibility that reffered contexts are changed.
if file_order_changed "${kubeconfig}" "${last_kubeconfig}"; then
reset_status_updated_time
fi
set_status_last_kubeconfig "${kubeconfig}"
# Check updated kubeconfig exists
files=(${kubeconfig//:/ })
## If not set KUBECONFIG, check ${HOME}/.kube/config.
[[ ${#files[@]} == 0 ]] && files=(${files[@]} "${HOME}/.kube/config")
for f in "${files[@]}"; do
if [[ ! -e "${f}" ]]; then
set_status_error "\"${f}\" was not found"
reset_context
return 1
fi
t=$(${stat_cmd} ${f} 2>/dev/null)
exitcode=$?
if [[ $exitcode != 0 ]]; then
set_status_error "stat command was failed"
return 1
fi
if [[ "${t}" -gt "${last_updated_time}" ]]; then
updated=1
fi
found=1
done
## Reset @tmux_kubecontext_status_updated_time to update context immediately,
## when kubeconfig files isn't found.
if [[ $found == 0 ]]; then
set_status_error "kubeconfig file was not found"
reset_context
reset_status_updated_time
return 1
fi
## If all kubeconfig files didn't be updated, should not update context.
if [[ $updated == 0 ]]; then
return 1
fi
return 0
}
update_context(){
# Support KUBECONFIG env
export KUBECONFIG=$(get_kubeconfig)
# Confirm kubectl binary is executable
local kubectl_bin exitcode=0
kubectl_bin=$(get_kubectl_binary)
if [[ ! -x "$(command -v ${kubectl_bin})" ]]; then
set_status_error "executable kubectl command was not found"
return
fi
# Update context
local context
context="$(${kubectl_bin} config current-context 2>/dev/null)"
exitcode=$?
if [[ -z "${context}" ]] || [[ $exitcode != 0 ]]; then
set_status_error "failed to get context with kubectl"
return
fi
set_status_context $context
# Update namespace
local namespace
if namespace_enabled; then
namespace="$(${kubectl_bin} config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)"
exitcode=$?
if [[ $exitcode != 0 ]]; then
set_status_error "failed to get namespace with kubectl"
return
fi
namespace="${namespace:-default}"
set_status_namespace $namespace
fi
# If updated context correctly, update updated_time and clear error.
update_status_updated_time_to_now
clear_status_error
}
try_to_update_context() {
should_update_context && update_context
if [[ -n $(get_status_error) ]]; then
reset_status_updated_time
fi
}
main() {
init
if lock_enabled; then
if try_to_lock; then
try_to_update_context
else
set_status_error "locked file exists!"
fi
else
try_to_update_context
fi
local result
local context=$(get_status_context)
local namespace=$(get_status_namespace)
local symbol=$(get_symbol)
local separator=$(get_separator)
local error=$(get_status_error)
local error_prefix=$(get_error_prefix)
local context_fg_color=$(get_context_fg_color)
local namespace_fg_color=$(get_namespace_fg_color)
local symbol_fg_color=$(get_symbol_fg_color)
local separator_fg_color=$(get_separator_fg_color)
local error_fg_color=$(get_error_fg_color)
# Symbol
if symbol_enabled; then
if [[ -n ${symbol_fg_color} ]]; then
result+="#[fg=${symbol_fg_color}]${symbol}#[default]"
else
result+="${symbol}"
fi
result+=" "
fi
# Context
if [[ -n ${context_fg_color} ]]; then
result+="#[fg=${context_fg_color}]${context}#[default]"
else
result+="${context}"
fi
# Separator
if [[ -n ${separator_fg_color} ]]; then
result+="#[fg=${separator_fg_color}]${separator}#[default]"
else
result+="${separator}"
fi
# Namespace
if namespace_enabled && [[ -n ${namespace} ]]; then
if [[ -n ${namespace_fg_color} ]]; then
result+="#[fg=${namespace_fg_color}]${namespace}#[default]"
else
result+="${namespace}"
fi
fi
# Error
if error_enabled && [[ -n ${error} ]]; then
result+=" "
if [[ -n ${error_fg_color} ]]; then
result+="#[fg=${error_fg_color}]${error_prefix}${error}#[default]"
else
result+="${error_prefix}${error}"
fi
fi
echo -n "${result}"
}
main