-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl-sample
executable file
·74 lines (60 loc) · 2.64 KB
/
kubectl-sample
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
#!/bin/bash
PARAMS=( "$@" )
PARAMS_NO=$#
function run_plugin() {
local content=""
local exit_code=0
content=$(kubectl get Foo --output=jsonpath='{range .items[*]}{.metadata.namespace}{"\n"}{.metadata.name}{"\n"}{.spec.deploymentName}{"\n"}{.spec.replicas}{"\n"}{.status.availableReplicas}{"\n"}{end}' --all-namespaces 2>&1)
exit_code=$?
if [ $exit_code -ne 0 ]; then
printf '%s\n' "${content}" 1>&2
exit $exit_code
fi
local header_namespace='NAMESPACE'
local header_resource='RESOURCE'
local header_deployment_name='DEPLOYMENT_NAME'
local header_replicas='REPLICAS'
local header_available_replicas='AVAILABLE_REPLICAS'
local resources=""
local nlen=${#header_namespace} # minimum column width for namespace
local rlen=${#header_resource} # minimum column width for Foo resource name
local dlen=${#header_deployment_name} # minimum column width for deployment name
local namespace=""
local resource_name=""
local deployment_name=""
local replicas=0
local available_replicas=0
while read namespace; read resource_name; read deployment_name; read replicas; read available_replicas; do
# package what we need for later use
resources=$(printf "${resources}\n%s:%s:%s:%d:%d" "${namespace}" "${resource_name}" "${deployment_name}" $replicas $available_replicas)
# update column widths, if needed
ns_len=$(printf '%s' "${namespace}" | wc -c)
if [ $ns_len -gt $nlen ]; then
nlen=$ns_len
fi
res_len=$(printf '%s' "${resource_name}" | wc -c)
if [ $res_len -gt $rlen ]; then
rlen=$res_len
fi
dep_len=$(printf '%s' "${deployment_name}" | wc -c)
if [ $dep_len -gt $dlen ]; then
dlen=$dep_len
fi
done < <(printf '%s\n' "${content}"; /bin/true)
# width for columns with numbers:
# we presume we won't have more than 18 digit number of replicas, 18 is the length of the available replicas header
local p_format="%-${nlen}s %-${rlen}s %-${dlen}s %-18s %-18s\n"
printf "${p_format}" "${header_namespace}" "${header_resource}" "${header_deployment_name}" "${header_replicas}" "${header_available_replicas}"
while read res; do
if [ "${res}" != '' ]; then
local namespace=$(printf '%s' "${res}" | cut -d: -f1)
local resource_name=$(printf '%s' "${res}" | cut -d: -f2)
local deployment_name=$(printf '%s' "${res}" | cut -d: -f3)
local replicas=$(printf '%s' "${res}" | cut -d: -f4)
local available_replicas=$(printf '%s' "${res}" | cut -d: -f5)
printf "${p_format}" "${namespace}" "${resource_name}" "${deployment_name}" $replicas $available_replicas
fi
done < <(printf '%s\n' "${resources}"; /bin/true)
}
# Main
run_plugin