forked from kubermatic/fubectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfubectl.source
executable file
·417 lines (369 loc) · 16.6 KB
/
fubectl.source
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#fix ZSH - perform field splitting - http://zsh.sourceforge.net/Doc/Release/Options.html
if [ -n "$ZSH_VERSION" ]; then
setopt SH_WORD_SPLIT
fi
# helper functions
if [ -z "$FUBECTL_MSYS_HACK" ]; then
alias _kctl_tty="sudo kubectl"
alias _inline_fzf="fzf --multi --ansi -i -1 --height=50% --reverse -0 --header-lines=1 --inline-info --border"
alias _inline_fzf_nh="fzf --multi --ansi -i -1 --height=50% --reverse -0 --inline-info --border"
else # hack for Msys2 shell, where fzf doesn't support mintty
alias _kctl_tty="winpty kubectl"
alias _inline_fzf="fzf --multi --ansi -i -1 --reverse -0 --header-lines=1 --inline-info"
alias _inline_fzf_nh="fzf --multi --ansi -i -1 --reverse -0 --inline-info"
fi
function _isClusterSpaceObject() {
# caller is responsible for assuring non-empty "$1"
obj="$1"
sudo kubectl api-resources --namespaced=false \
| awk '(apiidx){print substr($0, 0, apiidx),substr($0, kindidx) } (!apiidx){ apiidx=index($0, " APIVERSION");kindidx=index($0, " KIND")}' \
| grep -iq "\<${obj}\>"
}
# [k] like g for git but 233% as effective!
alias k="sudo kubectl"
# [kw] watch resources of any KIND in current namespace, usage: kw KIND[,KIND2,...]
alias kw="sudo watch kubectl get"
# [ka] get all pods from current namespace
alias ka="sudo kubectl get pods"
# [kall] get all pods from cluster
alias kall="sudo kubectl get pods --all-namespaces"
# [kwa] watch all pods in current namespace
alias kwa="sudo watch kubectl get pods"
# [kwall] watch all pods in cluster
alias kwall="sudo watch kubectl get pods --all-namespaces"
# TODO use "open" instead of "xdg-open" on a mac - also redirect xdg-open std{out,err} to /dev/null
# [kp] open kubernetes dashboard with proxy
alias kp="xdg-open 'http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/' & sudo kubectl proxy"
# [kwatchn] watch a resource of KIND in current namespace, usage: kwatchn [KIND] - if KIND is empty then pod is used
function kwatchn() {
local kind="${1:-pod}"
sudo kubectl get "${kind}" | _inline_fzf | awk '{print $1}' | xargs -r sudo watch kubectl get "${kind}"
}
# [kwatch] watch a resource of KIND in cluster, usage: kwatch [KIND] - if KIND is empty then pod is used
function kwatch() {
local kind="${1:-pod}"
if _isClusterSpaceObject "$kind" ; then
sudo kubectl get "${kind}" | _inline_fzf | awk '{print $1}' | xargs -r sudo watch kubectl get "${kind}"
else
sudo kubectl get "${kind}" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -r sudo watch kubectl get "${kind}" -n
fi
}
# [kcmd] create a pod from IMAGE (ubuntu by default) and execute CMD (bash by default), usage: kcmd [CMD] [IMAGE]
function kcmd() {
local cmd="$1"
local image="${2:-ubuntu}"
local ns="$(sudo kubectl get ns | _inline_fzf | awk '{print $1}')"
if [ -n "$cmd" ]; then
sudo kubectl run shell-$RANDOM --namespace $ns --rm -i --tty --image ${image} -- /bin/sh -c "${cmd}"
else
sudo kubectl run shell-$RANDOM --namespace $ns --rm -i --tty --image ${image} -- /bin/bash
fi
}
# [kube_ctx_name] get the current context
function kube_ctx_name() {
sudo kubectl config current-context
}
# [kube_ctx_namespace] get current namespace
function kube_ctx_namespace() {
local default_ns="$(sudo kubectl config view --minify|grep namespace: |sed 's/namespace: //g'|tr -d ' ')"
default_ns="${default_ns:-default}"
echo "$default_ns"
}
# [kgetn] get resource of KIND from current namespace, usage: kgetn [KIND] - if KIND is empty then pod is used
function kgetn() {
local kind="${1:-pod}"
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl get -o yaml "$kind"
}
# [kget] get resource of KIND from cluster, usage: kget [KIND] - if KIND is empty then pod is used
function kget() {
local kind="${1:-pod}"
if _isClusterSpaceObject "$kind" ; then
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl get -o yaml "$kind"
else
sudo kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -r sudo kubectl get -o yaml "$kind" -n
fi
}
# [kexp] as former `--export` field removes unwanted metadata - usage: COMMAND | kexp
function kexp() {
if [ -t 0 ]; then
echo "kexp has no piped input!"
echo "usage: COMMAND | kexp"
else
# remove not neat fields
sudo kubectl neat
fi
}
# [kget-exp] get a resource by its YAML as former `--export` flag
function kget-exp() {
kget "$@" | kexp
}
# [kedn] edit resource of KIND from current namespace, usage: kedn [KIND] - if KIND is empty then pod is used
function kedn() {
local kind="${1:-pod}"
sudo kubectl edit "${kind}" "$(sudo kubectl get "${kind}" | _inline_fzf | awk '{print $1}')"
}
# [ked] edit resource of KIND from cluster, usage: ked [KIND] - if KIND is empty then pod is used
function ked() {
local kind="${1:-pod}"
local edit_args
if _isClusterSpaceObject $kind ; then
edit_args=( $(sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}') )
else
edit_args=( $(sudo kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print "-n", $1, $2}') )
fi
sudo kubectl edit "$kind" ${edit_args[*]}
}
# [kdesn] describe resource of KIND in current namespace, usage: kdesn [KIND] - if KIND is empty then pod is used
function kdesn() {
local kind="${1:-pod}"
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl describe "$kind"
}
# [kdes] describe resource of KIND in cluster, usage: kdes [KIND] - if KIND is empty then pod is used
function kdes() {
local kind="${1:-pod}"
if _isClusterSpaceObject "$kind" ; then
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl describe "$kind"
else
sudo kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -r sudo kubectl describe "$kind" -n
fi
}
# [kdeln] delete resource of KIND in current namespace, usage: kdeln [KIND] - if KIND is empty then pod is used
function kdeln() {
local kind="${1:-pod}"
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r -p sudo kubectl delete "$kind"
}
# [kdel] delete resource of KIND in cluster, usage: kdel [KIND] - if KIND is empty then pod is used
function kdel() {
local kind="${1:-pod}"
if _isClusterSpaceObject "$kind" ; then
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r -p sudo kubectl delete "$kind"
else
sudo kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -r -p sudo kubectl delete "$kind" -n
fi
}
function _klog_usage() {
cat <<'EOF'
Usage: klog[n] [LINECOUNT] [options]
First argument is interpreted as LINECOUNT if it matches integer syntax.
Additional `options` are passed on (see `sudo kubectl logs --help` for details).
EOF
}
# [klogn] fetch log from container in current namespace
function klogn() {
[ "$1" = "--help" ] && _klog_usage && return
local line_count=10
if [[ $1 =~ ^[-]{0,1}[0-9]+$ ]]; then
line_count="$1"
shift
fi
local pod=$(sudo kubectl get po | _inline_fzf | awk '{print $1}')
[ -z "${pod}" ] && printf "klogn: no pods found. no logs can be shown.\n" && return
local containers_out=$(sudo kubectl get po "${pod}" -o=jsonpath='{.spec.containers[*].name} {.spec.initContainers[*].name}' | sed 's/ $//')
local container_choosen=$(echo "$containers_out" | tr ' ' "\n" | _inline_fzf_nh)
_kctl_tty logs "${pod}" -c "${container_choosen}" --tail="${line_count}" "$@"
}
# [klog] fetch log from container in the cluster
function klog() {
[ "$1" = "--help" ] && _klog_usage && return
local line_count=10
if [[ $1 =~ ^[-]{0,1}[0-9]+$ ]]; then
line_count="$1"
shift
fi
local arg_pair=$(sudo kubectl get po --all-namespaces | _inline_fzf | awk '{print $1, $2}')
[ -z "$arg_pair" ] && printf "klog: no pods found. no logs can be shown.\n" && return
local containers_out=$(echo "$arg_pair" | xargs sudo kubectl get po -o=jsonpath='{.spec.containers[*].name} {.spec.initContainers[*].name}' -n | sed 's/ $//')
local container_choosen=$(echo "$containers_out" | tr ' ' "\n" | _inline_fzf_nh)
_kctl_tty logs -n ${arg_pair} -c "${container_choosen}" --tail="${line_count}" "$@"
}
# [kkonfig] select a file in current directory and set it as $KUBECONFIG
function kkonfig() {
local kubeconfig
kubeconfig=$(_inline_fzf_nh) || return
export KUBECONFIG=$PWD/$kubeconfig
exec $SHELL
}
# [kexn] execute command in container from current namespace, usage: kexn CMD [ARGS]
function kexn() {
[ -z "$1" ] && printf "kexn: missing argument(s).\nUsage: kexn CMD [ARGS]\n" && return 255
local arg_pair=$(sudo kubectl get po | _inline_fzf | awk '{print $1}')
[ -z "$arg_pair" ] && printf "kexn: no pods found. no execution.\n" && return
local containers_out=$(echo "$arg_pair" | xargs sudo kubectl get po -o=jsonpath='{.spec.containers[*].name}' -n)
local container_choosen=$(echo "$containers_out" | tr ' ' "\n" | _inline_fzf_nh)
_kctl_tty exec -it ${arg_pair} -c "${container_choosen}" -- "$@"
}
# [kex] execute command in container from cluster, usage: kex CMD [ARGS]
function kex() {
[ -z "$1" ] && printf "kex: missing argument(s).\nUsage: kex CMD [ARGS]\n" && return 255
local arg_pair=$(sudo kubectl get po --all-namespaces | _inline_fzf | awk '{print $1, $2}')
[ -z "$arg_pair" ] && printf "kex: no pods found. no execution.\n" && return
local containers_out=$(echo "$arg_pair" | xargs sudo kubectl get po -o=jsonpath='{.spec.containers[*].name}' -n)
local container_choosen=$(echo "$containers_out" | tr ' ' "\n" | _inline_fzf_nh)
_kctl_tty exec -it -n ${arg_pair} -c "${container_choosen}" -- "$@"
}
# [kforn] port-forward a container port from current namesapce, usage: kforn LOCAL_PORT:CONTAINER_PORT
function kforn() {
local port="$1"
[ -z "$port" ] && printf "kforn: missing argument.\nUsage: kforn LOCAL_PORT:CONTAINER_PORT\n" && return 255
local arg_pair="$(sudo kubectl get po | _inline_fzf | awk '{print $1}')"
[ -z "$arg_pair" ] && printf "kforn: no pods found. no forwarding.\n" && return
_kctl_tty port-forward ${arg_pair} ${port}
}
# [kfor] port-forward a container port from cluster, usage: kfor LOCAL_PORT:CONTAINER_PORT
function kfor() {
local port="$1"
[ -z "$port" ] && printf "kfor: missing argument.\nUsage: kfor LOCAL_PORT:CONTAINER_PORT\n" && return 255
local arg_pair="$(sudo kubectl get po --all-namespaces | _inline_fzf | awk '{print $1, $2}')"
[ -z "$arg_pair" ] && printf "kfor: no pods found. no forwarding.\n" && return
_kctl_tty port-forward -n ${arg_pair} ${port}
}
# [ksearch] search for string in resources
function ksearch() {
local search_query="$1"
[ -z "$search_query" ] && printf "ksearch: missing argument.\nUsage: ksearch SEARCH_QUERY\n" && return 255
for ns in $(sudo kubectl get --export -o=json ns | jq -r '.items[] | .metadata.name'); do
sudo kubectl --namespace="${ns}" get --export -o=json \
deployment,ingress,daemonset,secrets,configmap,service,serviceaccount,statefulsets,pod,endpoints,customresourcedefinition,events,networkpolicies,persistentvolumeclaims,persistentvolumes,replicasets,replicationcontrollers,statefulsets,storageclasses | \
jq '.items[]' -c | \
grep "$search_query" | \
jq -r '. | [.kind, .metadata.name] | @tsv' | \
awk -v prefix="$ns" '{print "sudo kubectl get -n " prefix " " $0}'
done
}
# [kcl] context list
alias kcl='sudo kubectl config get-contexts'
# [kcs] context set
function kcs() {
local context="$(sudo kubectl config get-contexts | _inline_fzf | cut -b4- | awk '{print $1}')"
sudo kubectl config set current-context "${context}"
}
# [kcns] context set default namespace
function kcns() {
local ns="$1"
if [ -z "$ns" ]; then
ns="$(sudo kubectl get ns | _inline_fzf | awk '{print $1}')"
fi
[ -z "$ns" ] && printf "kcns: no namespace selected/found.\nUsage: kcns [NAMESPACE]\n" && return
sudo kubectl config set-context "$(sudo kubectl config current-context)" --namespace="${ns}"
}
# [kwns] watch pods in a namespace
function kwns() {
local ns=$(sudo kubectl get ns | _inline_fzf | awk '{print $1}')
[ -z "$ns" ] && printf "kcns: no namespace selected/found.\nUsage: kwns\n" && return
sudo watch kubectl get pod -n "$ns"
}
# [ktreen] prints a tree of k8s objects from current namespace, usage: ktreen [KIND]
function ktreen() {
local kind="$1"
if [ -z "$kind" ]; then
local kind="$(sudo kubectl api-resources -o name | _inline_fzf | awk '{print $1}')"
fi
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl tree "$kind"
}
# [ktree] prints a tree of k8s objects from cluster, usage: ktree [KIND]
function ktree() {
local kind="$1"
if [ -z "$kind" ]; then
local kind="$(sudo kubectl api-resources -o name | _inline_fzf | awk '{print $1}')"
fi
if _isClusterSpaceObject "$kind" ; then
sudo kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -r sudo kubectl tree "$kind"
else
sudo kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -r sudo kubectl tree "$kind" -n
fi
}
# [konsole] create root shell on a node
function konsole() {
local node_hostname="$(sudo kubectl get node --label-columns=kubernetes.io/hostname | _inline_fzf | awk '{print $6}')"
local ns="$(sudo kubectl get ns | _inline_fzf | awk '{print $1}')"
local name=shell-$RANDOM
local overrides='
{
"spec": {
"hostPID": true,
"hostNetwork": true,
"tolerations": [
{
"operator": "Exists",
"effect": "NoSchedule"
},
{
"operator": "Exists",
"effect": "NoExecute"
}
],
"containers": [
{
"name": "'$name'",
"image": "alpine",
"command": [
"/bin/sh"
],
"args": [
"-c",
"nsenter -t 1 -m -u -i -n -p -- bash"
],
"resources": null,
"stdin": true,
"stdinOnce": true,
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"tty": true,
"securityContext": {
"privileged": true
}
}
],
"nodeSelector": {
"kubernetes.io/hostname": "'$node_hostname'"
}
}
}
'
sudo kubectl run $name --namespace "$ns" --rm -it --image alpine --overrides="${overrides}"
}
# [ksecn] decode a value from a secret in current namespace, usage: ksecn
function ksecn() {
local secret=$(sudo kubectl get secret | _inline_fzf | awk '{print $1}')
local key=$(sudo kubectl get secret "${secret}" -o go-template='{{- range $k,$v := .data -}}{{- printf "%s\n" $k -}}{{- end -}}' | _inline_fzf_nh)
sudo kubectl get secret "${secret}" -o go-template='{{ index .data "'$key'" | base64decode }}'
}
# [ksec] decode a value from a secret in cluster, usage: ksec
function ksec() {
local ns=$(sudo kubectl get ns | _inline_fzf | awk '{print $1}')
local secret=$(sudo kubectl get secret -n "$ns" | _inline_fzf | awk '{print $1}')
local key=$(sudo kubectl get secret -n "$ns" "$secret" -o go-template='{{- range $k,$v := .data -}}{{- printf "%s\n" $k -}}{{- end -}}' | _inline_fzf_nh)
sudo kubectl get secret -n "$ns" "${secret}" -o go-template='{{ index .data "'$key'" | base64decode }}'
}
# [kinstall] Install the required sudo kubectl plugins
function kinstall() {
sudo kubectl krew install tree
sudo kubectl krew install neat
}
# [kupdate] Updates sudo kubectl plugins
function kupdate() {
sudo kubectl krew upgrade
}
#### Kubermatic KKP specific
# [kkp-cluster] Kubermatic KKP - extracts kubeconfig of user cluster and connects it in a new bash
function kkp-cluster() {
TMP_KUBECONFIG=$(mktemp)
local cluster="$(sudo kubectl get cluster | _inline_fzf | awk '{print $1}')"
sudo kubectl get secret admin-kubeconfig -n cluster-$cluster -o go-template='{{ index .data "kubeconfig" | base64decode }}' > $TMP_KUBECONFIG
KUBECONFIG=$TMP_KUBECONFIG $SHELL
}
# [khelp] show this help message
function khelp() {
echo "Usage of fubectl"
echo
echo "Reduces repetitive interactions with sudo kubectl"
echo "Find more information at https://github.com/kubermatic/fubectl"
echo
echo "Usage:"
if [ -n "$ZSH_VERSION" ]
then
grep -E '^# \[.+\]' "${(%):-%x}"
else
grep -E '^# \[.+\]' "${BASH_SOURCE[0]}"
fi
}