-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkprobe
executable file
·523 lines (443 loc) · 16.5 KB
/
kprobe
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
#!/bin/bash
version="0.60"
prog=${0##*/}
read -rd '' usage <<END || true
Usage: $prog [-h | --help] [-V | --version] [-D | --debug]
or: $prog help [ create | start | run | stop | delete ]
or: $prog create ( help | [-b|-B] [-m <memory>] [-c <cpu>] [-i <image>] <number> <yaml_file> )
or: $prog start [ help | <batch_number> | <list_file> ]
or: $prog run ( help | <workload> [ <batch_number> | <list_file> ] )
or: $prog stop [ help | <batch_number> | <list_file> ]
or: $prog delete [ help | <batch_number> | <list_file> ]
END
#-- define subcommands and options ---------------------------------------------
# using setopt library for option parsing and subcommand dispatch
read -rd '' setopt_define <<'END' || true
h help
V version
D debug
/help
/create
b batch
B no-batch
i image (.+)
m memory ([0-9]+[KkMmGgTt]i?)
c cpu ([0-9]+(,[0-9]+(,[0-9]+)?)?)
/start
/stop
/delete
/run
/run/fio
/run/uperf
/hcp
n name (.*)
r node-pool-replicas ([0-9]+)
d base-domain (.*)
s pull-secret (.*)
END
#-- constants ------------------------------------------------------------------
# namespace="$prog" # base name for the namespaces to use for batching
namespace="default" # base name for the namespaces to use for batching
podname="test" # base name for the numbered test VMs
batches=0 # default for whether to make separate batches, can override with -b or -B
tmpl_dir=./template/cnv # directory to find YAML templates for certain kinds of object
# mappings between override settings and their path within the kubernetes YAML file
declare -A path=(
[namespace]="metadata:namespace"
[name]="metadata:name"
[cores]="spec:template:spec:domain:cpu:cores"
[threads]="spec:template:spec:domain:cpu:threads"
[sockets]="spec:template:spec:domain:cpu:sockets"
[memory]="spec:template:spec:domain:resources:requests:memory"
[image]="spec:template:spec:volumes:0:dataVolume:name"
[root_tmp]="dataVolumeTemplates:0:metadata:name"
[data_tmp]="dataVolumeTemplates:1:metadata:name"
[root_volume]="spec:template:spec:volumes:0:dataVolume:name"
[data_volume]="spec:template:spec:volumes:1:dataVolume:name"
)
#-- help strings ---------------------------------------------------------------
declare -A help=()
read -rd '' help[main] <<END || true
This is $prog the kubenetes probe for streamline benchmark tasks and extrac
performance information.
$usage
Options:
-h --help dislplay this help
-V --version display version number
-D --debug enable debug information
The following subcommands are available:
create create a new batch of test instances
start start one or more batches of kubevirt instances
run run a test workload on running instances
stop stop one or more batches of kubevirt instances
delete delete one or more batches of test instances
help shows help on the given topic, which includes
the above subcommands and also:
help override
For more help on each subcommand you can use:
$prog <subcmd> help
or:
$prog help <topic>
END
read -rd '' help[override] <<END || true
How to use override yaml files.
The $prog create subcommand reads a yaml file as the second argument. This
can be a kubernetes yaml file, or it can be a much smaller and simpler
override yaml file. The override yaml file must contain the key "override:"
with the name of a kubernetes yaml file stored in the templates directory.
The templates dir is currently configured to be at:
$tmpl_dir
The other settings in the override yaml file will override the settings in
the template. For example:
---
override: vm
memory: 2G
cpu:
cores: 4
threads: 2
sockets: 1
image: rhel9
If you are using an override yaml file but also have create options on the
command line to override the same settings, then the command line options
take precedence over the settings in an override yaml. This mechanism
allows you to use kubernetes yaml directly, or just put it in the templates
dir and create override yaml files for the different configurations, and
you can still override the override yaml using command line options.
END
read -rd '' help[create] <<END || true
create subcommand -- Creates a batch of new instances.
Usage: $prog create [options] <batch_size> <yaml_template>
Options:
-b --batch use a numbered namespace for this batch
-B --no-batch do not use a numbered namespace for this batch
-i --image <name> override the VM disk image with this one
-m --memory <ram> override the VM memory size with this value
-c --cpu <cores> override the VM CPU corees with this number
can also say --cpu 2,4 to set cores,threads
or even --cpu 2,4,1 to set cores,threads,sockets
Arguments:
<batch_size> is the number of instances to create in this batch
<yaml_template> is the filename of the kubernetes YAML file to use
See also: help overrides
END
read -rd '' help[start] <<END || true
start subcommand -- Starts one or more batches of kubevirt instances.
Usage: $prog start
or: $prog start <batch_number>
or: $prog start <list_file>
Arguments:
<batch_number> to start the VMs of just one batch
<list_file> to start specific VMs listed in the file
Without arguments it will start all the created but not yet started VMs.
END
read -rd '' help[run] <<END || true
run subcommand -- Runs a test workload on the running instances.
Usage: $prog run <workload>
or: $prog run <workload> <batch_number>
or: $prog run <workload> <list_file>
Arguments:
<workload> name of the test workload to run
<batch_number> to start the VMs of just one batch
<list_file> to start specific VMs listed in the file
Workloads:
fio for file IO tests
uperf for network performance tests
The workload argument is required. If no batch number or list file is
given then it will run the workload on all the running instances.
END
read -rd '' help[stop] <<END || true
stop subcommand -- Stops one or more batches of kubevirt instances.
Usage: $prog stop
or: $prog stop <batch_number>
or: $prog stop <list_file>
Arguments:
<batch_number> to stop the VMs of just one batch
<list_file> to stop specific VMs listed in the file
Without arguments it will stop all the currently running VMs.
END
read -rd '' help[delete] <<END || true
delete subcommand -- Deletes one or more batches of test instances.
Usage: $prog delete
or: $prog delete <batch_number>
or: $prog delete <list_file>
Arguments:
<batch_number> to delete the instances of just one batch
<list_file> to delete specific instances listed in the file
Without arguments it will delete all the $prog test instances.
END
#-- functions ------------------------------------------------------------------
die() {
[[ -t 2 ]] && printf '\e[31;1m'
printf '%s\n' "$1" >&2
[[ -t 2 ]] && printf '\e[m'
(($#>1)) && printf '%s\n' "${@:2}" >&2
exit 1
}
help() {
if ((!$#)); then
printf '\n%s\n\n' "${help[main]}"
elif [[ -n ${help[$1]} ]]; then
printf '\n%s\n\n' "${help[$1]}"
else
printf 'Sorry, there is no help for that topic\n'
fi
}
yaml_get() {
local path=$1 file=$2 pycode
read -rd '' pycode <<'END'
import sys, yaml
data = yaml.safe_load(sys.stdin)
for k in sys.argv[1].split(":"):
if k.isdigit() and type(data) == list:
data = data[int(k)]
elif k in data:
data = data[k]
else:
data = ''
break
print(data)
END
if [[ -n $file ]]; then
python3 -c "$pycode" "$path" < "$file"
else
python3 -c "$pycode" "$path"
fi
}
yaml_set() {
local path=$1 value=$2 file=$3 pycode
read -rd '' pycode <<'END'
import sys, yaml
data = yaml.safe_load(sys.stdin)
a, j, b = None, None, data
for k in sys.argv[1].split(":"):
if k.isdigit():
k = int(k)
if type(b) != list:
b = []
if a is None:
data = b
else:
a[j] = b
if k >= len(b):
b.extend([ None ] * (k - len(b) + 1))
else:
if type(b) != dict:
b = {}
if a is None:
data = b
else:
a[j] = b
if k not in b:
b[k] = {}
if type(b[k]) not in (dict, list):
b[k] = {}
a, j = b, k
b = b[k]
val = sys.argv[2]
if val.isdigit():
a[j] = int(val)
elif len(val.strip('0123456789')) < 2 and val.strip('0123456789.') == '':
a[j] = float(val)
else:
a[j] = val
print(yaml.safe_dump(data))
END
if [[ -n $file ]]; then
python3 -c "$pycode" "$path" "$value" < "$file"
else
python3 -c "$pycode" "$path" "$value"
fi
}
hcp() {
local hcp_yaml=$1
if [[ -f $hcp_yaml ]]; then
[[ -n ${opt[name]} ]] || opt[name]=$(yaml_get name "$hcp_yaml")
[[ -n ${opt[node-pool-replicas]} ]] || opt[node-pool-replicas]=$(yaml_get node-pool-replicas "$hcp_yaml")
[[ -n ${opt[base-domain]} ]] || opt[base-domain]=$(yaml_get base-domain "$hcp_yaml")
[[ -n ${opt[pull-secret]} ]] || opt[pull-secret]=$(yaml_get pull-secret "$hcp_yaml")
fi
for k in "${!opt[@]}"; do
printf '%s -> %s\n' "$k" "${opt[$k]}"
done
}
create() {
if [[ $1 == help ]] || ((opt[help])); then
printf '\n%s\n\n' "${help[create]}"
elif (($#!=2)); then
die "Incorrect number of arguments to create" "$usage"
else
local how_many=$1 yaml_file=$2 override
[[ $how_many =~ ^[0-9]+ ]] || die "Create object count must be an integer" "$usage"
[[ -f $yaml_file ]] || die "Create could not find file: $yaml_file"
((opt[no-batch])) && batches=0
((opt[batch])) && batches=1
if [[ -n ${opt[cpu]} ]]; then
local cores threads sockets
IFS=, read cores threads sockets <<<"${opt[cpu]}"
[[ -n $cores ]] && opt[cores]=$cores
[[ -n $threads ]] && opt[threads]=$threads
[[ -n $sockets ]] && opt[sockets]=$sockets
fi
override=$(yaml_get override "$yaml_file")
if [[ -n $override ]]; then
[[ -n ${opt[memory]} ]] || opt[memory]=$(yaml_get memory "$yaml_file")
[[ -n ${opt[image]} ]] || opt[image]=$(yaml_get image "$yaml_file")
[[ -n ${opt[cores]} ]] || opt[cores]=$(yaml_get cpu:cores "$yaml_file")
[[ -n ${opt[threads]} ]] || opt[threads]=$(yaml_get cpu:threads "$yaml_file")
[[ -n ${opt[sockets]} ]] || opt[sockets]=$(yaml_get cpu:sockets "$yaml_file")
if [[ -f "$tmpl_dir/$override.yaml" ]]; then
yaml_file="$tmpl_dir/$override.yaml"
elif [[ -f "$tmpl_dir/$override" ]]; then
yaml_file="$tmpl_dir/$override"
elif [[ -f "$override.yaml" ]]; then
yaml_file="$override.yaml"
elif [[ -f "$override" ]]; then
yaml_file="$override"
else
die "Cannot find yaml template $override"
fi
fi
yaml=$(<"$yaml_file")
yaml=$(yaml_set "${path[namespace]}" "$namespace" <<<"$yaml")
[[ -n ${opt[memory]} ]] && yaml=$(yaml_set "${path[memory]}" "${opt[memory]}" <<<"$yaml")
[[ -n ${opt[image]} ]] && yaml=$(yaml_set "${path[image]}" "${opt[image]}" <<<"$yaml")
[[ -n ${opt[cores]} ]] && yaml=$(yaml_set "${path[cores]}" "${opt[cores]}" <<<"$yaml")
[[ -n ${opt[threads]} ]] && yaml=$(yaml_set "${path[threads]}" "${opt[threads]}" <<<"$yaml")
[[ -n ${opt[sockets]} ]] && yaml=$(yaml_set "${path[sockets]}" "${opt[sockets]}" <<<"$yaml")
local batch_num vm_num
if ((batches)); then
die "batching is not yet supported"
# batch_num=$(
# oc get ns --no-headers -o custom-columns=NAME:.metadata.name \
# | sed -nE 's/^.*\<'"$namespace"'([0-9]+).*$/\1/p' | sort -n | tail -1
# )
((batch_num++))
vm_num=1
else
vm_num=$(
oc get vms --no-headers -o custom-columns=NAME:.metadata.name \
| sed -nE 's/^.*\<'"$podname"'([0-9]+).*$/\1/p' | sort -n | tail -1
)
((vm_num++))
batch_num=""
fi
local n max=$((vm_num+how_many)) ns name
ns="$namespace$batch_num"
((batches)) || yaml=$(yaml_set "${path[namespace]}" "$ns" <<<"$yaml")
for ((n=vm_num; n<max; n++)); do
name="$podname$n"
printf 'Creating %s/%s\n' "$ns" "$name"
yaml=$(yaml_set "${path[name]}" "$name" <<<"$yaml")
# send the updated yaml to oc -f - to create it
# printf '\n%s\n\n' "$yaml" # for example
oc create -f - <<< "$yaml"
done
fi
}
start() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[start]}"
elif ((!$#)); then # no args means do all
printf 'Starting all vms\n'
elif [[ $1 =~ ^[0-9]+$ ]]; then # just a batch number
printf 'Starting vms in namespace %s\n' "$namespace$1"
elif [[ -f $1 ]]; then # a file with list of vms
local line
while read -r line; do
((${#line})) && printf 'Starting %s\n' "$line"
done < "$1"
else
printf 'Cannot read file %s\n' "$1"
fi
}
stop() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[stop]}"
elif ((!$#)); then # no args means do all
printf 'Stopping all vms\n'
elif [[ $1 =~ ^[0-9]+$ ]]; then # just a batch number
printf 'Stopping vms in namespace %s\n' "$namespace$1"
elif [[ -f $1 ]]; then # a file with list of vms
local line
while read -r line; do
((${#line})) && printf 'Stopping %s\n' "$line"
done < "$1"
else
printf 'Cannot read file %s\n' "$1"
fi
}
delete() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[delete]}"
elif ((!$#)); then # no args means do all
printf 'Deleting all pods/vms\n'
elif [[ $1 =~ ^[0-9]+$ ]]; then # just a batch number
printf 'Deleting pods/vms in namespace %s\n' "$namespace$1"
elif [[ -f $1 ]]; then # a file with list of pods
local line
while read -r line; do
((${#line})) && printf 'Deleting %s\n' "$line"
done < "$1"
else
printf 'Cannot read file %s\n' "$1"
fi
}
run_fio() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[run]}"
elif ((!$#)); then # no args means do all
printf 'Running fio workload on all pods/vms\n'
elif [[ $1 =~ ^[0-9]+$ ]]; then # just a batch number
printf 'Running fio workload on pods/vms in namespace %s\n' "$namespace$1"
elif [[ -f $1 ]]; then # a file with list of pods
local line
while read -r line; do
((${#line})) && printf 'Running fio workload on %s\n' "$line"
done < "$1"
else
printf 'Cannot read file %s\n' "$1"
fi
}
run_uperf() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[run]}"
elif ((!$#)); then # no args means do all
printf 'Running uperf workload on all pods/vms\n'
elif [[ $1 =~ ^[0-9]+$ ]]; then # just a batch number
printf 'Running uperf workload on pods/vms in namespace %s\n' "$namespace$1"
elif [[ -f $1 ]]; then # a file with list of pods
local line
while read -r line; do
((${#line})) && printf 'Running uperf workload on %s\n' "$line"
done < "$1"
else
printf 'Cannot read file %s\n' "$1"
fi
}
run() {
if [[ $1 == help ]]; then
printf '\n%s\n\n' "${help[run]}"
elif [[ $1 =~ ^(fio|uperf)$ ]]; then
setopt "$@"
elif (($#)); then
die "Unrecognised workload $1"
else
die "Workload to run is required"
fi
}
main() {
if ((opt[version])) || ((opt[help])); then
if ((opt[help])); then
printf '\n%s\n\n' "${help[main]}"
fi
((opt[version])) && printf 'Version %s\n' "$version"
exit
elif ((!$#)); then
die "This program expects a subcommand. See: $prog help"
else
setopt "$@"
fi
}
#-- main program ---------------------------------------------------------------
PATH="./lib/" source "setopt.bash" || die "Cannot find the setopt library"
setopt main "$@"