-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl-wider
executable file
·100 lines (79 loc) · 2.12 KB
/
kubectl-wider
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
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
SELF_CMD="$0"
OUT_FORMAT='["POD","NODE","PROVIDER ID"], (.[] | [.metadata.name, .node.metadata.name, .node.spec.providerID])'
check_command() {
if ! command -v $1 2>&1 >/dev/null
then
exit_err "error this plugin requires $1. Please check that your PATH contains $1"
fi
}
exit_err() {
echo >&2 "${1}"
exit 1
}
get_nodes() {
$KUBECTL get nodes -o json | jq '.items' > $1
}
get_pods() {
local TMP_FILE=$1
shift
$KUBECTL get pods -o json $@ | jq '.items' > $TMP_FILE
}
set_out_format() {
local output
local columns
local values
output=$(echo $1 | sed 's/custom-columns=//g')
IFS=',' read -r -a array <<< "$output"
for element in "${array[@]}"
do
value=$(echo $element | awk -F':' '{print $2","}')
if [[ $value == *".node.metadata.labels"* ]]; then
value='.node.metadata.labels."'$(echo $value | sed 's/.node.metadata.labels.//g' | sed 's/\\//g' | sed 's/,//g' )'"'
fi
columns+=$(echo $element | awk -F':' '{print "\""$1"\""","}')
values+=$(echo $value)
done
columns=$(echo $columns | sed 's/,*$//g')
values=$(echo $values | sed 's/,*$//g')
OUT_FORMAT='['$columns'], (.[] | ['$values'])'
}
run() {
TMP_NODE_FILE=$(mktemp /tmp/kubectl-wider.XXXXXX)
TMP_POD_FILE=$(mktemp /tmp/kubectl-wider.XXXXXX)
get_nodes $TMP_NODE_FILE
get_pods $TMP_POD_FILE $@
jq --slurpfile nodes $TMP_NODE_FILE '. |= map(.spec.nodeName as $nodeName | .node=($nodes[][] | select(.metadata.name == $nodeName)))' $TMP_POD_FILE \
| jq -r "$OUT_FORMAT | @tsv" \
| column -ts $'\t'
rm $TMP_NODE_FILE
rm $TMP_POD_FILE
}
main() {
if [[ -z "${KUBECTL:-}" ]]; then
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit 1
fi
fi
check_command "jq"
check_command "column"
for arg do
shift
[[ "$arg" == "-o" ]] && continue
if [[ "$arg" =~ "custom" ]]; then
set_out_format $arg
continue
fi
set -- "$@" "$arg"
done
run "$@"
}
main "$@"