-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc_kube
371 lines (331 loc) · 12.4 KB
/
.zshrc_kube
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
#### zsh config for kubectl tools
#
# INSTRUCTIONS:
# - set below env vars in .zshrc then source this file
# - set up wrapper functions in .zshrc to use these inner functions more easily
# - can set up wrapper function for each context
#
# env vars:
# KUBE_DEFAULT_CONTEXT - default kubernetes context
# kube_to_aws - map of kubernetes context to aws profile
function _kubectl_get_containers_from_user(){
namespace=$1
pod=$2
context="${3:-$KUBE_DEFAULT_CONTEXT}"
containers=($(kubectl --context=$context -n $namespace get pods -o json | jq -r '.items[] | select( .metadata.name | startswith("'$pod'")) | .spec.containers[].name' | sort | uniq))
containers_selection=$(
i=1
for c in $containers;do
echo "${(l:3:: :)i}: $c"
i=$(($i+1))
done
)
# make 3 columns if too many lines
container_count=$(printf "%s\n" "${containers[@]}" | wc -l | awk '{print $1}')
if [[ "(($container_count>8))" ]];then
pods_selection=$(echo "${containers_selection}" | pr -t -3 -w 120)
fi
# get selection from user input
read "ANSWER?${containers_selection}
which container? (number or name): "
if [[ $ANSWER == <-> ]];then
#selection is numeric
container=$containers[$ANSWER]
else
#selection is not numeric
container=$ANSWER
fi
echo $container
}
function _kubectl_stats(){
context=$1
namespace=$2
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
limits=$(kubectl --context=$context --namespace $namespace get pods -o json | \
jq '
def get_num:
if test("Gi") then rtrimstr("Gi") | tonumber*1024
elif test("Mi") then rtrimstr("Mi") | tonumber
else tonumber
end;
.items[] |
{
name: .metadata.name,
containers: [.spec.containers[] | {
name: .name,
cpu_limits: .resources.limits.cpu,
memory_limits: .resources.limits.memory,
cpu_requests: .resources.requests.cpu,
memory_requests: .resources.requests.memory
}]
}
|
{
name: .name,
cpu_limits: ([.containers[].cpu_limits | if . != null then rtrimstr("m") | tonumber else 0 end] | add | tostring + "m"),
cpu_requests: ([.containers[].cpu_requests | if . != null then rtrimstr("m") | tonumber else 0 end] | add | tostring + "m"),
memory_limits: ([.containers[].memory_limits | if . != null then get_num else 0 end] | add | "\(.)Mi"),
memory_requests: ([.containers[].memory_requests | if . != null then get_num else 0 end] | add | "\(.)Mi"),
}
')
usage=$(kubectl --context=$context --namespace $namespace top pods | \
awk '
NR>1 {
print "{\"name\":\"" $1 "\",\"cpu_usage\":\"" $2 "\",\"memory_usage\":\"" $3 "\"}"
}')
# join both json's on name
combined=$(echo "$limits $usage" | jq -s)
combined=$(echo $combined | jq 'reduce .[] as $item ({}; .[$item.name] += $item) | .[]' | jq -s)
#change to tsv format and print everything
combined=$(echo $combined | jq -r '
.[] |
[.name[0:50], .cpu_usage, .cpu_limits, .cpu_requests, .memory_usage, .memory_limits, .memory_requests]
| @tsv')
titles=$(printf "Name\tCPU Use\tCPU Lim\tCPU Req\tMem Use\tMem Lim\tMem Req")
printf "$titles\n$combined" | column -t -s $'\t' 2>/dev/null
}
function _kubectl_get_pods_from_user(){
namespace=$1
context="${2:-$KUBE_DEFAULT_CONTEXT}"
[[ -z $namespace ]] && return
pods=($(
kubectl --context=$context -n $namespace get pods -o json | jq -r '.items[].metadata.name'))
pods_selection=$(
i=1
for p in $pods;do
echo "${(l:3:: :)i}: $p"
i=$(($i+1))
done
)
# make 3 columns if too many lines
pod_count=$(printf "%s\n" "${pods[@]}" | wc -l | awk '{print $1}')
if [[ "(($pod_count>8))" ]];then
pods_selection=$(echo "${pods_selection}" | pr -t -3 -w 120)
fi
# get selection from user input
read "ANSWER?${pods_selection}
which pod? (number or start of name): "
if [[ $ANSWER == <-> ]];then
#selection is numeric
pod=$pods[$ANSWER]
else
#selection is not numeric
pod="service/${ANSWER}"
fi
echo $pod
}
function _kubectl_get_namespace_from_user(){
context="${1:-$KUBE_DEFAULT_CONTEXT}"
namespaces=($(kubectl --context=$context get namespaces -o name | sed 's/namespace\///'))
namespaces_selection=$(
i=1
for n in $namespaces;do
echo "${(l:3:: :)i}: $n"
i=$(($i+1))
done
)
# get selection from user input
namespaces_selection=$(echo "${namespaces_selection}" | pr -t -3 -w 120)
read "ANSWER?${namespaces_selection}
which namespace? (number or name): "
# vared -cp "${namespaces_selection}
# which namespace? (number or name): " ANSWER
if [[ $ANSWER == <-> ]];then
#selection is numeric
namespace=$namespaces[$ANSWER]
else
#selection is not numeric
namespace=$ANSWER
fi
echo $namespace
}
function _kubectl_ec2_info(){
context=$1
verbose=$2
namespace=$3
pod=$4
if [[ -z $namespace ]];then
echo "choose namespace for $context..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
if [[ -z $pod ]]; then
printf "%s\n" "choose pod for $context/$namespace..."
pod=$(_kubectl_get_pods_from_user $namespace $context)
[[ -z $pod ]] && echo "no pod selected. exiting." && return
printf "%s\n\n" "pod: $pod"
fi
pod=$(echo $pod | sed 's/^service\///')
node_name=$(kubectl --context $context -n $namespace get pod $pod -o json | jq -r '.spec.nodeName')
instance_type=$(aws --profile "${kube_to_aws[$context]}" ec2 describe-instances --filters "Name=tag:Name,Values=$node_name" | jq -r '.Reservations[0].Instances[0].InstanceType')
if [[ $verbose == true ]];then
aws --profile "${kube_to_aws[$context]}" ec2 describe-instance-types --instance-types $instance_type --output json \
| jq '.InstanceTypes[0]'
else
aws --profile "${kube_to_aws[$context]}" ec2 describe-instance-types --instance-types $instance_type --output json \
| jq '.InstanceTypes[0] | {InstanceType, "Memory_MiB":.MemoryInfo.SizeInMiB, "CPU_GB": .InstanceStorageInfo.TotalSizeInGB}'
fi
}
function _kubectl_env(){
context=$1
namespace=$2
pod=$3
container=$4
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
if [[ -z $pod ]]; then
printf "%s\n" "choose pod..."
pod=$(_kubectl_get_pods_from_user $namespace $context)
[[ -z $pod ]] && echo "no pod selected. exiting." && return
printf "%s\n\n" "pod: $pod"
fi
pod=$(echo $pod | sed 's/^service\///')
if [[ -z $container ]];then
echo "choose container..."
container=$(_kubectl_get_containers_from_user $namespace $pod $context)
[[ -z $container ]] && echo "no container selected. exiting." && return
printf "%s\n\n" "container: $container"
fi
printf "%s\n\n" "showing env of [${context}]/[${namespace}]/[${pod}]/[${container}]..."
kubectl --context=$context -n $namespace get pods -o json | \
jq -r '.items[] | select( .metadata.name | startswith("'$pod'")) | .spec.containers[] | select( .name == "'$container'" ) | .env'
}
function _kubectl_logs(){
context=$1
namespace=$2
pod=$3
container=$4
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
if [[ -z $pod ]]; then
printf "%s\n" "choose pod..."
pod=$(_kubectl_get_pods_from_user $namespace $context)
[[ -z $pod ]] && echo "no pod selected. exiting." && return
printf "%s\n\n" "pod: $pod"
fi
pod=$(echo $pod | sed 's/^service\///')
if [[ -z $container ]];then
echo "choose container from pod $pod..."
container=$(_kubectl_get_containers_from_user $namespace $pod $context)
[[ -z $container ]] && echo "no container selected. exiting." && return
printf "%s\n\n" "container: $container"
fi
services=($(kubectl --context=$context -n $namespace get services -o name | sed 's/^service\///'))
if (($services[(I)$pod]));then
pod="service/${pod}"
fi
printf "\n%s\n\n" "showing logs of [${context}]/[${namespace}]/[${pod}]/[${container}]..."
cmnd="kubectl --context=$context -n $namespace logs -c $container $pod -f"
printf "%s\n" "$cmnd"
kubectl --context=$context -n $namespace logs -c $container $pod -f
printf "%s\n" "$cmnd"
# read -q "ANSWER?Copy command to cipboard (y/n)? "
# [[ $ANSWER == y ]] && printf "$cmnd" | pbcopy
}
function _kubectl_console(){
context=$1
namespace=$2
pod=$3
container=$4
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
if [[ -z $pod ]]; then
printf "%s\n" "choose pod..."
pod=$(_kubectl_get_pods_from_user $namespace $context)
[[ -z $pod ]] && echo "no pod selected. exiting." && return
printf "%s\n\n" "pod: $pod"
fi
pod=$(echo $pod | sed 's/^service\///')
if [[ -z $container ]];then
echo "choose container..."
container=$(_kubectl_get_containers_from_user $namespace $pod $context)
[[ -z $container ]] && echo "no container selected. exiting." && return
printf "%s\n\n" "container: $container"
fi
services=($(kubectl --context=$context -n $namespace get services -o name | sed 's/^service\///'))
if (($services[(I)$pod]));then
pod="service/${pod}"
fi
printf "\n%s\n\n" "entering console of [${context}]/[${namespace}]/[${pod}]/[${container}]..."
kubectl --context=$context -n $namespace exec -c $container $pod -it -- sh
cmnd="kubectl --context=$context -n $namespace exec -c $container $pod -it -- sh"
printf "%s\n" "$cmnd"
# read -q "ANSWER?Copy command to cipboard (y/n)? "
# [[ $ANSWER == y ]] && printf "$cmnd" | pbcopy
}
function _kubectl_exec(){
context=$1
namespace=$2
pod=$3
container=$4
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
if [[ -z $pod ]]; then
printf "%s\n" "choose pod..."
pod=$(_kubectl_get_pods_from_user $namespace $context)
[[ -z $pod ]] && echo "no pod selected. exiting." && return
printf "%s\n\n" "pod: $pod"
fi
pod=$(echo $pod | sed 's/^service\///')
if [[ -z $container ]];then
echo "choose container..."
container=$(_kubectl_get_containers_from_user $namespace $pod $context)
[[ -z $container ]] && echo "no container selected. exiting." && return
printf "%s\n\n" "container: $container"
fi
services=($(kubectl --context=$context -n $namespace get services -o name | sed 's/^service\///'))
if (($services[(I)$pod]));then
pod="service/${pod}"
fi
cmnd="kubectl --context=$context -n $namespace exec -c $container $pod -- "
read "ANSWER?$cmnd"
ANSWER=(${(@s: :)ANSWER})
printf "\n%s\n\n" "exec of [${context}]/[${namespace}]/[${pod}]/[${container}]..."
kubectl --context=$context -n $namespace exec -c $container $pod -- ${ANSWER[@]}
}
function _kubectl_status(){
namespace=$1
context=$2
if [[ -z $namespace ]];then
echo "choose namespace..."
namespace=$(_kubectl_get_namespace_from_user $context)
[[ -z $namespace ]] && echo "no namespace selected. exiting." && return
printf "%s\n\n" "namespace: $namespace"
fi
printf "%s\n\n" "showing pods and status of ${context}/${namespace}..."
printf "%s\n" "kubectl --context=$context -n $namespace get pods -w"
kubectl --context=$context -n $namespace get pods -w
}
function _kubectl_endpoints(){
context=$1
# this will list out all endpoints that can be accessed from any pod
kubectl --context=$context get services -A -o json | \
jq -r '.items[] | "\(.metadata.namespace).\(.metadata.name).svc.cluster.local:\(.spec.ports[0].port)"'
}
function _kubectl_namespaces(){
context=$1
kubectl --context=$context get namespaces -o name \
| sed 's/namespace\///' | pr -t -4 -w 120
}