forked from RobertKrawitz/OpenShift4-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-worker-parameters
executable file
·170 lines (150 loc) · 4.83 KB
/
set-worker-parameters
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
#!/bin/bash
# Copyright 2020-2022 Robert Krawitz/Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Unfortunate that there's no way to tell shellcheck to always source
# specific files.
# shellcheck disable=SC2034
# See the bash change log, differences between 4.4-beta2 and 4.4-rc2:
# a. Using ${a[@]} or ${a[*]} with an array without any assigned elements when
# the nounset option is enabled no longer throws an unbound variable error.
declare -A args
declare argsSet=0
declare oconfig
declare -r configName='set-max-pods'
declare -i force=0
declare -i waitOnly=0
declare -i oldGeneration=0
declare -i newGeneration=0
function help() {
cat <<EOF
Usage: $0 [options]
-f Attempt to force changes even if the config
appears unchanged
-p maxPods Maximum pods/node
-b KubeAPIBurst Kubelet-apiserver burst queries/sec
-q KubeAPIQPS Kubelet-apiserver sustained queries/sec
-X option=value Any other option and value
EOF
exit 1
}
while getopts "fhp:b:q:X:" opt ; do
case "$opt" in
f) force=1 ;;
p) args[maxPods]="$OPTARG"; argsSet=1 ;;
b) args[KubeAPIBurst]="$OPTARG"; argsSet=1 ;;
q) args[KubeAPIQPS]="$OPTARG"; argsSet=1 ;;
X)
option=${OPTARG%%=*}
value=${OPTARG#*=}
if [[ -n $value ]] ; then
args[$option]=$value
argsSet=1
else
unset "args[$option]"
fi
;;
*) help ;;
esac
done
if ((! argsSet )) ; then
echo "No options set"
help
fi
function get_var() {
jq -r "[.$1?] | map (select (. != null) | tostring) |join(\"\")" <<< "$2"
}
function test_unchanged() {
local k
local v
local -i changed=0
for k in "${!args[@]}" ; do
v="$(get_var "$k" "$1")"
if [[ ${args[$k]} != "$v" ]] ; then
echo "$k ${v:-(null)} => ${args[$k]:-(null)}"
changed=1
else
echo "$k $v (unchanged)"
fi
done
if ((! changed)) ; then
if ((force)) ; then
waitOnly=1
else
echo "*** Kubeconfig is unchanged, not updating"
exit 0
fi
fi
}
oconfig="$(oc get kubeletconfig.machineconfiguration.openshift.io/$configName -ojson 2>/dev/null | jq '.spec.kubeletConfig')"
test_unchanged "$oconfig"
function get_machine_status() {
oc get machineconfigpool worker -ojson | jq -r '[.metadata.name, (.metadata.generation | tostring), ([foreach .status.conditions[] as $cond ([[],[]];0; if ($cond.status == "True") then $cond.type else null end)] | flatten | map (select (. != null)) | join(" ")), (.status.machineCount | tostring), (.status.degradedMachineCount | tostring), (.status.readyMachineCount | tostring), (.status.unavailableMachineCount | tostring), (.status.updatedMachineCount | tostring)] | join("|")'
}
function tsec() {
printf "%(%s)T" -1
}
function ptime() {
local -i start=$1
local -i interval="$(($(tsec) - start))"
if (( interval >= 3600 )) ; then
printf "%d:%02d:%02d" "$((interval / 3600))" "$(((interval % 3600) / 60))" "$((interval % 60))"
else
printf "%d:%02d" "$((interval / 60))" "$((interval % 60))"
fi
}
if [[ $(oc get machineconfigpool worker -o json | jq -r '.metadata.labels."custom-kubelet"?' 2>/dev/null) != 'large-pods' ]] ; then
oc label --overwrite machineconfigpool worker custom-kubelet=large-pods
fi
oldGeneration=$(oc get mcp worker -ojsonpath='{.status.observedGeneration}')
oc apply -f - <<EOF
apiVersion: machineconfiguration.openshift.io/v1
kind: KubeletConfig
metadata:
name: "$configName"
spec:
machineConfigPoolSelector:
matchLabels:
custom-kubelet: large-pods
kubeletConfig:
apiVersion: machineconfiguration.openshift.io/v1
kind: KubeletConfig
$(for arg in "${!args[@]}" ; do
echo " $arg: ${args[$arg]}"
done)
EOF
if ((! waitOnly)) ; then
while : ; do
newGeneration=$(oc get mcp worker -ojsonpath='{.status.observedGeneration}')
if ((newGeneration != oldGeneration)) ; then break; fi
sleep 1
done
fi
declare -i start
declare -i readycount=0
start=$(tsec)
while : ; do
# shellcheck disable=SC2034
IFS='|' read -r name generation status count degradedcount readycount unavailable updatedcount < <(get_machine_status)
if [[ ${status,,} = *'degraded' ]] ; then
echo "Machine config is degraded!"
exit 1
fi
echo -en "\r($(ptime "$start")) Waiting for nodes to become ready ($readycount / $count)..."
if [[ ${status,,} = updated ]] ; then
echo 'ready!'
break
fi
sleep 10
done
exit 0