-
Notifications
You must be signed in to change notification settings - Fork 1
/
kdbench
executable file
·279 lines (255 loc) · 7.87 KB
/
kdbench
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
#!/usr/bin/env bash
# kdbench(1) is a utility to test storageclass performance easily
# Copyright 2022 SysNet4Admin (Hoon Jo)
#
# 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.
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
SELF_CMD="$0"
SCRACHFILE=~/.kdbench/kdbench.scra
YAMLFILE=~/.kdbench/kdbench.yaml
GLOBAL_STORAGE_SIZE="100" # unit Gi
GLOBAL_File_NAME="kdbench.yaml"
exit_oky(){
exit 0
}
exit_err() {
exit 1
}
usage(){
cat <<EOF
USAGE:
kdbench : Select storageclass with fzf
kdbench <STORAGECLASS NAME> : Select storageclass manually (w/o fzf)
kdbench -q, --quick : Quick run mode (w/ fzf)
kdbench -d, --direct : Direct fio write mode (w/ fzf)
kdbench -a <rwo,rox,rwx,rwop> : Set access mode (w/ fzf)
kdbench -s <storage size> : Set stroage size (w/ fzf)
kdbench -f <YAML FILE> : Run as a file (w/ fzf)
kdbench reset : Reset all of resources
kdbench -h, --help : Show this message
EOF
}
delete_pvc_after_job_done() {
echo -n "Waiting for job finished by ttlSecondsAfterFinished..."
while true; do
if [[ "$(kubectl get job -l fio=kdbench 2>&1)" = *"No resources"* ]]; then
echo -e "\nkdbench finsihed and pvc will delete"
kubectl delete pvc -l fio=kdbench
exit_oky
else
# print dot for waiting status
echo -n ".";sleep 1
fi
done
}
run_kdbench(){
# deploy kdbench
if [[ "$#" -eq 1 ]]; then
:
if [[ "${1}" == 'file' ]]; then
kubectl apply -f "${GLOBAL_File_NAME}"
else
echo -e "error: check Yaml file name"
exit_err
fi
else
# mostly run this command
kubectl apply -f "$YAMLFILE"
fi
# wait message for dbench
echo -n "Waiting for kdbench's load (up to 5mins)..."
local count=0
while true; do
PODSTATUS=$(kubectl get po -l job-name=kdbench 2>&1)
if [[ "$PODSTATUS" = *"Creating"* ]] || [[ "$PODSTATUS" = *"Pending"* ]]; then
echo -n ".";sleep 1; ((count=count+1))
if [[ $count -gt 300 ]]; then
echo -e "\n\n********* Error: Events as below *********"
$KUBECTL describe pvc kdbench-pv-claim | grep Message -A 50
exit_err
fi
elif [[ "$PODSTATUS" = *"Running"* ]]; then
break
else
echo "Pod is abnormal status"
fi
done
# kdbench result print out on stdout
kubectl logs -f job/kdbench
}
get_storageclasses() {
$KUBECTL get storageclasses -o=name | cut -c 29-
}
set_storageclass() {
sed -i'' -e "s|your-storageclass|${1}|g" "$YAMLFILE"
}
choose_n_run_storageclasses_interactive() {
local choice
choice="$(FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --ansi --no-preview || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit_err
else
# create yaml file
create_yaml_file
# change mode (optional)
if [[ "$#" -eq 1 ]]; then
if [[ "${1}" == 'ReadOnlyMany' || "${1}" == 'ReadWriteMany' || "${1}" == 'ReadWriteOncePod' ]]; then
change_access_mode $1
elif [[ "${1}" == 'quick' ]]; then
apply_quick_mode
elif [[ "${1}" == 'direct' ]]; then
apply_fio_direct
elif [[ "${1}" == 'storage' ]]; then
change_storage_size "${GLOBAL_STORAGE_SIZE}"
fi
fi
# change storageclass name from your-storageclass to $choice
set_storageclass "${choice}"
# actual run kdbench and show up to stdout
run_kdbench
# delete all objects for convenience purpose
delete_pvc_after_job_done
fi
}
choose_n_run_storageclasses_direct() {
# file mode (optional)
if [[ "${1}" == 'file' ]]; then
run_kdbench $1
delete_pvc_after_job_done
exit 0
fi
# normal direct mode
local choice
choice="$1"
create_yaml_file
# change storageclass name from your-storageclass to $1
set_storageclass "${choice}"
# actual run kdbench and show up to stdout
run_kdbench
# delete all objects for convenience purpose
delete_pvc_after_job_done
}
apply_quick_mode() {
sed -i'' -e "s|# - name: KDBENCH_QUICK|- name: KDBENCH_QUICK|g" "$YAMLFILE"
sed -i'' -e 's|# value: "yes"| value: "yes"|g' "$YAMLFILE"
}
apply_fio_direct() {
sed -i'' -e "s|# - name: FIO_DIRECT|- name: FIO_DIRECT|g" "$YAMLFILE"
sed -i'' -e 's|# value: "1"| value: "1"|g' "$YAMLFILE"
}
change_access_mode() {
sed -i'' -e "s|ReadWriteOnce|$1|g" "$YAMLFILE"
}
change_storage_size() {
sed -i'' -e "s|storage: 100|storage: $1|g" "$YAMLFILE"
}
create_yaml_file(){
cp -r "$SCRACHFILE" "$YAMLFILE"
}
validate_kubectl() {
if [[ -z "${KUBECTL:-}" ]]; then
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit_err
fi
fi
}
run_storageclasses_interactive(){
if [[ -t 1 && -z "${KDBENCH_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
if [[ "$#" -eq 0 ]]; then
choose_n_run_storageclasses_interactive
elif [[ "$#" -eq 1 ]]; then
choose_n_run_storageclasses_interactive "${1}"
else
:
fi
else
get_storageclasses
fi
}
main() {
validate_kubectl
# basic_run_interative
if [[ "$#" -eq 0 ]]; then
run_storageclasses_interactive
# quick_mode_on
elif [[ "${1}" == '-q' || "${1}" == '--quick' ]]; then
run_storageclasses_interactive "quick"
# write_fio_direct_on
elif [[ "${1}" == '-d' || "${1}" == '--direct' ]]; then
run_storageclasses_interactive "direct"
# reset to init stage
elif [[ "${1}" == 'reset' ]]; then
rm -f $YAMLFILE $YAMLFILE-e
$KUBECTL delete -f $SCRACHFILE 2>/dev/null || echo "Delete partly kdbench resource completely"
$KUBECTL get po,pv,pvc -l fio=kdbench
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" == '-a' ]]; then
if [[ "${2}" == 'RWO' || "${2}" == 'rwo' ]]; then
:
elif [[ "${2}" == 'ROX' || "${2}" == 'rox' ]]; then
run_storageclasses_interactive "ReadOnlyMany"
elif [[ "${2}" == 'RWX' || "${2}" == 'rwx' ]]; then
run_storageclasses_interactive "ReadWriteMany"
elif [[ "${2}" == 'RWOP' || "${2}" == 'rwop' ]]; then
run_storageclasses_interactive "ReadWriteOncePod"
else
echo "error: check access_mode in RWO,ROX,RWX,RWOP"
fi
# storage size
elif [[ "${1}" == '-s' ]]; then
if [[ "${2}" =~ ^-?[0-9]+$ ]]; then
GLOBAL_STORAGE_SIZE="${2}"
run_storageclasses_interactive "storage"
else
echo "error: check storage class size number" >&2
exit_err
fi
# From Yaml file
elif [[ "${1}" == '-f' ]]; then
if [[ "$#" -eq 2 ]]; then
GLOBAL_File_NAME="${2}"
choose_n_run_storageclasses_direct "file"
else
echo "error: check Yaml file name" >&2
exit_err
fi
# run_by_storage_name_manually
elif [[ "$#" -eq 1 ]]; then
STORAGECLASSESLIST=$(kubectl get storageclasses -o=name | cut -c 29- 2>&1)
if printf '%s\n' "${STORAGECLASSESLIST[@]}" | grep -q -F -x "$1" ; then
choose_n_run_storageclasses_direct $1
else
echo "error: check storageclass name or flag option" >&2
exit_err
fi
elif [[ "$#" -gt 2 ]]; then
echo "error: too many arguments" >&2
usage
exit_err
else
echo "error: unrecognized flag \"${1}\" & value \"${2}"\ >&2
usage
exit_err
fi
}
main "$@"