|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +expect_completed=$1 |
| 4 | +buffer_rate=${2:-0.01} |
| 5 | +wait_interval=${3:-30} |
| 6 | + |
| 7 | +echo "waiting for $expect_completed Jobs to be completed successfully" |
| 8 | +echo "using buffer failure rate: $buffer_rate, wait interval: ${wait_interval}s" |
| 9 | + |
| 10 | +while true; do |
| 11 | + sleep "$wait_interval" |
| 12 | + |
| 13 | + num_completed=0 |
| 14 | + num_failed=0 |
| 15 | + num_running=0 |
| 16 | + num_pending=0 |
| 17 | + |
| 18 | + # Print table header |
| 19 | + echo "╭─────────────────┬────────────┬──────────┬──────────┬─────────╮" |
| 20 | + echo "│ Namespace │ Completed │ Running │ Pending │ Failed │" |
| 21 | + echo "├─────────────────┼────────────┼──────────┼──────────┼─────────┤" |
| 22 | + |
| 23 | + for ns in $(kubectl get ns --no-headers -o custom-columns=":metadata.name" | grep '^test-'); do |
| 24 | + # Initialize namespace-specific counters |
| 25 | + ns_completed=0 |
| 26 | + ns_running=0 |
| 27 | + ns_pending=0 |
| 28 | + ns_failed=0 |
| 29 | + |
| 30 | + # Get job statuses and count them |
| 31 | + jobs_output=$(kubectl get jobs -n "$ns" --no-headers 2>/dev/null) |
| 32 | + if [[ -n "$jobs_output" ]]; then |
| 33 | + # Process each job line |
| 34 | + while IFS= read -r line; do |
| 35 | + if [[ -n "$line" ]]; then |
| 36 | + # Parse job fields: NAME STATUS COMPLETIONS DURATION AGE |
| 37 | + status=$(echo "$line" | awk '{print $2}') |
| 38 | + completions=$(echo "$line" | awk '{print $3}') |
| 39 | + |
| 40 | + # Determine job status based on kubectl output |
| 41 | + if [[ "$status" == "Complete" ]]; then |
| 42 | + num_completed=$((num_completed+1)) |
| 43 | + ns_completed=$((ns_completed+1)) |
| 44 | + elif [[ "$status" == "Running" ]]; then |
| 45 | + # Check if completions field matches pattern X/Y to distinguish running vs pending |
| 46 | + if [[ "$completions" =~ ^([0-9]+)/([0-9]+)$ ]]; then |
| 47 | + current=${BASH_REMATCH[1]} |
| 48 | + total=${BASH_REMATCH[2]} |
| 49 | + |
| 50 | + if [[ "$current" -gt 0 && "$current" -lt "$total" ]]; then |
| 51 | + num_running=$((num_running+1)) |
| 52 | + ns_running=$((ns_running+1)) |
| 53 | + else |
| 54 | + num_pending=$((num_pending+1)) |
| 55 | + ns_pending=$((ns_pending+1)) |
| 56 | + fi |
| 57 | + else |
| 58 | + num_running=$((num_running+1)) |
| 59 | + ns_running=$((ns_running+1)) |
| 60 | + fi |
| 61 | + elif [[ "$status" == "Failed" ]]; then |
| 62 | + num_failed=$((num_failed+1)) |
| 63 | + ns_failed=$((ns_failed+1)) |
| 64 | + else |
| 65 | + num_pending=$((num_pending+1)) |
| 66 | + ns_pending=$((ns_pending+1)) |
| 67 | + fi |
| 68 | + fi |
| 69 | + done <<< "$jobs_output" |
| 70 | + fi |
| 71 | + |
| 72 | + # Print namespace row in table format |
| 73 | + printf "│ %-15s │ %10s │ %8s │ %8s │ %7s │\n" "$ns" "$ns_completed" "$ns_running" "$ns_pending" "$ns_failed" |
| 74 | + done |
| 75 | + |
| 76 | + # Print table footer |
| 77 | + echo "╰─────────────────┴────────────┴──────────┴──────────┴─────────╯" |
| 78 | + echo |
| 79 | + |
| 80 | + echo "Job Status Summary:" |
| 81 | + echo " Completed: $num_completed" |
| 82 | + echo " Running: $num_running" |
| 83 | + echo " Pending: $num_pending" |
| 84 | + echo " Failed: $num_failed" |
| 85 | + |
| 86 | + buffer=$(echo "$expect_completed * $buffer_rate" | bc) |
| 87 | + min_completed=$(printf "%.0f" "$(echo "$expect_completed - $buffer" | bc)") |
| 88 | + if [[ "$num_completed" -ge "$min_completed" ]]; then |
| 89 | + break; |
| 90 | + fi |
| 91 | +done |
0 commit comments