forked from thecasualcoder/kube-fzf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kube-fzf.sh
executable file
·315 lines (287 loc) · 10.3 KB
/
kube-fzf.sh
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
#!/usr/bin/env bash
_kube_fzf_usage() {
local func=$1
echo -e "\nUSAGE:\n"
case $func in
kdelete)
cat << EOF
kdelete [resource name] [-a | -n <namespace-query>] [pod-query]
ex) kdelete pod
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
kstern)
cat << EOF
kstern [resource name] [-a | -n <namespace-query>] [pod-query]
ex) kstern pod
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
kget)
cat << EOF
kget [resource name] [-a | -n <namespace-query>] [pod-query]
ex) kget pod
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
tailpod)
cat << EOF
tailpod [-a | -n <namespace-query>] [pod-query]
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
execpod)
cat << EOF
execpod [-a | -n <namespace-query>] [pod-query] <command>
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
pfpod)
cat << EOF
pfpod [ -c | -o | -a | -n <namespace-query>] [pod-query] <source-port:destination-port | port>
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
-o - Open in Browser after port-forwarding
EOF
;;
kdesc)
cat << EOF
kdesc [resource name] [-a | -n <namespace-query> | -c <kubectl context>] [pod-query]
ex) kdesc service
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
krestart)
cat << EOF
krestart [resource name] [-a | -n <namespace-query> | -c <kubectl context>] [pod-query]
ex) krestart deployment
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
kedit)
cat << EOF
kedit [resource name] [-a | -n <namespace-query> | -c <kubectl context>] [pod-query]
ex) kedit deployment
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
kout)
cat << EOF
kout [-a | -n <namespace-query> | -c <kubectl context>] [pod-query]
ex) kout pod1
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
ktdump)
cat << EOF
ktdump [-a | -n <namespace-query> | -c <kubectl context> | -d <output path> | -p <pid>] [pod-query]
ex) ktdump pod1
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-d <path> - Default: /dumps
Output path of thread dump files
it will save to $<path>/$now_$podname.tdump
-p <pid> - Default: 1
Target Process id for thread dump.
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
khdump)
cat << EOF
khdump [-a | -n <namespace-query> | -c <kubectl context> | -d <output path> | -p <pid>] [pod-query]
ex) khdump pod1
-a - Search in all namespaces
-h - Show help
-c - Find kubectl context and do fzf
-d <path> - Default: /dumps
Output path of heap dump files
it will save to $<path>/$now_$podname.hdump
-p <pid> - Default: 1
Target Process id for heap dump.
-n <namespace-query> - Find namespaces matching <namespace-query> and do fzf.
If there is only one match then it is selected automatically.
EOF
;;
esac
}
kube_fzf_api_resources() {
read resource <<< $(kubectl api-resources --no-headers | awk '{print $1}' | sort -u \
| fzf $(echo $pod_fzf_args) --header "choose api-resource " --no-info \
| awk '{ print $1 }')
if [ -z "$resource" ]; then
exit
fi
echo "$resource"
}
_kube_fzf_handler() {
local opt namespace_query pod_query cmd context path pid
local open=false
local context=false
local OPTIND=1
local func=$1
shift $((OPTIND))
while getopts "n:p:d:caoh" opt; do
case $opt in
h)
_kube_fzf_usage "$func"
return 1
;;
n)
namespace_query="$OPTARG"
;;
p)
pid="$OPTARG"
;;
d)
path="$OPTARG"
;;
a)
namespace_query="--all-namespaces"
;;
o)
open=true
;;
c)
context=true
;;
\?)
echo "Invalid Option: -$OPTARG."
_kube_fzf_usage "$func"
return 1
;;
:)
echo "Option -$OPTARG requires an argument."
_kube_fzf_usage "$func"
return 1
;;
esac
done
if [ "$context" = true ] ; then
read ctx <<< $(kubectl config get-contexts -o=name --no-headers | awk '{print $1}' \
| fzf $(echo $pod_fzf_args) --header "choose kubectl context "\
| awk '{ print $1 }')
context=$(echo "$ctx")
if [ -z "$context" ]; then
exit
fi
else
context=$(kubectl config current-context)
fi
shift $((OPTIND - 1))
if [ "$func" = "execpod" ] || [ "$func" = "pfpod" ]; then
if [ $# -eq 1 ]; then
cmd=$1
[ -z "$cmd" ] && cmd="bash"
elif [ $# -eq 2 ]; then
pod_query=$1
cmd=$2
if [ -z "$cmd" ]; then
if [ "$func" = "execpod" ]; then
echo "Command required." && _kube_fzf_usage "$func" && return 1
elif [ "$func" = "pfpod" ]; then
echo "Port required." && _kube_fzf_usage "$func" && return 1
fi
fi
else
if [ -z "$cmd" ]; then
if [ "$func" = "execpod" ]; then
cmd="sh"
elif [ "$func" = "pfpod" ]; then
echo "Port required." && _kube_fzf_usage "$func" && return 1
fi
fi
fi
else
pod_query=$1
fi
args="$namespace_query|$pod_query|$cmd|$open|$context|$pid|$path"
}
_kube_fzf_fzf_args() {
local search_query=$1
local extra_args=$2
local fzf_args="--height=100 --ansi --reverse $extra_args"
[ -n "$search_query" ] && fzf_args="$fzf_args --query=$search_query"
echo "$fzf_args"
}
_kube_fzf_search() {
local namespace rs_name
local namespace_query=$1
local pod_query=$2
local context_selector=$3
local resource=$4
local pod_fzf_args=$(_kube_fzf_fzf_args "$pod_query")
local bat_command=""
if command -v bat > /dev/null; then
bat_command="| bat -l yaml --color 'always' --style 'numbers'"
fi
if [ -z "$namespace_query" ]; then
namespace=$(kubectl config get-context --no-headers $context_selector \
| awk '{ print $5 }')
namespace=${namespace:=default}
rs_name=$(kubectl get $resource --namespace=$namespace --context $context_selector --no-headers -o wide \
| fzf $(echo $pod_fzf_args) --preview "kubectl get $resource {1} -o yaml -n $namespace --context $context_selector $bat_command" \
| awk '{ print $1 }')
elif [ "$namespace_query" = "--all-namespaces" ]; then
read namespace rs_name <<< $(kubectl get $resource --all-namespaces --context $context_selector --no-headers -o wide \
| fzf $(echo $pod_fzf_args) --preview "kubectl get $resource {2} -o yaml -n {1} --context $context_selector $bat_command" \
| awk '{ print $1, $2 }')
else
local namespace_fzf_args=$(_kube_fzf_fzf_args "'$namespace_query" "--select-1")
namespace=$(kubectl get namespaces --context $context_selector --no-headers \
| fzf $(echo $namespace_fzf_args) --prompt "Find namespace > " \
| awk '{ print $1 }')
namespace=${namespace:=default}
rs_name=$(kubectl get $resource --namespace=$namespace --context $context_selector --no-headers -o wide \
| fzf $(echo $pod_fzf_args) --preview "kubectl get $resource {1} -o yaml -n $namespace --context $context_selector $bat_command" \
| awk '{ print $1 }')
fi
[ -z "$rs_name" ] && return 1
echo "$namespace|$rs_name"
}
_kube_fzf_echo() {
local reset_color="\033[0m"
local bold_green="\033[1;32m"
local message=$1
echo -e "\n$bold_green $message $reset_color\n"
}