forked from kubermatic/fubectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfubectl.source
executable file
·305 lines (273 loc) · 10.9 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
#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
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"
_isClusterSpaceObject() {
# caller is responsible for assuring non-empty "$1"
obj="$1"
kubectl api-resources --namespaced=false \
| awk '(apiidx){print substr($0, 0, apiidx),substr($0, kindidx) } (!apiidx){ apiidx=index($0, " APIGROUP");kindidx=index($0, " KIND")}' \
| grep -iq "\<${obj}\>"
}
# [k] like g for git but 233% as effective!
alias k="kubectl"
# [kw] kw get po,svc,md
alias kw="watch kubectl get"
# [ka] get all pods in namespace
alias ka="kubectl get pods"
# [kall] get all pods in cluster
alias kall="kubectl get pods --all-namespaces"
# [kwa] watch all pods in the current namespace
alias kwa="watch kubectl get pods"
# [kwall] watch all pods in cluster
alias kwall="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/' & kubectl proxy"
# [kwatch] watch resource
kwatch() {
local kind="$1"
[ -z "$kind" ] && printf "kwatch: missing argument.\nUsage: kwatch RESOURCE\n" && return 255
if _isClusterSpaceObject "$kind" ; then
kubectl get "${kind}" | _inline_fzf | awk '{print $1}' | xargs watch kubectl get "${kind}"
else
kubectl get "${kind}" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs watch kubectl get "${kind}" -n
fi
}
# [kcmd] create a pod and execute a cmd/image (default bash/ubuntu) usage: "kcmd cmd" or "kcmd cmd image"
kcmd() {
local cmd="$1"
local image="ubuntu"
if [ -n "$2" ]; then
image="$2"
fi
local ns="$(kubectl get ns | _inline_fzf | awk '{print $1}')"
if [ -n "$cmd" ]; then
kubectl run shell-$RANDOM --namespace $ns --rm -i --tty --image ${image} -- /bin/sh -c "${cmd}"
else
kubectl run shell-$RANDOM --namespace $ns --rm -i --tty --image ${image} -- bash
fi
}
# [kube_ctx_name] get the current context
kube_ctx_name() {
kubectl config current-context
}
# [kube_ctx_namespace] get current namespace
kube_ctx_namespace() {
local default_ns="$(kubectl config view --minify|grep namespace: |sed 's/namespace: //g'|tr -d ' ')"
default_ns="${default_ns:-default}"
echo "$default_ns"
}
# [kget] get a resource by its YAML
kget() {
local kind="$1"
[ -z "$kind" ] && printf "kget: missing argument.\nUsage: kget RESOURCE\n" && return 255
if _isClusterSpaceObject "$kind" ; then
kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs kubectl get -o yaml "$kind"
else
kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs kubectl get -o yaml "$kind" -n
fi
}
# [ked] edit a resource by its YAML
ked() {
local kind="$1"
if [ -z "$kind" ]; then
echo "ked requires resource-type (pod,deployment,...) as argument."
return 255
fi
local edit_args
if _isClusterSpaceObject $kind ; then
edit_args=( $(kubectl get "$kind" | _inline_fzf | awk '{print $1}') )
else
edit_args=( $(kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print "-n", $1, $2}') )
fi
kubectl edit "$kind" ${edit_args[*]}
}
# [kdes] describe resource
kdes() {
local kind="$1"
[ -z "$kind" ] && printf "kdes: missing argument.\nUsage: kdes RESOURCE\n" && return 255
if _isClusterSpaceObject "$kind" ; then
kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs kubectl describe "$kind"
else
kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs kubectl describe "$kind" -n
fi
}
# [kdel] delete resource
kdel() {
local kind="$1"
[ -z "$kind" ] && printf "kdel: missing argument.\nUsage: kdel RESOURCE\n" && return 255
if _isClusterSpaceObject "$kind" ; then
kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs -p kubectl delete "$kind"
else
kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs -p kubectl delete "$kind" -n
fi
}
# [klog] fetch log from container
_klog_usage() {
cat <<'EOF'
Usage: klog [LINECOUNT] [options]
First argument is interpreted as LINECOUNT if it matches integer syntax.
Additional `options` are passed on (see `kubectl logs --help` for details).
EOF
}
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=$(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 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)
kubectl logs -n ${arg_pair} -c "${container_choosen}" --tail="${line_count}" "$@"
}
# [kex] execute command in container
kex() {
[ -z "$1" ] && printf "kex: missing argument(s).\nUsage: kex COMMAND [arguments]\n" && return 255
local arg_pair=$(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 kubectl get po -o=jsonpath='{.spec.containers[*].name}' -n)
local container_choosen=$(echo "$containers_out" | tr ' ' "\n" | _inline_fzf_nh)
kubectl exec -it -n ${arg_pair} -c "${container_choosen}" -- "$@"
}
# [kfor] port-forward a container port to your local machine
kfor() {
local port="$1"
[ -z "$port" ] && printf "kfor: missing argument.\nUsage: kfor PORT_TO_FORWARD\n" && return 255
local arg_pair="$(kubectl get po --all-namespaces | _inline_fzf | awk '{print $1, $2}')"
[ -z "$arg_pair" ] && printf "kfor: no pods found. no forwarding.\n" && return
kubectl port-forward -n $arg_pair "$port"
}
# [kforsvc] port-forward a service port to your local machine
kforsvc() {
local port="$1"
[ -z "$port" ] && printf "kforsvc: missing argument.\nUsage: kforsvc PORT_TO_FORWARD\n" && return 255
local arg_pair="$(kubectl get svc --all-namespaces | _inline_fzf | awk '{print $1, "svc/"$2}')"
[ -z "$arg_pair" ] && printf "kforsvc: no services found. no forwarding.\n" && return
kubectl port-forward -n $arg_pair "$port"
}
# [ksearch] search for string in resources
ksearch() {
local search_query="$1"
[ -z "$search_query" ] && printf "ksearch: missing argument.\nUsage: ksearch SEARCH_QUERY\n" && return 255
for ns in $(kubectl get --export -o=json ns | jq -r '.items[] | .metadata.name'); do
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 "kubectl get -n " prefix " " $0}'
done
}
# [kcl] context list
alias kcl='kubectl config get-contexts'
# [kcs] context set
kcs() {
local context="$(kubectl config get-contexts | _inline_fzf | cut -b4- | awk '{print $1}')"
kubectl config set current-context "${context}"
}
# [kcns] context set default namespace
kcns() {
local ns="$1"
if [ -z "$ns" ]; then
ns="$(kubectl get ns | _inline_fzf | awk '{print $1}')"
fi
[ -z "$ns" ] && printf "kcns: no namespace selected/found.\nUsage: kcns [NAMESPACE]\n" && return
kubectl config set-context "$(kubectl config current-context)" --namespace="${ns}"
}
# [kwns] watch pods in a namespace
kwns() {
local ns=$(kubectl get ns | _inline_fzf | awk '{print $1}')
[ -z "$ns" ] && printf "kcns: no namespace selected/found.\nUsage: kwns\n" && return
watch kubectl get pod -n "$ns"
}
# [ktree] prints a tree of k8s objects (kubectl tree plugin needs to be installed)
ktree() {
local kind="$1"
if [ -z "$kind" ]; then
local kind="$(kubectl api-resources -o name | _inline_fzf | awk '{print $1}')"
fi
if _isClusterSpaceObject "$kind" ; then
kubectl get "$kind" | _inline_fzf | awk '{print $1}' | xargs kubectl tree "$kind"
else
kubectl get "$kind" --all-namespaces | _inline_fzf | awk '{print $1, $2}' | xargs kubectl tree "$kind" -n
fi
}
# [konsole] create root shell on a node
konsole() {
local node_hostname="$(kubectl get node --label-columns=kubernetes.io/hostname | _inline_fzf | awk '{print $6}')"
local ns="$(kubectl get ns | _inline_fzf | awk '{print $1}')"
local name=shell-$RANDOM
local overrides='
{
"spec": {
"hostPID": true,
"hostNetwork": true,
"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'"
}
}
}
'
kubectl run $name --namespace $ns --rm -it --image alpine --overrides="${overrides}"
}
# [ksec] decode a value from a secret
ksec () {
ns=$(kubectl get ns | _inline_fzf | awk '{print $1}')
local secret=$(kubectl get secret -n "$ns" | _inline_fzf | awk '{print $1}')
local key=$(kubectl get secret -n "$ns" "$secret" -o json | jq -r '.data | to_entries[] | "\(.key)"' | _inline_fzf_nh)
kubectl view-secret -n "$ns" "$secret" "$key"
}
# [kinstall] Install the required kubectl plugins
kinstall() {
kubectl krew install tree
kubectl krew install view-secret
}
# [kupdate] Updates kubectl plugins
kupdate() {
kubectl krew upgrade
}
# [khelp] show this help message
khelp() {
echo "Usage of fubectl"
echo
echo "Reduces repetitive interactions with 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
}