-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuberes
executable file
·186 lines (140 loc) · 8.09 KB
/
kuberes
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
#!/bin/bash
MODE_0="raw"
MODE_1="list"
MODE_2="sum"
MODE_3="minmax"
MODE_4="total"
finish () {
# Print the result in a way that makes it possible to diff:
# - Resources are sorted by name.
# - If there are resources that share name, they are ordered by kind.
# - Object keys are sorted alphabetically
echo $TOTAL | jq 'sort_by(.name, .kind)' -s -S # -s receives inputs as array, -S sort object keys in output
# Clean Python background job
if [ -n "$PYTHON_CODE" ]; then
kill %%
fi
exit 0
}
MODE=$1
shift
if ! [[ "$MODE" =~ ^($MODE_0|$MODE_1|$MODE_2|$MODE_3|$MODE_4)$ ]]; then
cat <<EOF
$MODE is not a valid working mode
USAGE: $0 MODE FILE_1 FILE_2 ...
If no filenames are provided, will read from standard input
Modes:
- $MODE_0 : raw information from the YAML. Also add minReplicas and maxReplicas values based on Horizontal Pod Autoscalers
- $MODE_1 : top level objects with lists of the underlying container requests and limits
- $MODE_2 : top level objects with the sum of the underlying container requests and limits
- $MODE_3 : top level objects with minRequests, minLimits, maxRequests and maxLimits, taking into consideration minReplicas and maxReplicas
- $MODE_4 : summary of the total minRequests, minLimits, maxRequests and maxLimits across all objects
EOF
exit 1
fi
### MODE 0 #############################################################################################################
# Translate resources to JSON
JSON_RES=$(yq '.' $@)
# Separate resources by kind
POD=$(echo $JSON_RES | jq 'select(.kind == "Pod")')
RS=$(echo $JSON_RES | jq 'select(.kind == ("Deployment", "ReplicaSet", "StatefulSet", "ReplicationController"))')
HPA=$(echo $JSON_RES | jq 'select(.kind == "HorizontalPodAutoscaler")')
# TODO: think how to manage DaemonSet
# Homogenize the information of Pods and replication controllers
POD=$(echo $POD | jq '{apiVersion, kind, name: .metadata.name, containers: .spec.containers, minReplicas: 1, maxReplicas: 1}')
RS=$(echo $RS | jq '{apiVersion, kind, name: .metadata.name, containers: .spec.template.spec.containers, minReplicas: .spec.replicas, maxReplicas: .spec.replicas}')
# Update replication controllers with the min and max replica values form HorizontalPodAutoscaler
for i in $(echo $HPA | jq -c '.spec') ; do
FILTER=".apiVersion==$(echo $i | jq -c '.scaleTargetRef.apiVersion') and .kind==$(echo $i | jq -c '.scaleTargetRef.kind') and .name==$(echo $i | jq -c '.scaleTargetRef.name')"
MAX=$(echo $i | jq '.maxReplicas')
MIN=$(echo $i | jq '.minReplicas')
RS=$(echo $RS | jq "(select($FILTER) | . + {maxReplicas: $MAX, minReplicas: $MIN}), select($FILTER | not)")
done
TOTAL=$POD$RS
if [ $MODE == $MODE_0 ]; then
finish
fi
### MODE 1 #############################################################################################################
# Eliminate all the containers information except requests and limits
TOTAL=$(echo $TOTAL | jq '{apiVersion, kind, name, minReplicas, maxReplicas, requests: [(.containers[] | .resources.requests)], limits: [(.containers[] | .resources.limits)]}')
# Put together in arrays all the requests.cpu, requests.memory, limits.cpu and limits.memory
TOTAL=$(echo $TOTAL | jq '{apiVersion, kind, name, minReplicas, maxReplicas, requests: {cpu: [(.requests[] | .cpu)], memory: [(.requests[] | .memory)]}, limits: {cpu: [(.limits[] | .cpu)], memory: [(.limits[] | .memory)]}}')
if [ $MODE == $MODE_1 ]; then
finish
fi
### MODE 2 #############################################################################################################
# Sum the values of requests and limits taking units into consideration
PYTHON_CODE=$(cat <<'EOF'
import sys
from pint import UnitRegistry
file_in = sys.argv[1]
file_out = sys.argv[2]
ureg = UnitRegistry()
while True:
text = ''
with open(file_in, 'r') as fi:
while True:
line = fi.read()
if len(line) == 0:
break
text = line.strip().strip('"') # Accept lines surrounded by quotation marks
if len(text) != 0:
result = ureg(text)
with open(file_out, 'w') as fo:
fo.write(f'{result:~}'.replace(' ', '')) # Print in 100MiB format
EOF
)
# Create a background Python job with the unit parser to avoid constant reinitialization
PIPE_IN="/tmp/kuberes-pipe-in-$RANDOM"
PIPE_OUT="/tmp/kuberes-pipe-out-$RANDOM"
mkfifo $PIPE_IN
mkfifo $PIPE_OUT
python3 -c "$PYTHON_CODE" $PIPE_IN $PIPE_OUT &
T=""
for i in $(echo $TOTAL | jq -c '.') ; do
REQUESTS_CPU=$(echo $i | jq -r '[(.requests.cpu[] | tostring | .+"s")] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
LIMITS_CPU=$(echo $i | jq -r '[(.limits.cpu[] | tostring | .+"s")] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
REQUESTS_MEMORY=$(echo $i | jq -r '[(.requests.memory[] | tostring | .+"B")] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
LIMITS_MEMORY=$(echo $i | jq -r '[(.limits.memory[] | tostring | .+"B")] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
T=$T$(echo $i | jq "{apiVersion, kind, name, minReplicas, maxReplicas, requests: {cpu: \"$REQUESTS_CPU\", memory: \"$REQUESTS_MEMORY\"}, limits: {cpu: \"$LIMITS_CPU\", memory: \"$LIMITS_MEMORY\"}}")
done
TOTAL=$T
if [ $MODE == $MODE_2 ]; then
finish
fi
### MODE 3 #############################################################################################################
T=""
for i in $(echo $TOTAL | jq -c '.') ; do
REQUESTS_CPU=$(echo $i | jq -r '.requests.cpu')
LIMITS_CPU=$(echo $i | jq -r '.limits.cpu')
REQUESTS_MEMORY=$(echo $i | jq -r '.requests.memory')
LIMITS_MEMORY=$(echo $i | jq -r '.limits.memory')
MAX_REPLICAS=$(echo $i | jq -r '.maxReplicas')
MIN_REPLICAS=$(echo $i | jq -r '.minReplicas')
MAX_REQUESTS_CPU=$(echo "$MAX_REPLICAS * $REQUESTS_CPU" > $PIPE_IN && cat $PIPE_OUT)
MAX_REQUESTS_MEMORY=$(echo "$MAX_REPLICAS * $REQUESTS_MEMORY" > $PIPE_IN && cat $PIPE_OUT)
MAX_LIMITS_CPU=$(echo "$MAX_REPLICAS * $LIMITS_CPU" > $PIPE_IN && cat $PIPE_OUT)
MAX_LIMITS_MEMORY=$(echo "$MAX_REPLICAS * $LIMITS_MEMORY" > $PIPE_IN && cat $PIPE_OUT)
MIN_REQUESTS_CPU=$(echo "$MIN_REPLICAS * $REQUESTS_CPU" > $PIPE_IN && cat $PIPE_OUT)
MIN_REQUESTS_MEMORY=$(echo "$MIN_REPLICAS * $REQUESTS_MEMORY" > $PIPE_IN && cat $PIPE_OUT)
MIN_LIMITS_CPU=$(echo "$MIN_REPLICAS * $LIMITS_CPU" > $PIPE_IN && cat $PIPE_OUT)
MIN_LIMITS_MEMORY=$(echo "$MIN_REPLICAS * $LIMITS_MEMORY" > $PIPE_IN && cat $PIPE_OUT)
T=$T$(echo $i | jq "{apiVersion, kind, name, minReplicas, maxReplicas, requests: {cpu: {min: \"$MIN_REQUESTS_CPU\", max: \"$MAX_REQUESTS_CPU\"}, memory: {min: \"$MIN_REQUESTS_MEMORY\", max: \"$MAX_REQUESTS_MEMORY\"}}, limits: {cpu: {min: \"$MIN_LIMITS_CPU\", max: \"$MAX_LIMITS_CPU\"}, memory: {min: \"$MIN_LIMITS_MEMORY\", max: \"$MAX_LIMITS_MEMORY\"}}}")
done
TOTAL=$T
if [ $MODE == $MODE_3 ]; then
finish
fi
### MODE 4 #############################################################################################################
MAX_REQUESTS_CPU=$(echo $TOTAL | jq -s -r '[(.[] | .requests.cpu.max)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MAX_REQUESTS_MEMORY=$(echo $TOTAL | jq -s -r '[(.[] | .requests.memory.max)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MAX_LIMITS_CPU=$(echo $TOTAL | jq -s -r '[(.[] | .limits.cpu.max)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MAX_LIMITS_MEMORY=$(echo $TOTAL | jq -s -r '[(.[] | .limits.memory.max)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MIN_REQUESTS_CPU=$(echo $TOTAL | jq -s -r '[(.[] | .requests.cpu.min)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MIN_REQUESTS_MEMORY=$(echo $TOTAL | jq -s -r '[(.[] | .requests.memory.min)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MIN_LIMITS_CPU=$(echo $TOTAL | jq -s -r '[(.[] | .limits.cpu.min)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
MIN_LIMITS_MEMORY=$(echo $TOTAL | jq -s -r '[(.[] | .limits.memory.min)] | join(" + ")' > $PIPE_IN && cat $PIPE_OUT)
TOTAL="{\"requests\": {\"cpu\": {\"min\": \"$MIN_REQUESTS_CPU\", \"max\": \"$MAX_REQUESTS_CPU\"}, \"memory\": {\"min\": \"$MIN_REQUESTS_MEMORY\", \"max\": \"$MAX_REQUESTS_MEMORY\"}}, \"limits\": {\"cpu\": {\"min\": \"$MIN_LIMITS_CPU\", \"max\": \"$MAX_LIMITS_CPU\"}, \"memory\": {\"min\": \"$MIN_LIMITS_MEMORY\", \"max\": \"$MAX_LIMITS_MEMORY\"}}}"
if [ $MODE == $MODE_4 ]; then
finish
fi