forked from foundation-model-stack/multi-nic-cni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
executable file
·578 lines (527 loc) · 16.3 KB
/
script.sh
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#!/bin/bash
# yq
if [ -z ${OPERATOR_NAMESPACE} ]; then
OPERATOR_NAMESPACE="multi-nic-cni-operator"
fi
if [ -z ${DAEMON_STUB_IMG} ]; then
DAEMON_STUB_IMG="e2e-test/daemon-stub:latest"
fi
if [ -z ${CNI_STUB_IMG} ]; then
CNI_STUB_IMG="e2e-test/cni-stub:latest"
fi
probe_port=8083
get_controller() {
kubectl get po -n ${OPERATOR_NAMESPACE}|grep multi-nic-cni-operator-controller-manager|awk '{print $1}'
}
get_controller_log() {
controller=$(get_controller)
kubectl logs $controller -n ${OPERATOR_NAMESPACE} -c manager
}
restart_controller() {
controller=$(get_controller)
kubectl delete po $controller -n ${OPERATOR_NAMESPACE}
echo "Wait for deployment to be available"
kubectl wait deployment -n ${OPERATOR_NAMESPACE} multi-nic-cni-operator-controller-manager --for condition=Available=True --timeout=90s
ready=$(echo $(get_controller_log)|grep ConfigReady)
while [ -z "$ready" ];
do
sleep 5
echo "Wait for config to be ready..."
response=$(check_readyz)
echo "Readyz check: ${response}"
ready=$(echo $(get_controller_log)|grep ConfigReady)
done
echo "Config Ready"
}
deploy_controller() {
kubectl apply -f deploy/controller/deployment.yaml
kubectl wait deployment -n ${OPERATOR_NAMESPACE} multi-nic-cni-operator-controller-manager --for condition=Available=True --timeout=90s
ready=$(echo $(get_controller_log)|grep ConfigReady)
while [ -z "$ready" ];
do
sleep 5
echo "Wait for config to be ready..."
response=$(check_readyz)
echo "Readyz check: ${response}"
ready=$(echo $(get_controller_log)|grep ConfigReady)
done
echo "Config Ready"
deploy_fake_ds
}
deploy_fake_ds() {
kubectl delete ds multi-nicd -n ${OPERATOR_NAMESPACE}
kubectl apply -f deploy/controller/config.yaml
}
deploy_kwok() {
kubectl apply -f deploy/kwok/kwok.yaml
kubectl wait deployment -n kube-system kwok-controller --for condition=Available=True --timeout=90s
}
deploy_netattachdef(){
kubectl apply -f deploy/net-attach-def-crd.yaml
}
deploy_network(){
hb=$1
kubectl delete multinicnetwork --all
kubectl apply -f deploy/multinicnetwork/${hb}-hb-net.yaml
}
delete_controller() {
kubectl delete multinicnetwork --all
kubectl wait --for=delete multinicnetwork/multi-nic-sample --timeout=60s
kubectl delete config.multinic --all
kubectl wait --for=delete config.multinic/multi-nicd --timeout=60s
kubectl delete -f deploy/controller/deployment.yaml
}
port_forward() {
controller=$(get_controller)
kubectl port-forward ${controller} -n ${OPERATOR_NAMESPACE} ${probe_port}:${probe_port} > /dev/null
}
stop_port_forward(){
port_forward_pid=$(pidof kubectl)
kill ${port_forward_pid} > /dev/null
}
check_healthz(){
port_forward & sleep 5 && curl -s localhost:${probe_port}/healthz
stop_port_forward
}
check_readyz(){
port_forward & sleep 5 && curl -s localhost:${probe_port}/readyz
stop_port_forward
}
wait_node() {
for nodename in $(kubectl get nodes |awk '(NR>1){print $1}'); do
kubectl wait node ${nodename} --for condition=Ready --timeout=1000s
done
}
_deploy_node() {
i=$1
export podname=multi-nicd-stub-$i
export nodename=kwok-node-$i
export DAEMON_STUB_IMG=${DAEMON_STUB_IMG}
yq e '(.metadata.name = env(podname)),(.spec.containers[0].image = env(DAEMON_STUB_IMG),(.spec.containers[0].env[2].value = env(nodename)))' deploy/template/daemon-stub-pod.tpl|kubectl apply -n ${OPERATOR_NAMESPACE} -f - > /dev/null 2>&1
sleep 5
kubectl wait pod ${podname} -n ${OPERATOR_NAMESPACE} --for condition=Ready --timeout=1000s > /dev/null 2>&1
export hostIP=$(kubectl get po multi-nicd-stub-${i} -n ${OPERATOR_NAMESPACE} -oyaml|yq .status.podIP)
yq e '(.metadata.name=env(nodename)),(.metadata.labels."kubernetes.io/hostname"=env(nodename)),(.status.addresses[0].address=env(hostIP))' deploy/template/fake-node.tpl|kubectl apply -f - > /dev/null 2>&1
kubectl wait node ${nodename} --for condition=Ready --timeout=1000s > /dev/null 2>&1
}
deploy_n_node() {
from=$1
to=$2
pids=""
i=$from
while [ "$i" -le $to ]; do
_deploy_node $i&
pids="$pids $!"
i=$(( i + 1 ))
done
wait $pids
check_ip_sync $from $to
}
_delete_node() {
i=$1
export podname=multi-nicd-stub-$i
kubectl delete pod ${podname} -n ${OPERATOR_NAMESPACE} > /dev/null 2>&1
export nodename=kwok-node-$i
kubectl patch node ${nodename} -p '{"metadata":{"finalizers":null}}' --type=merge > /dev/null 2>&1
kubectl delete node ${nodename} > /dev/null 2>&1
kubectl delete po --field-selector spec.nodeName=${nodename} -n ${OPERATOR_NAMESPACE} --grace-period=0 > /dev/null 2>&1
}
delete_n_node() {
from=$1
to=$2
pids=""
i=$from
while [ "$i" -le $to ]; do
_delete_node $i&
pids="$pids $!"
i=$(( i + 1 ))
done
wait $pids
}
_reset_node() {
i=$1
echo "reset fake node $i, cool down 10s"
_delete_node $i
sleep 10
_deploy_node $i
}
_check_sync() {
i=$1
export podname=multi-nicd-stub-$i
export nodename=kwok-node-$i
nodeIP=$(kubectl get node ${nodename} -ojson|jq ".status.addresses[0].address")
if [ "$nodeIP" == "" ]; then
_reset_node $i
else
stubIP=$(kubectl get po ${podname} -n ${OPERATOR_NAMESPACE} -ojson|jq .status.podIP)
if [ "$stubIP" != "$nodeIP" ]; then
_reset_node $i
fi
fi
}
check_ip_sync() {
from=$1
to=$2
pids=""
i=$from
while [ "$i" -le $to ]; do
_check_sync $i
i=$(( i + 1 ))
done
}
_check_update_done() {
expr $(get_controller_log|grep "changeCIDR"|wc -l) % 2
}
log_failure() {
get_controller_log|grep "Failed"
get_controller_log|tail
}
wait_n() {
n=$1
cidrCount=$(kubectl get multinicnetwork -o 'jsonpath={..status.discovery.cidrProcessed}')
while [[ "$cidrCount" != "$n" ]]; do
prevCount=$cidrCount
sleep 10
response=$(check_healthz)
echo "Healthz check: ${response}"
waitingInQueue=$(get_controller_log|grep "Add UpdateRequest"|tail -1|awk '{ print substr($6,2) }')
cidrCount=$(kubectl get multinicnetwork -o 'jsonpath={..status.discovery.cidrProcessed}')
existDaemon=$(kubectl get multinicnetwork -o 'jsonpath={..status.discovery.existDaemon}')
infoAvailable=$(kubectl get multinicnetwork -o 'jsonpath={..status.discovery.infoAvailable}')
print_discovery_status
if [ "$existDaemon" == "$infoAvailable" ];then
updateDone=$(_check_update_done)
if [ "$updateDone" == "0" ]; then
if [ "$cidrCount" == "$prevCount" ]; then
log_failure
if [ "$waitingInQueue" == "0" ]; then
echo "No update trigger in Queue, potentially hang $existDaemon/$infoAvailable/$cidrCount"
check_cidr 1 $n
fi
echo "${waitingInQueue} trigger in queue"
fi
fi
else
check_ip_sync 1 $n
fi
done
}
wait_n_old_way() {
n=$1
len=$(kubectl get cidr -o 'jsonpath={..spec.cidr[0].hosts}'| jq '.|length')
while [[ $len != $n ]]; do
len=$(kubectl get cidr -o 'jsonpath={..spec.cidr[0].hosts}'| jq '.|length')
echo $len $(date -u +"%Y-%m-%dT%H:%M:%SZ")
sleep 5
done
}
wait_daemon() {
sleep 5
echo "Wait for daemonset to be ready"
kubectl rollout status daemonset multi-nicd -n ${OPERATOR_NAMESPACE} --timeout 300s
}
print_discovery_status() {
kubectl get multinicnetwork -o custom-columns=NAME:.metadata.name,Total:.status.discovery.existDaemon,Available:.status.discovery.infoAvailable,Processed:.status.discovery.cidrProcessed
}
clean_fake_cni() {
kubectl delete po --selector app=cni-stub -n ${OPERATOR_NAMESPACE} > /dev/null 2>&1
kubectl delete job --selector app=cni-stub -n ${OPERATOR_NAMESPACE} > /dev/null 2>&1
}
add_pod() {
from=$1
to=$2
starti=$3
n=$4
i=$from
export CNI_STUB_IMG=${CNI_STUB_IMG}
while [ "$i" -le $to ]; do
export hostName=kwok-node-$i
export hostIP=$(kubectl get po multi-nicd-stub-${i} -n ${OPERATOR_NAMESPACE} -oyaml|yq .status.podIP)
export jobName=cni-${hostName}
export args="./cni --command=add --start=${starti} --n=${n} --host=${hostName} --dip=${hostIP}"
export hostIP=$(kubectl get po multi-nicd-stub-${i} -n ${OPERATOR_NAMESPACE} -oyaml|yq .status.podIP)
yq e '(.metadata.name=env(jobName)),(.spec.template.spec.containers[0].args=[env(args)]),(.spec.template.spec.containers[0].image = env(CNI_STUB_IMG))' deploy/template/cni-stub-job.tpl|kubectl apply -n ${OPERATOR_NAMESPACE} -f - > /dev/null 2>&1
i=$(( i + 1 ))
done
kubectl wait --for=condition=complete --timeout=1000s job --selector app=cni-stub -n ${OPERATOR_NAMESPACE} > /dev/null 2>&1
clean_fake_cni
}
add_pod_with_step() {
from=$1
to=$2
step=$3
starti=$4
n=$5
i=$from
while [ "$i" -le $to ]; do
nfrom=$i
nto=$(( i + $step ))
add_pod $nfrom $nto $starti $n
i=$nto
done
}
delete_pod() {
from=$1
to=$2
starti=$3
n=$4
i=$from
while [ "$i" -le $to ]; do
export hostName=kwok-node-$i
export hostIP=$(kubectl get po multi-nicd-stub-${i} -n ${OPERATOR_NAMESPACE} -oyaml|yq .status.podIP)
export jobName=cni-${hostName}
export args="./cni --command=delete --start=${starti} --n=${n} --host=${hostName} --dip=${hostIP}"
export hostIP=$(kubectl get po multi-nicd-stub-${i} -n ${OPERATOR_NAMESPACE} -oyaml|yq .status.podIP)
yq e '(.metadata.name=env(jobName)),(.spec.template.spec.containers[0].args=[env(args)]),(.spec.template.spec.containers[0].image = env(CNI_STUB_IMG))' deploy/template/cni-stub-job.tpl|kubectl apply -n ${OPERATOR_NAMESPACE} -f - > /dev/null 2>&1
i=$(( i + 1 ))
done
kubectl wait --for=condition=complete --timeout=1000s job --selector app=cni-stub -n ${OPERATOR_NAMESPACE} > /dev/null 2>&1
clean_fake_cni
}
delete_pod_with_step() {
from=$1
to=$2
step=$3
starti=$4
n=$5
i=$from
while [ "$i" -le $to ]; do
nfrom=$i
nto=$(( i + $step ))
delete_pod $nfrom $nto $starti $n
i=$nto
done
}
check_ippool() {
from=$1
to=$2
n=$3
i=$from
while [ "$i" -le $to ]; do
export "nodename=kwok-node-$i"
ippools=$(kubectl get ippool -o custom-columns=NAME:.metadata.name,HOST:.spec.hostName|grep $nodename$|awk {'print $1'})
for ippool in $ippools; do
export len=$(kubectl get ippool $ippool -o json| jq '.spec.allocations | length')
if [ "$len" != "$n" ] ; then
echo >&2 "Fatal error: $ippool of $nodename $len != $n"
exit 2
else
echo "IPPool $ippool of $nodename checked ($n)"
fi
done
i=$(( i + 1 ))
done
}
check_ippool_all() {
n=$1
ippools=$(kubectl get ippools -ojson|jq .items)
for row in $(echo "${ippools}" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
export allocations=$(_jq '.spec.allocations')
export len=$(echo $allocations| jq '. | length')
if [ "$len" != "$n" ] ; then
echo >&2 "Fatal error: $len != $n"
exit 2
else
echo "IPPool checked ($n)"
fi
done
}
check_hostinterface() {
from=$1
to=$2
i=$from
while [ "$i" -le $to ]; do
export nodename=kwok-node-$i
len=$(kubectl get hostinterface ${nodename} -ojson|jq '.spec.interfaces|length')
if [ "$len" != 3 ] ; then
echo >&2 "Fatal error: ${nodename} interface length $len != 3"
exit 2
else
echo "HostInterface checked (${nodename})"
fi
i=$(( i + 1 ))
done
}
check_cidr(){
from=$1
to=$2
cidr=$(kubectl get cidr multi-nic-sample -ojson|jq .spec.cidr)
n=$((${to}-${from}+1))
vlanlen=$(echo $cidr| jq '. | length')
if [ "$n" != 0 ] && [ "$vlanlen" -lt 3 ] ; then
echo >&2 "Fatal error: interface length $n != 0 and $vlanlen < 3"
exit 2
else
total_hostlen=0
i=0
while [ "$i" -lt "$vlanlen" ]; do
hosts=$(echo $cidr| jq .[${i}].hosts)
hostlen=$(echo $hosts| jq '.|length')
total_hostlen=$(( total_hostlen + hostlen ))
i=$(( i + 1 ))
done
hostlen_per_iface=$(( total_hostlen / 3 ))
if [ "$hostlen_per_iface" != $n ] ; then
echo >&2 "Fatal error: host length $hostlen_per_iface != $n"
exit 2
fi
echo "CIDR checked (${n})"
fi
}
check_cidr_change(){
cidrChanges=$(get_controller_log|grep "changeCIDR"|wc -l|tr -d ' ')
if [ "$cidrChanges" != 0 ] ; then
echo >&2 "Fatal error: CIDR changed (${cidrChanges})"
exit 2
fi
echo "CIDR has no change"
}
watch_network() {
kubectl get multinicnetwork -o custom-columns=NAME:.metadata.name,Total:.status.discovery.existDaemon,Available:.status.discovery.infoAvailable,Processed:.status.discovery.cidrProcessed,Time:.status.lastSyncTime -w
}
test_scale() {
deploy_network 8
echo $(date -u +"%Y-%m-%dT%H:%M:%SZ")
START=$(date +%s)
time deploy_n_node 1 100
time wait_n 100
check_hostinterface 1 100
check_cidr 1 100
time deploy_n_node 101 200
time wait_n 200
check_hostinterface 1 200
check_cidr 1 200
test_clean
END=$(date +%s)
echo $((END-START)) | awk '{print "Test time: "int($1/60)":"int($1%60)}'
}
test_step_scale() {
deploy_network 8
echo $(date -u +"%Y-%m-%dT%H:%M:%SZ")
START=$(date +%s)
time deploy_n_node 1 10
time wait_n 10
check_cidr 1 10
time deploy_n_node 11 20
time wait_n 20
check_cidr 1 20
time deploy_n_node 21 50
time wait_n 50
check_cidr 1 50
time deploy_n_node 51 100
time wait_n 100
check_cidr 1 100
time deploy_n_node 101 200
time wait_n 200
check_cidr 1 200
test_step_clean
export END=$(date +%s)
echo $((END-START)) | awk '{print "Test time: "int($1/60)":"int($1%60)}'
}
test_clean() {
time delete_n_node 101 200
time wait_n 100
check_cidr 1 100
time delete_n_node 1 100
time wait_n 0
check_cidr 1 0
}
test_step_clean() {
time delete_n_node 101 200
time wait_n 100
check_cidr 1 100
time delete_n_node 51 100
time wait_n 50
check_cidr 1 50
time delete_n_node 21 50
time wait_n 20
check_cidr 1 20
time delete_n_node 11 20
time wait_n 10
check_cidr 1 10
time delete_n_node 1 10
time wait_n 0
check_cidr 1 0
}
test_small_scale() {
deploy_network 8
echo $(date -u +"%Y-%m-%dT%H:%M:%SZ")
START=$(date +%s)
time deploy_n_node 1 10
time wait_n 10
check_cidr 1 10
time deploy_n_node 11 20
time wait_n 20
check_cidr 1 20
time delete_n_node 1 20
time wait_n 0
check_cidr 1 0
}
test_allocate() {
echo "Deploying 10 nodes"
time deploy_n_node 1 10
time wait_n 10
check_cidr 1 10
echo "Start test"
export START=$(date +%s)
time add_pod 1 10 1 5
check_ippool 1 10 5
time delete_pod 1 10 1 5
check_ippool 1 10 0
export END=$(date +%s)
echo $((END-START)) | awk '{print "Test time: "int($1/60)":"int($1%60)}'
echo "Cleaning nodes"
time delete_n_node 1 10
time wait_n 0
check_cidr 1 0
}
test_taint() {
echo "Deploying 5 nodes"
time deploy_n_node 1 5
time wait_n 5
check_cidr 1 5
echo "Taint a node"
kubectl taint node kwok-node-5 test=true:NoSchedule
echo "Add pods to non-taint"
time add_pod 1 4 1 1
check_ippool 1 4 1
echo "Untaint"
kubectl taint node kwok-node-5 test-
echo "Add pods to untaint node"
time add_pod 5 5 1 1
check_ippool 1 5 1
echo "Taint already allocated node"
kubectl taint node kwok-node-1 test=true:NoSchedule
check_ippool 1 5 1
echo "Untaint"
kubectl taint node kwok-node-1 test-
check_ippool 1 5 1
time delete_pod 1 5 1 1
check_ippool 1 5 0
echo "Cleaning nodes"
time delete_n_node 1 5
time wait_n 0
check_cidr 1 0
}
test_resilience() {
echo "Deploying 5 nodes"
deploy_n_node 1 5
wait_n 5
check_cidr 1 5
echo "Restart controller"
restart_controller
check_cidr_change
echo "Restart multi-nicd"
kubectl delete po -l app=multi-nicd -n ${OPERATOR_NAMESPACE}
wait_daemon
wait_n 5
check_cidr_change
echo "Cleaning nodes"
delete_n_node 1 5
wait_n 0
check_cidr 1 0
}
"$@"