forked from kedify/kubectl-kedify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.sh
More file actions
executable file
·517 lines (489 loc) · 15.9 KB
/
Copy pathdebug.sh
File metadata and controls
executable file
·517 lines (489 loc) · 15.9 KB
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
#!/usr/bin/env bash
function debug::__failure() {
local lineno=$1
local msg=$2
echo "Failed at $lineno: $msg"
}
function debug::__configure_shell() {
if [[ -n "${ZSH_VERSION:-}" ]]; then
emulate -L ksh
setopt typeset_silent
fi
set -euo pipefail
if [[ -n "${BASH_VERSION:-}" ]]; then
set -E
trap 'debug::__failure ${LINENO} "$BASH_COMMAND"' ERR
fi
}
function debug::__aggregate_interceptor_queue() {
local output_type="$1"
local mode="$2"
local cmd=""
case $output_type in
json)
cmd="jq '.'"
;;
yaml)
cmd="yq e -P"
;;
*)
cmd="$(debug::__addon_queue_structured_output "$mode") | column -t -s $'\t'"
;;
esac
jq -n --arg mode "$mode" '
if $mode == "aggregated" then
# Aggregate all queues (original behavior)
reduce inputs as $in ({};
reduce ($in.queue | to_entries[]) as $entry (.;
.[$entry.key] as $existing |
if $existing then
.[$entry.key] = (
$existing |
with_entries(
.value as $v |
$entry.value[.key] as $new_val |
if $new_val then
.value = ($v + $new_val)
else
.
end
)
)
else
.[$entry.key] = $entry.value
end
)
)
else
# Individual mode: preserve pod names
[inputs | {pod: .name, queue: .queue}]
end
' | eval "$cmd"
}
function debug::__addon_queue_structured_output() {
local mode="$1"
if [ "$mode" = "individual" ]; then
cat <<'EOF'
jq -r '
(["POD", "TARGET", "CONCURRENCY", "RPS"],
(.[] | [
.pod,
(.queue | keys[0]),
.queue[.queue | keys[0]].Concurrency,
.queue[.queue | keys[0]].RPS
]))
| @tsv'
EOF
else
cat <<'EOF'
jq -r '
(["TARGET", "CONCURRENCY", "RPS"],
(to_entries[] | [
.key,
.value.Concurrency,
.value.RPS
]))
| @tsv'
EOF
fi
}
function debug::__print_usage() {
cat << EOF
Usage: kubectl kedify debug <command> [options]
Available commands:
so/scaledobject Inspect ScaledObject resource
httpaddon Verify HTTP Addon setup and current configuration
Options:
-o, --output FORMAT Output format (json, yaml) - for supported commands
-i, --individual Show individual queue sizes per interceptor instead of aggregating
-w, --watch Continuously watch and update every second
-h, --help Show this help message
Examples:
kubectl kedify debug scaledobject -n default foo ... inspect ScaledObject resource named 'foo' in the 'default' namespace
kubectl kedify debug scaledobject --watch ... continuously watch all ScaledObjects and update every second
kubectl kedify debug httpaddon queue ... check the queue sizes in each HTTP addon interceptor pod
kubectl kedify debug httpaddon queue --watch ... continuously watch HTTP addon queue sizes and update every second
EOF
}
function debug::__structured_output() {
cat << EOF
jq -s '
group_by(.namespace, .name) |
map({
namespace: .[0].namespace,
name: .[0].name,
triggers: map({
triggerName: .triggerName,
triggerType: .triggerType,
metric: .metric,
value: .value
})
})
'
EOF
}
function debug::__httpaddon_cmd() {
if [[ $# -eq 0 ]]; then
echo "No sub-command provided, available sub-commands are:"
echo " queue"
echo ""
debug::__print_usage
exit 1
fi
local sub_command="$1"
shift
local ns=""
if kubectl get kedify -A > /dev/null 2>&1; then
ns=$(kubectl get kedify -A -o json | jq -r '.items[0].metadata.namespace')
fi
local output_type=""
local mode="aggregated"
local watch="false"
local next="false"
for o in "$@"; do
if [[ "$next" == "true" ]]; then
output_type="$o"
next="false"
continue
fi
case $o in
-o|--output)
next="true"
;;
-o=*|--output=*)
output_type="${o#*=}"
;;
-o*)
output_type="${o#-o}"
;;
--output*)
output_type="${o#--output}"
;;
-i|--individual)
mode="individual"
;;
-w|--watch)
watch="true"
;;
-h|--help)
debug::__print_usage
exit 0
;;
*)
echo "Unknown flag: $o"
echo ""
echo "Available flags:"
echo " -o|--output ... output format (json, yaml)"
echo " -i|--individual ... show individual queue sizes per interceptor instead of aggregating"
echo " -w|--watch ... continuously watch and update every second"
echo " -h|--help ... show this help message"
debug::__print_usage
exit 1
;;
esac
done
case $sub_command in
queue)
if [[ "$watch" == "true" ]]; then
while true; do
output=$(kubectl get pods -l app.kubernetes.io/name=http-add-on -l app.kubernetes.io/component=interceptor -n "$ns" -o json | jq -r '.items[].metadata.name' | while read -r pod; do
kubectl get --raw "/api/v1/namespaces/$ns/pods/$pod/proxy/queue" | jq -r --arg pod "$pod" '. | {name: $pod, queue: .}'
done | debug::__aggregate_interceptor_queue "$output_type" "$mode")
clear
echo "$(date): HTTP Add-on Queue Status (Press Ctrl+C to stop)"
echo ""
echo "$output"
sleep 1
done
else
kubectl get pods -l app.kubernetes.io/name=http-add-on -l app.kubernetes.io/component=interceptor -n "$ns" -o json | jq -r '.items[].metadata.name' | while read -r pod; do
kubectl get --raw "/api/v1/namespaces/$ns/pods/$pod/proxy/queue" | jq -r --arg pod "$pod" '. | {name: $pod, queue: .}'
done | debug::__aggregate_interceptor_queue "$output_type" "$mode"
fi
;;
*)
echo "Unknown sub-command: \"$sub_command\""
debug::__print_usage
exit 1
;;
esac
}
function debug::__no_value() {
local output_type="$1"
case "$output_type" in
json|yaml)
echo "0"
;;
*)
echo "<none>"
;;
esac
}
function debug::__no_trigger() {
local output_type="$1"
case "$output_type" in
json|yaml)
echo ""
;;
*)
echo "<none>"
;;
esac
}
function debug::__scaledobject() {
local so="$1"
local output_type="$2"
local print_namespace="$3"
local so_name=""
local hpa_name=""
local namespace=""
local hpa=""
local trigger_count=0
local index_offset=0
local i=0
local default_trigger_name=""
local trigger_type=""
local trigger_name=""
local metric=""
local metricType=""
local hasStatusCurrentMetrics=""
local val=""
local hpa_index=0
local api=""
so_name=$(echo "$so" | jq --raw-output '.metadata.name')
hpa_name=$(echo "$so" | jq --raw-output '.status.hpaName')
namespace=$(echo "$so" | jq --raw-output '.metadata.namespace')
hpa=$(kubectl get hpa "$hpa_name" -n "$namespace" -o json)
default_trigger_name="$(debug::__no_trigger "$output_type")"
trigger_count=$(echo "$so" | jq --raw-output '.spec.triggers | length')
while [[ $i -lt $trigger_count ]]; do
trigger_type=$(echo "$so" | jq --raw-output ".spec.triggers[$i].type")
trigger_name=$(echo "$so" | jq --raw-output --arg default_trigger_name "$default_trigger_name" ".spec.triggers[$i].name // \$default_trigger_name")
if [[ "$trigger_type" == "cpu" || "$trigger_type" == "memory" ]]; then
index_offset=$((index_offset + 1))
metric=$trigger_type
metricType=$(echo "$so" | jq --raw-output ".spec.triggers[$i].metricType")
hasStatusCurrentMetrics=$(echo "$hpa" | jq --raw-output '.status.currentMetrics | length')
if [[ "$hasStatusCurrentMetrics" -eq 0 ]]; then
val="$(debug::__no_value "$output_type")"
else
val=$(echo "$hpa" | jq --raw-output ".status.currentMetrics[] | select(.resource.name == \"$metric\") | .resource.current.average$metricType")
fi
else
hpa_index=$((i - index_offset))
metric=$(echo "$hpa" | jq --raw-output ".spec.metrics[$hpa_index].external.metric.name")
api="/apis/external.metrics.k8s.io/v1beta1/namespaces/${namespace}/${metric}?labelSelector=scaledobject.keda.sh%2Fname%3D${so_name}"
val=$(kubectl get --raw "$api" | jq --raw-output '.items[].value')
fi
case $output_type in
json|yaml)
jq -cn \
--arg namespace "$namespace" \
--arg name "$so_name" \
--arg trigger_name "$trigger_name" \
--arg trigger_type "$trigger_type" \
--arg metric "$metric" \
--arg value "$val" \
'{
namespace: $namespace,
name: $name,
triggerName: $trigger_name,
triggerType: $trigger_type,
metric: $metric,
value: ($value | tonumber? // .)
}'
;;
wide)
if [[ "$print_namespace" == "true" ]]; then
echo "$namespace $so_name $i $trigger_name $trigger_type $metric $val"
else
echo "$so_name $i $trigger_name $trigger_type $metric $val"
fi
;;
*)
if [[ "$print_namespace" == "true" ]]; then
echo "$namespace $so_name $trigger_type $val"
else
echo "$so_name $trigger_type $val"
fi
;;
esac
i=$((i + 1))
done
}
function debug::__scaledobject_cmd() {
local filtered_flags=()
local output_type=""
local next="false"
local print_namespace="false"
local watch="false"
for o in "$@"; do
if [[ "$next" == "true" ]]; then
output_type="$o"
next="false"
continue
fi
case $o in
-o|--output)
next="true"
;;
-o=*|--output=*)
output_type="${o#*=}"
;;
-o*)
output_type="${o#-o}"
;;
--output*)
output_type="${o#--output}"
;;
-w|--watch)
watch="true"
;;
--all-namespaces|-A)
print_namespace="true"
filtered_flags+=("$o")
;;
-h|--help)
debug::__print_usage
exit 0
;;
*)
# Check if it's a flag (starts with -)
if [[ "$o" == -* ]]; then
echo "Unknown flag: $o"
echo ""
echo "Available flags:"
echo " -o|--output ... output format (json, yaml, wide)"
echo " -w|--watch ... continuously watch and update every second"
echo " -A|--all-namespaces ... list resources from all namespaces"
echo ""
echo " -h|--help ... show this help message"
debug::__print_usage
exit 1
else
filtered_flags+=("$o")
fi
;;
esac
done
if [[ "$watch" == "true" ]]; then
while true; do
if [[ ${#filtered_flags[@]} -eq 0 ]]; then
output=$(debug::__execute_scaledobject_once "$output_type" "$print_namespace")
else
output=$(debug::__execute_scaledobject_once "$output_type" "$print_namespace" "${filtered_flags[@]}")
fi
clear
echo "$(date): ScaledObject Status (Press Ctrl+C to stop)"
echo ""
echo "$output"
sleep 1
done
else
if [[ ${#filtered_flags[@]} -eq 0 ]]; then
debug::__execute_scaledobject_once "$output_type" "$print_namespace"
else
debug::__execute_scaledobject_once "$output_type" "$print_namespace" "${filtered_flags[@]}"
fi
fi
}
function debug::__execute_scaledobject_once() {
local output_type="$1"
local print_namespace="$2"
shift 2
local filtered_flags=("$@")
local output=""
if [[ ${#filtered_flags[@]} -eq 0 ]]; then
if ! output=$(kubectl get scaledobjects -o json 2>&1); then
echo "Error: Failed to get ScaledObjects. Make sure KEDA is installed and you have the necessary permissions."
exit 1
fi
else
if ! output=$(kubectl get scaledobjects "${filtered_flags[@]}" -o json 2>&1); then
# Check if it's a "not found" error
if echo "$output" | grep -q "not found"; then
# Extract the resource name from the error message
local resource_name=""
resource_name=$(echo "$output" | sed -n 's/.*scaledobjects.keda.sh "\([^"]*\)" not found.*/\1/p')
if [[ -n "$resource_name" ]]; then
echo "ScaledObject \"$resource_name\" not found"
else
echo "ScaledObject not found"
fi
else
echo "Error: $output"
fi
exit 1
fi
fi
local kind=""
local formatting_cmd=""
local header=""
kind=$(echo "$output" | jq -r '.kind')
case $output_type in
json)
formatting_cmd="$(debug::__structured_output)"
;;
yaml)
formatting_cmd="$(debug::__structured_output) | yq e -P"
;;
wide)
formatting_cmd="column -t"
if [[ "$print_namespace" == "true" ]]; then
header="NAMESPACE NAME INDEX TRIGGER_NAME TYPE METRIC VALUE"
else
header="NAME INDEX TRIGGER_NAME TYPE METRIC VALUE"
fi
;;
*)
formatting_cmd="column -t"
if [[ "$print_namespace" == "true" ]]; then
header="NAMESPACE NAME TYPE VALUE"
else
header="NAME TYPE VALUE"
fi
;;
esac
(
if [[ -n "$header" ]]; then
echo "$header"
fi
case $kind in
"List")
echo "$output" | jq -c '.items[]' | while read -r item; do
debug::__scaledobject "$item" "$output_type" "$print_namespace"
done
;;
"ScaledObject")
debug::__scaledobject "$output" "$output_type" "$print_namespace"
;;
*)
echo "Unknown kind: $kind"
exit 1
;;
esac
) | eval "$formatting_cmd"
}
function debug::__cmd_impl() {
if [[ $# -eq 0 ]]; then
debug::__print_usage
exit 1
fi
case $1 in
so|scaledobject)
debug::__scaledobject_cmd "${@:2}"
;;
httpaddon)
debug::__httpaddon_cmd "${@:2}"
;;
*)
echo "Unknown sub-command: \"$1\""
debug::__print_usage
exit 1
;;
esac
}
function debug::cmd() (
debug::__configure_shell
debug::__cmd_impl "$@"
)