-
Notifications
You must be signed in to change notification settings - Fork 24
/
install-custom-kubelet
executable file
·380 lines (337 loc) · 10 KB
/
install-custom-kubelet
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
372
373
374
375
376
377
378
379
380
#!/bin/bash
# Find our helpers
function finddir() {
local path_to_file
path_to_file=$(readlink -f "$0")
if [[ -z $path_to_file ]] ; then
return 1
elif [[ -d $path_to_file ]] ; then
echo "$path_to_file/"
elif [[ -e $path_to_file ]] ; then
echo "${path_to_file%/*}/"
else
return 1
fi
return 0
}
cmddir=$(finddir "$0")
[[ -z $cmddir ]] && fatal "Can't find directory for $0"
PATH="${cmddir}:$PATH"
set -eu
# This was renamed from hyperkube to kubelet between 4.1 and 4.2, evidently.
declare kubelet_bin=kubelet
declare godir=${GOPATH:-$HOME/go}
declare -r srcdir="$godir/src/github.com/openshift/origin"
declare lkubelet="$srcdir/_output/local/bin/linux/amd64/${kubelet_bin}"
declare bastion=bastion-
declare -i rebuild=1
declare -i do_setup_cert=0
declare -i do_uninstall=0
declare -i twidth=1000
declare -i install_all=0
declare -i install_infra=0
declare -i install_master=0
declare -i install_worker=1
declare -i copy_kubelet=1
declare -i parallel_key=1
declare -i parallel_kube=1
declare private_key=
function help() {
cat <<EOF
Usage: $0 [options] [specific_nodes...]
Args:
-A Don't install on all nodes (default)
-a Install on all nodes
-B Don't use ssh bastion host (direct ssh)
-b Use a bastion ssh host (default)
-C Don't setup node certs for ssh (default)
-c Set up node certs for ssh (must be done once)
-I Don't install on infra nodes (default)
-i Install on infra nodes
-k file Use the specified kubelet file
-K file Use the specified private key file
-M Don't install on master nodes (default)
-m Install on master nodes
-N Don't copy the kubelet to remote (must be there)
-n Copy the kubelet to the remote (default)
-p N Set up ssh keys N-way parallel
-P N Install kubelets N-way paralleo
-R Don't attempt to rebuild the kubelet
-r Rebuild the kubelet (default)
-U Install custom kubelet (default)
-u Uninstall (restore original kubelet)
-W Don't install on worker nodes
-w Install on worker nodes (default)
EOF
exit 1
}
while getopts "AaBbCchIiK:k:MmNnP:p:RrUuWw" opt ; do
case "$opt" in
A) install_all=0 ;;
a) install_all=1 ;;
B) bastion= ;;
b) bastion=bastion- ;;
C) do_setup_cert=0 ;;
c) do_setup_cert=1 ;;
I) install_infra=0 ;;
i) install_infra=1 ;;
K) private_key="$OPTARG" ;;
k) lkubelet="$OPTARG"; rebuild=0 ;;
M) install_master=0 ;;
m) install_master=1 ;;
N) copy_kubelet=0 ;;
n) copy_kubelet=1 ;;
P) parallel_kube="$OPTARG" ;;
p) parallel_key="$OPTARG" ;;
R) rebuild=0 ;;
r) rebuild=1 ;;
U) do_uninstall=0 ;;
u) do_uninstall=1 ;;
W) install_worker=0 ;;
w) install_worker=1 ;;
*) help ;;
esac
done
shift $((OPTIND - 1))
declare -a all_nodes=()
if [[ -n "$*" ]] ; then
all_nodes=("$@")
else
# shellcheck disable=SC2034
while read -r node status class rest ; do
if [[ ($install_all -eq 1) ||
($install_infra -eq 1 && $class == *infra*) ||
($install_master -eq 1 && $class == *master*) ||
($install_worker -eq 1 && $class == *worker*) ]] ; then
all_nodes+=("$node")
fi
done < <(oc get nodes --no-headers=true)
fi
function rcmd() {
"${bastion}ssh" "$@"
}
function rcp() {
"${bastion}scp" "$@"
}
function build_upload() {
cd "$srcdir"
make WHAT=vendor/k8s.io/kubernetes/cmd/${kubelet_bin}
ls -l _output/local/bin/linux/amd64/${kubelet_bin}
md5sum _output/local/bin/linux/amd64/${kubelet_bin}
}
if (( rebuild && !do_uninstall && copy_kubelet )) ; then
(build_upload)
fi
declare expected_md5
expected_md5=$(md5sum "$lkubelet" |awk '{print $1}') || {
echo "Can't checksum $lkubelet"
exit 1
}
function generate_grappling_hook_install() {
cat <<'EOF'
function generate_install_kubelet() {
cat <<'EOL'
#!/bin/bash
declare lkubelet="$1"
declare -r expected_md5="$2"
declare -r lbindir="/usr/bin"
function die() {
if [[ $1 = -r ]] ; then
cd /
mount -o remount,ro /usr 2>&1 |grep -v 'mount point is busy'
fi
echo "$*"
exit 1
}
if [[ $lkubelet = *.gz ]] ; then
rm -f "${lkubelet%.gz}"
gunzip "$lkubelet"
lkubelet=${lkubelet%.gz}
fi
cd "$lbindir" || die "Can't cd $lbindir!"
[[ -f "$lkubelet" ]] || die "Missing kubelet $lkubelet"
found_md5=$(md5sum "$lkubelet" |awk '{print $1}')
[[ $(stat -c %s "$lkubelet") -ge 1048576 && "$expected_md5" = "$found_md5" ]] || die "Bad kubelet (expected $expected_md5, found $found_md5)"
sudo mount -o remount,rw /usr || die "Can't remount /usr rw"
sudo cp "$lkubelet" @[email protected]
cmp "$lkubelet" @[email protected] || die -r "$lkubelet and @[email protected] failed compare!"
if [[ -f @KUBELET@ && ! -f @[email protected] ]] ; then
sudo mv @KUBELET@ @[email protected]
fi
sudo mv @[email protected] @KUBELET@ || die -r "Can't rename @[email protected] to @KUBELET@!"
cd /
sudo mount -o remount,ro /usr 2>&1 |grep -v 'mount point is busy'
sudo sysctl -w kernel.pid_max=4194304 >/dev/null
sudo systemctl restart kubelet.service
ps aux |grep /usr/bin/@KUBELET@ |grep kubelet |grep -v grep
EOL
}
function install_kubelet_n() {
local parallel_kube=$1
shift
local rotor=$1
shift
set -e
while (( rotor < ${#all_nodes[@]} )) ; do
local node=${all_nodes[$rotor]}
echo "$node: " "$(tar cf - install-kubelet @[email protected] | ssh -o StrictHostKeyChecking=no -i ~/.ssh/temp_install "$node" 'cd /var/tmp; rm -f install-kubelet @[email protected]; tar xf -; /var/tmp/install-kubelet /var/tmp/@[email protected] '"$expected_md5")"
rotor=$((rotor + parallel_kube))
done
}
set -u
expected_md5="$1"
shift
parallel_kube="$1"
myname="${all_nodes[0]}"
if (( ${#all_nodes[@]} < parallel_kube - 1)) ; then
parallel_kube=$((${#all_nodes[@]} - 1))
fi
cd /var/tmp
generate_install_kubelet > install-kubelet
chmod +x install-kubelet
chmod +x @KUBELET@
echo "$myname: " "$(./install-kubelet /var/tmp/@KUBELET@ "$expected_md5")"
rm -f /var/tmp/@[email protected]
gzip -c /var/tmp/@KUBELET@ > /var/tmp/@[email protected]
chmod +x /var/tmp/@[email protected]
# Don't try to copy this to ourself.
for i in $(seq 1 $parallel_kube) ; do
install_kubelet_n "$parallel_kube" "$i" &
done
wait
rm -f /var/tmp/@[email protected]
ls -l /usr/bin/@KUBELET@
EOF
}
function generate_grappling_hook_uninstall() {
cat <<'EOF'
function generate_uninstall_kubelet() {
cat <<'EOL'
#!/bin/bash
declare -r lbindir="/usr/bin"
function die() {
if [[ $1 = -r ]] ; then
cd /
mount -o remount,ro /usr 2>&1 |grep -v 'mount point is busy'
fi
echo "$*"
exit 1
}
cd "$lbindir" || die "Can't cd $lbindir!"
if [[ -f @[email protected] ]] ; then
sudo mount -o remount,rw /usr || die "Can't remount /usr rw"
sudo mv @[email protected] @KUBELET@ || die -r "Can't rename @[email protected] to @KUBELET@"
cd /
sudo mount -o remount,ro /usr 2>&1 |grep -v 'mount point is busy'
sudo systemctl restart kubelet.service
ps aux |grep @KUBELET@ |grep kubelet |grep -v grep
else
echo "No old @KUBELET@ to restore"
fi
EOL
}
function uninstall_kubelet_n() {
local parallel_kube=$1
shift
local rotor=$1
shift
while (( rotor < ${#all_nodes[@]} )) ; do
local node=${all_nodes[$rotor]}
echo "$node: " "$(ssh -o StrictHostKeyChecking=no -i ~/.ssh/temp_install "$node" 'cat > /var/tmp/uninstall-kubelet; chmod +x /var/tmp/uninstall-kubelet; /var/tmp/uninstall-kubelet' < uninstall-kubelet)"
rotor=$((rotor + parallel_kube))
done
}
set -u
shift
parallel_kube="$1"
myname="${all_nodes[0]}"
if (( ${#all_nodes[@]} < parallel_kube - 1)) ; then
parallel_kube=$((${#all_nodes[@]} - 1))
fi
cd /var/tmp
generate_uninstall_kubelet > uninstall-kubelet
chmod +x uninstall-kubelet
echo "$myname: " "$(./uninstall-kubelet)"
# Don't try to re-uninstall ourself.
for i in $(seq 1 $parallel_kube) ; do
uninstall_kubelet_n "$parallel_kube" "$i" &
done
wait
ls -l /usr/bin/@KUBELET@
EOF
}
function generate_grappling_hook_1() {
echo "#!/bin/bash"
local -a nodes=()
readarray -t nodes
echo "declare -a all_nodes=(${nodes[*]@Q})"
if (( do_uninstall )) ; then
generate_grappling_hook_uninstall
else
generate_grappling_hook_install
fi
}
function generate_grappling_hook() {
generate_grappling_hook_1 | sed "s/@KUBELET@/$kubelet_bin/g"
}
function install_ssh_key_n() {
local parallel_key=$1
local rotor=$2
local pdir=${private_key%/*}
if [[ -n "$pdir" ]] ; then
pdir="-C $pdir"
fi
local pkey=${private_key##*/}
set -e
while (( rotor < ${#all_nodes[@]} )) ; do
local f=${all_nodes[$rotor]}
# shellcheck disable=SC2086
tar c $pdir -f - "$pkey" "${pkey}.pub" | rcmd "$f" "cd .ssh; rm -f '$pkey' '${pkey}.pub'; tar xf -; sudo chown core authorized_keys; sudo chmod 644 authorized_keys; cat '${pkey}.pub' >> authorized_keys"
rotor=$((rotor + parallel_key))
done
}
function setup_cert() {
local tmpdir
local private_key="$1"
local i
if (( ${#all_nodes[@]} < parallel_key )) ; then
parallel_key=${#all_nodes[@]}
fi
if [[ -z "$private_key" ]] ; then
tmpdir=$(mktemp -d) || {
echo "Can't create temporary cert dir"
exit 1
}
private_key="$tmpdir/temp_install"
ssh-keygen -q -f "$private_key" -N '' || {
echo "Can't create temporary key"
rm -rf "$tmpdir"
exit 1
}
fi
echo "Install key:"
for i in $(seq 0 $((parallel_key - 1))) ; do
install_ssh_key_n "$parallel_key" "$i"&
done
wait
if [[ -n "${tmpdir:-}" && -d "${tmpdir:-}" ]] ; then
rm -rf "$tmpdir"
fi
}
function print_node_names() {
(IFS='
'; echo "${all_nodes[*]}")
}
if [[ -n $TERM ]] ; then
twidth=$(tput cols)
fi
if (( do_setup_cert )) ; then
setup_cert "$private_key"
fi
if (( copy_kubelet && ! do_uninstall )) ; then
echo "Copy $lkubelet to remote"
rcp -C "$lkubelet" "${all_nodes[0]}:/var/tmp"
fi
echo "Set up kubelet"
generate_grappling_hook < <(print_node_names "$@") | rcmd "${all_nodes[0]}" "cat > /var/tmp/kubelet-grappling-hook; chmod +x /var/tmp/kubelet-grappling-hook; /var/tmp/kubelet-grappling-hook $expected_md5 $parallel_kube" |cut "-c1-$twidth"
oc get nodes