|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | set -eo pipefail |
| 4 | +NAMESPACE=${NAMESPACE:-default} |
| 5 | +WATCH=${WATCH:-0} |
| 6 | +WATCH_DELAY=${WATCH_DELAY:-0.5} |
| 7 | +LABEL=${LABEL:-"app=sleep"} |
4 | 8 |
|
5 | | -_pod_json=$(kubectl -n default get -o json pods) |
6 | | -_len=$(echo "$_pod_json" | jq -r '.items | length - 1') |
| 9 | +show_help() { |
| 10 | + cat << EOF |
| 11 | + Usage: $0 [--namespace NS] [--watch] [--watch-delay WATCH_DELAY] [--label LABEL] |
7 | 12 |
|
8 | | -# jq -r '.spec.nodeName as $hip | .spec.containers[] | .name as $n | .resources | "\($hip) - \($n) - REQ \(.requests.memory) - LIM \(.limits.memory)"' |
| 13 | + Dumps the requests/limits of all pods in a concise way. |
| 14 | + Uses the active kubectl context. |
9 | 15 |
|
10 | | -for i in `seq 0 "$_len"`; do |
11 | | - _node=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.nodeName") |
12 | | - _containers_count=$(echo "$_pod_json" | jq ".items[$i] | .spec.containers | length - 1") |
| 16 | + Options: |
| 17 | + -n, --namespace Namespace. Default is 'default' |
13 | 18 |
|
14 | | - for j in `seq 0 "$_containers_count"`; do |
15 | | - _name=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.containers[$j] | .name") |
16 | | - _cpu_req=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.containers[$j] | .resources.requests.cpu") |
17 | | - _mem_req=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.containers[$j] | .resources.requests.memory") |
18 | | - _cpu_lim=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.containers[$j] | .resources.limits.cpu") |
19 | | - _mem_lim=$(echo "$_pod_json" | jq -r ".items[$i] | .spec.containers[$j] | .resources.limits.memory") |
| 19 | + -w, --watch Enable 'watch' like mode, which refreshes the output every |
| 20 | + WATCH_DELAY seconds. Unlike watch, this will print colors just fine. |
20 | 21 |
|
21 | | - # shellcheck disable=SC2182 |
22 | | - printf "%-20s cpu: req=%-5s lim=%-5s\n" "$_node" "$_cpu_req" "$_cpu_lim" |
23 | | - printf "%-20s mem: req=%-5s lim=%-5s\n" "--- $_name" "$_mem_req" "$_mem_lim" |
24 | | - done |
| 22 | + -n, --watch-delay Sets the delay between two prints. Defaults to 0.5 |
| 23 | + Any valid parameters to 'sleep' will work here. |
| 24 | +
|
| 25 | + -l, --label Label selector for the pods. Defaults to "app=sleep" |
| 26 | +EOF |
| 27 | +} |
| 28 | + |
| 29 | +while true; do |
| 30 | + case "$1" in |
| 31 | + -h | --help ) show_help; exit 0 ;; |
| 32 | + -n | --namespace ) shift; NAMESPACE=$1 shift ;; |
| 33 | + -w | --watch ) WATCH=1; shift ;; |
| 34 | + -n | --watch-delay ) shift; WATCH_DELAY=$1; shift ;; |
| 35 | + -l | --label ) shift; LABEL=$1; shift ;; |
| 36 | + -- ) shift; break ;; |
| 37 | + * ) break ;; |
| 38 | + esac |
25 | 39 | done |
| 40 | + |
| 41 | +function print_stats() { |
| 42 | + _pod_json=$(kubectl -n "$NAMESPACE" get -o json -l "$LABEL" pods) |
| 43 | + _len=$(echo "$_pod_json" | jq -r '.items | length - 1') |
| 44 | + |
| 45 | + if [[ "$_len" == 0 ]]; then |
| 46 | + echo "No pods found in namespace=$NAMESPACE with label=$LABEL" >&2 |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + # Display relevant labels / annotations |
| 51 | + _ds_json=$(kubectl -n "$NAMESPACE" get -o json ds -l "$LABEL") |
| 52 | + _annotations=$(echo "$_ds_json" | jq -r '.items[0] | .spec.template.metadata | [.annotations, .labels | to_entries] | flatten | .[] | select(.key | test("node-specific")) | "\(.key): \(.value)"') |
| 53 | + printf "\x1b[38;2;128;192;255m%s \033[0m \n\n\n" "$_annotations" |
| 54 | + |
| 55 | + # Display per-container resource usage, compact |
| 56 | + for i in $(seq 0 "$_len"); do |
| 57 | + _item_json=$(echo "$_pod_json" | jq -c ".items[$i]") |
| 58 | + |
| 59 | + _node=$(echo "$_item_json" | jq -r '.spec.nodeName | strings | gsub("\\.[^\\n]*"; "")') |
| 60 | + _containers_count=$(echo "$_item_json" | jq ".spec.containers | length - 1") |
| 61 | + |
| 62 | + _grey=1 |
| 63 | + for j in $(seq 0 "$_containers_count"); do |
| 64 | + _container_resources=$(echo "$_item_json" | \ |
| 65 | + jq -rc ".spec.containers[$j] | \"\\(.name) \\(.resources.requests.cpu) \\(.resources.requests.memory) \\(.resources.limits.cpu) \\(.resources.limits.memory)\"") |
| 66 | + readarray -t -d ' ' arr <<< "$_container_resources" |
| 67 | + |
| 68 | + _name="${arr[0]}" |
| 69 | + _cpu_req="${arr[1]}" |
| 70 | + _mem_req="${arr[2]}" |
| 71 | + _cpu_lim="${arr[3]}" |
| 72 | + _mem_lim="${arr[4]%$'\n'}" |
| 73 | + |
| 74 | + # shellcheck disable=SC2182 |
| 75 | + # printf "%-60s cpu: req=%-5s lim=%-5s\n" "$_node" "$_cpu_req" "$_cpu_lim" |
| 76 | + [[ $_grey == 1 ]] && echo -ne "\033[48;5;238m" # ANSI Light Grey Background |
| 77 | + |
| 78 | + printf "%-30s %-20s mem: req=%-7s lim=%-7s" "$_node" "$_name" "$_mem_req" "$_mem_lim" |
| 79 | + |
| 80 | + [[ $_grey == 1 ]] && { echo -ne "\033[0m"; _grey=0; } || _grey=1 |
| 81 | + |
| 82 | + printf "\n" |
| 83 | + done |
| 84 | + done |
| 85 | +} |
| 86 | + |
| 87 | +if [[ "$WATCH" == 0 ]]; then |
| 88 | + print_stats |
| 89 | +else |
| 90 | + while true; do |
| 91 | + _stats=$(print_stats) |
| 92 | + echo -e "\033[H\033[J\033[K${_stats}" # clear visible screen |
| 93 | + sleep "$WATCH_DELAY" |
| 94 | + done |
| 95 | +fi |
0 commit comments