forked from RobertKrawitz/OpenShift4-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterbuster
executable file
·2841 lines (2658 loc) · 78.2 KB
/
clusterbuster
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright 2019-2020 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.
set -u
declare -i namespaces=1
declare -i depsPerNamespace=1
declare -i secrets=0
declare -i replicas=1
declare -i parallel=1
declare -i firstDeployment=0
declare -i sleepSecrets=0
declare -i sleepNamespaces=0
declare -i sleepDeployments=0
declare -i parallelSecrets=0
declare -i parallelNamespaces=0
declare -i parallelDeployments=0
declare -i blocksize=1
declare -i blocksizeSecrets=0
declare -i blocksizeNamespaces=0
declare -i blocksizeDeployments=0
declare -i containers=1
declare -i createRoutes=0
declare -i sleeptime=0
declare -i doit=1
declare -i blockitemcount=0
declare -i port=7777
declare -i syncPort=7778
declare -i affinity=0
declare -i verbose=0
declare -i wait_for_secrets=1
declare dataRate=0
declare -i bytes=0
declare -i bytesMax=0
declare -i msgSize=32768
declare -i poddelay=0
declare -i xferTime=0
declare -i xferTimeMax=0
declare -i exitAtEnd=0
declare -i reportObjectCreation=1
declare -i defaultBytes=1000000000
declare -A objectsCreated=()
declare podType=ClusterBuster
declare podsleeptime=infinity
declare basename=clusterbuster
declare deploymentType=deployment
declare -i basetime
declare opt
declare -r nl=$'\n'
declare -a pod_resource_requests=()
declare -a pod_resource_limits=()
declare -a container_resource_requests=()
declare -a container_resource_limits=()
declare -A namespaces_in_use=()
declare -a namespaces_to_create=()
declare -i scale_ns=0
declare -i scale_deployments=1
declare -i syncStart=0
declare runtimeClass=
declare -a emptydirs=()
declare -a sysbench_generic_options=()
declare -a sysbench_fileio_options=()
declare -A sysbench_fileio_tests=()
declare -i report=0
declare -i verbose_report=0
declare -i cleanup=0
declare -i timeout=-1
declare pathdir=${0%/*}
declare -a unknown_opts=()
declare -i totalObjectsCreated=0
declare accumulateddata=
declare OC
OC=$(type -p oc)
OC=${OC:-$(type -p kubectl)} # kubectl might not work, though...
function _helpmsg() {
local opt
for opt in "$@" ; do
echo "Unknown option $opt"
done
cat <<EOF
Usage: $0 [options] [name]
Options:
-B basename Base name of pods (default clusterbuster).
All objects are labeled with this name.
-b blocksize Number of objects to create in one oc call (default 1)
-c containers Number of containers per pod
-d deployments Number of deployments per namespace (default 1)
-h Print this help message
-H Print extended help. Use --help-everything to
get an extended description.
-n Print what would be done without doing it
-N namespaces Number of namespaces to create (default 1)
-P podtype Pod type (classic, pause, server, cpu, sysbench)
-p parallel Number of operations to perform in parallel (default 1)
-r replicas Number of replicas per deployment (default 1)
For client/server, this refers to client pods.
For cpu, this refers to processes per pod.
-S Scale up (add namespaces to existing load) (default no)
-s secrets Number of secrets per deployment (default 0)
-T type Use the specified deployment type:
pod or deployment (default $deploymentType)
-w wait Number of seconds to wait between ops (default 0)
-W Wait for secrets to be created (default 1)
-z podsleep Amount of time in seconds for pod to sleep before exit
(default infinity). This may be an expression evaluated
as an argument to sleep(1) and need not be a numeric
constant.
--opt[=val] Set the specified option. Use -H or --help-everything
to get a list.
-Y Sync start of data transfer or CPU load (default no)
Client/server options:
-D data rate Data rate for client/server (MB/sec) (default 0)
May be a decimal number.
-E Don't exit after all data transmitted (default)
-e Exit after all data transmitted.
-M MB[,max] Amount of data to transfer. If two values are
specified, the amount will be randomly chosen
per container.
-m msgsize Message size in data transfer
-t time[,max] Time (in seconds) to transfer data (default 0
means no time limit). If two values are specified,
the amount will be randomly chosen per container.
-v Print verbose log messages. Useful with
clusterbuster-connstat.
-x Use pod affinity between client and server (default no)
-X Use pod anti-affinity between client/server (default no)
EOF
}
function _helpExtended() {
_helpmsg
cat <<'EOF'
Extended Options:
General Extended Options (short equivalents):
--pod_type=type Specify the type of pod (-P)
--basename=name Specify the base name (-B)
--containers=N Number of containers per pod (-c)
--namespaces=N Number of namespaces (-N)
--secrets=N Number of secrets (-s)
--replicas=N Number of replicas per deployment (-r)
--podsleep=N Time for pod to sleep before exit (-z)
--report Print report (client/server, soaker,
sysbench only)
--verbose-report Print detailed report
--cleanup Clean up generated objects
--timeout=N Time out reporting
Tuning object creation (short equivalents):
--scale-ns=[0,1] Scale up the number of namespaces.
(default 0)
--scale-deployments=[0,1]
Scale up the number of deployments.
(default 1)
--first_deployment=N
Specify the index of the first deployment.
Default is 0 (but see --scale_deployments)
--first_secret=N
--first_namespace=N
--first_deployment=N
--blocksize=N Number of objects per oc call (-b)
Below options default to blocksize
--blocksize_secrets=N
--blocksize_namespaces=N
--blocksize_deployments=N
--sleep=N Number of seconds between object creations (-w)
Below options default to sleeptime
--sleep_secrets=N
--sleep_namespaces=N
--sleep_deployments=N
--parallel=N Number of operations in parallel (-p)
Below options default to parallel
--parallel_secrets=N
--parallel_namespaces=N
--parallel_deployments=N
Pod Options:
--<type>=<resource=value>
Resource requests and limits
<type> is pod_resource_request, pod_resource_limit,
container_resource_request, or container_resource_limit
--runtimeclass=class
Run the pods in the designated runtimeclass.
--kata Synonym for --runtimeclass=kata-qemu
--emptydir=dir Mount an emptydir volume at the specified mount point.
Client/server options:
--affinity Force affinity between client and server pods
in a deployment.
--anti-affinity Force anti-affinity between client and server pods
in a deployment.
--bytes=N[,M] Number of bytes to transfer
Also --kb, --kib, --mb, --mib, --gb, --gib
(-M is equivalent to --mb)
--create_routes Create routes for services (default 0)
--datarate=N Data transfer rate (-D)
--exit_at_end Exit upon completion of transfer (-e)
--msgsize=N Message size for data transfer (-m)
--poddelay=N Time in seconds to wait for pods to start data xfer
--transfertime=N Time to transfer data (-t). Two comma-separated
numbers may be used to specify maximum time.
--verbose Print verbose log messages (-v)
--wait_secrets Wait for secrets to be created (-W) (default 1)
Sysbench General Options:
--sysbench-general-options=<options>
Space or comma separated general Sysbench options
Sysbench FileIO Options:
--sysbench-fileio-tests=<modes>
Space of comma separated list of file test modes
to use (seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw)
If multiple modes are specified, modes are
spread around.
--sysbench-fileio-options=<options>
Space or comma separated fileio Sysbench options
EOF
}
function _helpEverything() {
_helpExtended
cat <<'EOF'
Clusterbuster is a tool to permit you to load a configurable workload
onto an OpenShift cluster. This is not a full-featured workload
generator like clusterloader; it generates very specific types of load
that for the most part focus on API objects.
Clusterbuster allows you to create three kinds of pods (via
deployments). The type of pod is selected by the -P option.
- A simple pause-type implementation written in perl that emits a log
heartbeat once a minute.
- Pause pod (uses the least resources). Useful for testing the control
plane, but not the data plane.
- A client-server workload, with optional bidirectional data transfer
at a specified data rate. Allows load testing of the data plane.
All three types allow you to configure the following load parameters:
- Number of namespaces created (-N)
- Number of deployments per namespace (-d)
- Number of replicas per deployment (-r)
- Number of containers per replica (-c)
- Number of secrets per deployment (-s)
The total number of pods created is (N * d * r) and the total number
of containers is (N * d * r * c). The number of secrets is (N * d).
This is useful for testing of scaling of both nodes and apiserver/etcd
combination on the control plane.
The client-server workload consists of one server pod, with one
container, per deployment. The clients and server can optionally
engage in ping-pong data transfer. The number of client pods is
controlled by the number of replicas. Therefore, each server will
service (r * c) connections. Each container will attempt to transmit
up to the specified data rate (-D) megabytes/second, and then receive
the amount of data. The amount of data to be sent can be specified
via -M, in megabytes; the size of each message can be specified via
-m, in bytes. By default, no data is sent. The amount of time to
perform data transfer can be specified by -t (default is none).
Via -x and -X it is possible to specify whether clients will be
colocated with their server (-x) or forced onto different nodes (-X).
If neither option is specified, clusterbuster allows OpenShift to
place pods as it sees fit. You can use `oc logs` on a client pod (or
use Prometheus metrics) to get data transfer information; with -v,
verbose information about each transfer is printed.
The client logs the following at the end of any data transfer:
STATS <atime> <stime> <gtime> <etime> <endtime> <user> <sys> <interval> <data> <detime> <rate> <mean> <stdev>
defined as:
atime Time (seconds from start of run) of "oc apply" to
create the client
stime Time when the client started running
gtime Time when "gethostbyname" completed
etime Time when connection to server succeeded
endtime Time when client completed
user User CPU time
user System CPU time
interval Duration between first attempt to connect and
successful connection
data Number of data bytes sent and received
detime Elapsed time for data transmission/reception
rate Rate (MB/sec) of data transfer, if any
mean Mean time sleeping between messages to attempt to
achieve the desired transmission rate
stdev Standard deviation of sleep interval between messages
You can use `clusterbuster-connstat` to extract client information about a
running or completed test run, including the above data. Running
`clusterbuster -v` allows `clusterbuster-connstat` to extract data about
transfers completed thus far.
You can run multiple concurrent clusterbusters by choosing different
basenames for each instance (-B). All objects created by that run
will be tagged with the basename, in addition to 'clusterbusterbase'
(to allow you to easily find or delete all objects created by
clusterbuster).
Examples:
- You have a 30 node cluster and wish to test 500 pods per node, all
in one namespace:
clusterbuster -d 15000
- You wish to use 1000 namespaces for the same test:
clusterbuster -N 1000 -d 15
- You wish to add 150,000 secrets to further stress the apiserver and
etcd:
clusterbuster -N 1000 -d 15 -s 10
- You wish to exchange data at 10 MB/sec with a total of 2 GB per client,
with 5 deployments in 2 namespaces each of which uses 4 replica clients
and 3 containers. You wish to place the clients on nodes other than the
servers:
clusterbuster -P server -N 2 -d 5 -r 4 -c 3 -X -v
EOF
}
function help() {
_helpmsg "$@" | "${PAGER:-more}"
exit 1
}
function help_extended() {
_helpExtended | "${PAGER:-more}"
exit 1
}
function help_everything() {
_helpEverything | "${PAGER:-more}"
exit 1
}
function ts() {
local dt
dt=$(date '+%s.%N')
local sec=${dt%.*}
local ns=${dt#*.}
echo "${sec}.${ns:0:6}"
}
function timestamp() {
while read -r 'LINE' ; do
printf "%s %s\n" "$(TZ=GMT-0 date '+%Y-%m-%dT%T.%N' | cut -c1-26)" "$LINE"
done
}
trap exit INT
function find_free_port_perl() {
cat <<'EOF'
#!/usr/bin/perl
use Socket;
use POSIX;
use strict;
use Getopt::Long;
Getopt::Long::Configure("bundling", "pass_through");
my ($port) = 4097;
GetOptions("p=i" => \$port);
while ($port < 65535) {
my $sockaddr = "S n a4 x8";
my ($socket);
socket($socket, AF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "socket: $!";
setsockopt($socket,SOL_SOCKET, SO_REUSEADDR, pack("l",1)) || die "setsockopt reuseaddr: $!\n";
setsockopt($socket,SOL_SOCKET, SO_KEEPALIVE, pack("l",1)) || die "setsockopt keepalive: $!\n";
if (bind($socket, pack($sockaddr, AF_INET, $port, "\0\0\0\0"))) {
print "$port\n";
exit 0;
}
$port++;
}
die "Can't find free port!\n";
EOF
}
function find_free_port() {
perl -e "$(find_free_port_perl)"
}
function get_logs_perl() {
cat <<'EOF'
#!/usr/bin/perl
use Socket;
use POSIX;
use strict;
use Getopt::Long;
Getopt::Long::Configure("bundling", "pass_through");
my ($timeout) = -1;
my ($port) = 4096;
my ($expect_count) = -1;
GetOptions("t=i" => \$timeout,
"p=i" => \$port,
"c=i" => \$expect_count);
my $sockaddr = "S n a4 x8";
my ($socket);
socket($socket, AF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "socket: $!";
setsockopt($socket,SOL_SOCKET, SO_REUSEADDR, pack("l",1)) || die "setsockopt reuseaddr: $!\n";
setsockopt($socket,SOL_SOCKET, SO_KEEPALIVE, pack("l",1)) || die "setsockopt keepalive: $!\n";
if (!bind($socket, pack($sockaddr, AF_INET, $port, "\0\0\0\0"))) {
die "Can't bind to port $port\n";
}
listen($socket, 100) || die "Can't listen on port $port\n";
my ($client_count) = 0;
eval {
if ($timeout > 0) {
alarm $timeout;
}
my $kid;
while ($expect_count < 0 || $client_count < $expect_count) {
my $child = fork;
if ($child == 0) {
my ($client);
accept ($client, $socket) || next;
close ($socket);
my ($tbuf) = "";
my ($status);
if (($status = sysread($client, $tbuf, 1024)) > 0) {
print "$tbuf\n";
}
exit(0);
}
do {
$kid = waitpid(-1, WNOHANG);
} while ($kid > 0);
$client_count++;
}
do {
$kid = wait();
} while ($kid > 0);
};
if ($@) {
die "$@";
}
EOF
}
function get_logs() {
local varg=
((verbose_report)) && varg=-v
exec perl -e "$(get_logs_perl)" -- "$@" | "${pathdir:-.}/clusterbuster-connstat" -s -f - "$varg"
}
function set_xfer() {
local sizespec=$1
local -i scale=${2:-1}
if [[ $sizespec = *','* ]] ; then
bytes=$((${sizespec#*,} * scale))
bytesMax=$((${sizespec%%,*} * scale))
if (( bytes > bytesMax )) ; then
local -i tmp=$bytes
bytes=$bytesMax
bytesMax=$tmp
fi
else
bytes=$((sizespec * scale))
bytesMax=$((sizespec * scale))
fi
}
function set_xfer_time() {
local timespec=$1
if [[ $timespec = *','* ]] ; then
xferTime=${timespec#*,}
xferTimeMax=${timespec%%,*}
if (( xferTime > xferTimeMax )) ; then
local -i tmp=$xferTime
xferTime=$xferTimeMax
xferTimeMax=$tmp
fi
else
xferTime=$timespec
xferTimeMax=$timespec
fi
}
function process_option() {
local option=$1
local optname
local optvalue
optname=${option%%=*}
optname=${optname,,}
optvalue=${option#*=}
noptname=${optname//-/_}
if [[ $option != *'='* ]] ; then
if [[ $optname = "no_"* || $optname = "dont_"* ]] ; then
optname=${optname#dont_}
optname=${optname#no_}
optvalue=0
else
optvalue=1
fi
fi
# shellcheck disable=SC2206
case "$noptname" in
help_all*) help_extended ;;
help_everything*) help_everything ;;
help*) help ;;
pod_type) podType=$optvalue ;;
basename) basename=$optvalue ;;
blocksize) blocksize=$optvalue ;;
containers) containers=$optvalue ;;
namespaces) namespaces=$optvalue ;;
datarate) dataRate=$optvalue ;;
bytes) set_xfer "$optvalue" ;;
kb|kilobytes) set_xfer "$optvalue" 1000 ;;
kib|kibibytes) set_xfer "$optvalue" 1024 ;;
mb|megabytes) set_xfer "$optvalue" 1000000 ;;
mib|mebibytes) set_xfer "$optvalue" 1048576 ;;
gb|gigabytes) set_xfer "$optvalue" 1000000000 ;;
gib|gibibytes) set_xfer "$optvalue" 1073741824 ;;
msgsize) msgSize=$optvalue ;;
secrets) secrets=$optvalue ;;
verbose) verbose=$optvalue ;;
quiet) verbose=$((!optvalue)) ;;
replicas) replicas=$optvalue ;;
parallel) parallel=$optvalue ;;
sleep) sleeptime=$optvalue ;;
podsleep) podsleeptime=$optvalue ;;
poddelay) poddelay=$optvalue ;;
transfertime) set_xfer_time "$optvalue" ;;
first_deployment) firstDeployment=$optvalue ;;
parallel_secrets) parallelSecrets=$optvalue ;;
parallel_namespaces) parallelNamespaces=$optvalue ;;
parallel_deployments) parallelDeployments=$optvalue ;;
blocksize_secrets) blocksizeSecrets=$optvalue ;;
blocksize_namespaces) blocksizeNamespaces=$optvalue ;;
blocksize_deployments) blocksizeDeployments=$optvalue ;;
sleep_secrets) sleepSecrets=$optvalue ;;
sleep_namespaces) sleepNamespaces=$optvalue ;;
sleep_deployments) sleepDeployments=$optvalue ;;
exit_at_end) exitAtEnd=$optvalue ;;
pod_resource_request) pod_resource_requests+=("$optvalue") ;;
pod_resource_limit) pod_resource_limits+=("$optvalue") ;;
container_resource_request) container_resource_requests+=("$optvalue") ;;
container_resource_limit) container_resource_limits+=("$optvalue") ;;
create_routes) createRoutes=$optvalue ;;
wait_secrets) wait_for_secrets=$optvalue ;;
scale_ns) scale_ns=$optvalue ;;
scale_deployments) scale_deployments=$optvalue ;;
deployment_type) deploymentType=$optvalue ;;
runtimeclass|runtime_class) runtimeClass=$optvalue ;;
report_object_creation) reportObjectCreation=$optvalue ;;
kata) runtimeClass=kata ;;
report) report=$optvalue ;;
cleanup) cleanup=$optvalue ;;
timeout) timeout=$optvalue ;;
verbose_report) verbose_report=$optvalue ;;
emptydir) emptydirs+=("$optvalue") ;;
sync|syncstart|sync_start) syncStart=$((1-syncStart)) ;;
sysbench_general_options) sysbench_general_options+=(${optvalue//,/ });;
sysbench_fileio_options) sysbench_fileio_options+=(${optvalue//,/ }) ;;
sysbench_fileio_tests)
local ftest
for ftest in ${optvalue//,/ } ; do
sysbench_fileio_tests[$ftest]=1
done
;;
affinity)
case "$optvalue" in
1|'') affinity=1 ;;
2|anti) affinity=2 ;;
*) affinity=0 ;;
esac
;;
antiaffinity|anti_affinity)
case "$optvalue" in
1|'') affinity=2 ;;
*) affinity=0 ;;
esac
;;
*) unknown_opts+=("$noptname") ;;
esac
if [[ $deploymentType != pod && $deploymentType != deployment ]] ; then
echo "-T/--deployment_type must be either por or deployment"
help
fi
}
while getopts ":B:b:c:D:d:EeHhM:m:N:nP:p:Qqr:Ss:T:t:vWw:XxYz:-:" opt ; do
case "$opt" in
B) basename="$OPTARG" ;;
D) dataRate="$OPTARG" ;;
E) exitAtEnd=0 ;;
e) exitAtEnd=1 ;;
h) help ;;
H) help_extended ;;
M) set_xfer "$OPTARG" 1000000 ;;
N) namespaces="$OPTARG" ;;
P) podType="$OPTARG" ;;
X) affinity=2 ;;
b) blocksize="$OPTARG" ;;
c) containers="$OPTARG" ;;
d) depsPerNamespace="$OPTARG" ;;
m) msgSize="$OPTARG" ;;
n) doit=0 ;;
p) parallel="$OPTARG" ;;
Q) reportObjectCreation=0 ;;
q) verbose=0 ;;
r) replicas="$OPTARG" ;;
S) scale_ns=1 ;;
s) secrets="$OPTARG" ;;
T) deploymentType="$OPTARG" ;;
t) set_xfer_time "$OPTARG" ;;
v) verbose=1 ;;
W) wait_for_secrets=0 ;;
w) sleeptime="$OPTARG" ;;
x) affinity=1 ;;
Y) syncStart=$((1-syncStart)) ;;
z) podsleeptime="$OPTARG" ;;
-) process_option "$OPTARG" ;;
*) help "$OPTARG" ;;
esac
done
[[ -n "${unknown_opts[*]}" ]] && help "${unknown_opts[@]}"
(( !parallelSecrets )) && parallelSecrets=$parallel
(( !parallelNamespaces )) && parallelNamespaces=$parallel
(( !parallelDeployments )) && parallelDeployments=$parallel
(( !blocksizeSecrets )) && blocksizeSecrets=$blocksize
(( !blocksizeNamespaces )) && blocksizeNamespaces=$blocksize
(( !blocksizeDeployments )) && blocksizeDeployments=$blocksize
[[ -n $runtimeClass ]] && runtimeClass="runtimeClassName: \"$runtimeClass\""
shift $((OPTIND - 1))
if [[ -z $OC && $doit -gt 0 ]] ; then
echo "Cannot find oc or kubectl command, exiting!"
exit 1
fi
if (( msgSize <= 0 )) ; then
echo "Message size must be positive, exiting!"
exit 1
fi
if [[ $dataRate != 0 && $dataRate != '' && $xferTimeMax -eq 0 && bytesMax -eq 0 ]] ; then
bytes=$defaultBytes
bytesMax=$defaultBytes
fi
case "$podType" in
classic|ClusterBuster) podType=ClusterBuster ;;
server) podType=Server ;;
cpu*|CPU*|soaker) podType=CPUSoaker ;;
sysbench|Sysbench) podType=Sysbench ;;
pause|PausePod|*) podType=PausePod ;;
esac
if [[ -n ${1:-} ]] ; then
basename="$1"
shift
fi
[[ -z "$*" ]] || echo "Warning: extraneous arguments $# after basename will be ignored!" 1>&2
function indent_1() {
local -i column="$1"
local line
while IFS='' read -r 'line' ; do
[[ -z "$line" ]] || printf "%${column}s%s\n" ' ' "$line"
done
}
function indent() {
local -i column="$1"
shift
if [[ -n "$*" ]] ; then
# "$@" | indent_1 "$column" strips whitespace with bash 4.2
indent_1 "$column" <<< "$("$@")"
else
indent_1 "$column"
fi
}
function finishCreation() {
if [[ $doit -ne 0 && -n $accumulateddata ]] ; then
"${OC}" apply -f - <<< "$accumulateddata" | timestamp
accumulateddata=
fi
}
function createResources() {
local token
for token in "$@" ; do
local resource=${token%%=*}
local value=${token#*=}
echo "$resource: $value"
done
}
function createPodResources() {
if (( ${#pod_resource_limits[@]} + ${#pod_resource_requests[@]} )) ; then
echo "resources:"
if (( ${#pod_resource_limits[@]} )) ; then
echo " limits:"
indent 4 createResources "${pod_resource_limits[@]}"
fi
if (( ${#pod_resource_requests[@]} )) ; then
echo " requests:"
indent 4 createResources "${pod_resource_requests[@]}"
fi
fi
}
function createContainerResources() {
if (( ${#container_resource_limits[@]} + ${#container_resource_requests[@]} )) ; then
echo "resources:"
if (( ${#container_resource_limits[@]} )) ; then
echo " limits:"
indent 4 createResources "${container_resource_limits[@]}"
fi
if (( ${#container_resource_requests[@]} )) ; then
echo " requests:"
indent 4 createResources "${container_resource_requests[@]}"
fi
fi
}
function createObject() {
local data=
local line=
while IFS='' read -r 'line' ; do
if [[ -n $line ]] ; then
[[ -n $data ]] || data="---$nl"
data+="$line$nl"
fi
done
data+="$nl"
if (( doit )) ; then
accumulateddata+="$data"
if (( ++blockitemcount >= blocksize )) ; then
"${OC}" apply -f - <<< "$accumulateddata" | timestamp
accumulateddata=
blockitemcount=0
(( !sleeptime )) || sleep "$sleeptime"
fi
else
echo "$data"
fi
}
function createNamespace() {
local namespace=$1
createObject <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: "${namespace}"
labels:
${basename}: "true"
clusterbusterbase: "true"
EOF
}
function createSecrets() {
local namespace=$1
local depsPerNamespace=${2:-1}
local secrets=${3:-1}
local -i i
local -i j
(( secrets )) || return;
for i in $(seq $firstDeployment $((depsPerNamespace + firstDeployment - 1))) ; do
for j in $(seq 0 $((secrets - 1))) ; do
createObject <<EOF
apiVersion: v1
kind: Secret
metadata:
name: "secret-${namespace}-${i}-${j}"
namespace: "$namespace"
labels:
${basename}: "true"
clusterbusterbase: "true"
data:
key1: "$(base64 <<< "${namespace}X${i}Y${j}Z1")"
key2: "$(base64 <<< "${namespace}X${i}Y${j}Z2")"
type: Opaque
EOF
done
done
}
function createVolumeMountsYAML() {
local namespace=$1
local deployment=${2:-1}
local secrets=${3:-1}
(( secrets + ${#emptydirs[@]})) || return;
echo "volumeMounts:"
local -i i
for i in $(seq 0 $((secrets - 1))) ; do
local name="secret-${namespace}-${deployment}-$i"
cat <<EOF
- name: $name
mountPath: /etc/$name
readOnly: true
EOF
done
if [[ -n "${emptydirs[*]}" ]] ; then
local vol
for vol in "${emptydirs[@]}" ; do
cat <<EOF
- name: ${vol##*/}
mountPath: "$vol"
EOF
done
fi
}
function createVolumesYAML() {
local namespace=$1
local deployment=${2:-1}
local secrets=${3:-1}
(( secrets + ${#emptydirs[@]})) || return;
local -i i
echo "volumes:"
for i in $(seq 0 $((secrets - 1))) ; do
local name="secret-${namespace}-${deployment}-$i"
cat<<EOF
- name: $name
secret:
secretName: $name
EOF
done
if [[ -n "${emptydirs[*]}" ]] ; then
local vol
for vol in "${emptydirs[@]}" ; do
cat <<EOF
- name: ${vol##*/}
emptydDir: {}
EOF
done
fi
}
function createContainersClusterBusterYAML() {
local namespace=$1
local i=$2
local secretCount=$3
local -i pods=$4
local -i containers=$5
local -i j
for j in $(seq 0 $((containers - 1))) ; do
cat <<EOF
- name: ${namespace}-$i-$j
imagePullPolicy: IfNotPresent
image: "busybox"
$(indent 2 createContainerResources)
command:
- /bin/sh
args:
- -c
- |
trap 'exit 0' TERM
(while : ; do date ; sleep 60 ; done)&
sleep $podsleeptime
exit 1
$(indent 2 createVolumeMountsYAML "$namespace" "$i" "$secretCount")
EOF
done
}
function createContainersPausePodYAML() {
local namespace=$1
local i=$2
local secretCount=$3
# shellcheck disable=SC2034
local -i pods=$4
local -i containers=$5
local -i j
for j in $(seq 0 $((containers - 1))) ; do
cat <<EOF
- name: ${namespace}-$i-$j
imagePullPolicy: IfNotPresent
image: "gcr.io/google_containers/pause-amd64:3.0"
$(indent 2 createContainerResources)
$(indent 2 createVolumeMountsYAML "$namespace" "$i" "$secretCount")
EOF
done
}
function createClassicSpec() {
local namespace=$1
local i=$2
local secretCount=$3
local -i replicas=$4
local -i containers=$5
cat <<EOF
spec:
nodeSelector:
node-role.kubernetes.io/worker: ""
terminationGracePeriodSeconds: 1
$(indent 2 createPodResources)
$(indent 2 <<< "$runtimeClass")
containers:
$(indent 2 createContainers${podType}YAML "$@")
$(indent 2 createVolumesYAML "$namespace" "$i" "$secretCount")
EOF
}
function createDeploymentClassicYAML() {
local namespace=$1
local i=$2
local secretCount=$3
local -i replicas=$4
local -i containers=$5
if [[ $deploymentType = pod ]] ; then
local -i j=0
while (( j < replicas )) ; do
createObject <<EOF
apiVersion: v1
kind: Pod
metadata:
name: ${namespace}-${i}-${j}
namespace: $namespace
nodeSelector:
node-role.kubernetes.io/worker: ""
selector:
matchLabels:
app: ${namespace}-${i}
labels:
${basename}: "true"
name: ${namespace}-${i}
app: ${namespace}-${i}
${basename}: "true"
${basename}-client: "true"
clusterbusterbase: "true"
$(createClassicSpec "$@")
EOF
j=$((j+1))
done
else
createObject <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${namespace}-$i
namespace: $namespace
labels:
${basename}: "true"
clusterbusterbase: "true"
spec:
replicas: $replicas
nodeSelector:
node-role.kubernetes.io/worker: ""
selector:
matchLabels:
app: ${namespace}-$i
strategy:
type: RollingUpdate