Skip to content

Commit 28e2a98

Browse files
enhance tools ahead of demo
1 parent d707f3a commit 28e2a98

File tree

2 files changed

+118
-22
lines changed

2 files changed

+118
-22
lines changed

bin/playground.sh

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ WITH_BUILD=0
1212
WITH_DEPLOY=0
1313
WITH_WORKLOAD=0
1414
OPT_IN_MODE=0
15+
AMBIENT_CONTEXT=0
1516

1617
show_help() {
1718
cat << EOF
@@ -23,11 +24,17 @@ show_help() {
2324
2425
Options:
2526
-r, --recreate-cluster Drop and recreate the playground K3D cluster
27+
2628
-b, --with-build Build the current version of the webhook docker image
2729
and make it available in the playground cluster
30+
2831
-d, --with-deploy Re-renders the various manifests required to setup the
2932
webhook deployment and its configuration.
33+
3034
-w, --with-workload Re-applies the demonstration/test DaemonSet workload.
35+
36+
-c, --ambient-context !!! Target ambient kubectl context instead of the K3d
37+
!!! playground cluster. Caution advised: read the script.
3138
EOF
3239
}
3340

@@ -38,6 +45,7 @@ while true; do
3845
-b | --with-build ) OPT_IN_MODE=1; WITH_BUILD=1; shift ;;
3946
-d | --with-deploy ) OPT_IN_MODE=1; WITH_DEPLOY=1; shift ;;
4047
-w | --with-workload ) OPT_IN_MODE=1; WITH_WORKLOAD=1; shift ;;
48+
-c | --ambient-context ) AMBIENT_CONTEXT=1; shift ;;
4149
-- ) shift; break ;;
4250
* ) break ;;
4351
esac
@@ -60,14 +68,22 @@ if ! k3d cluster list "$K3D_CLUSTER" | grep knss >/dev/null 2>&1; then
6068
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.15.3/cert-manager.yaml
6169
fi
6270

63-
_ctx=$(kubectl config get-contexts -o name | grep knss)
64-
if [[ -z "$_ctx" ]]; then
65-
echo "Cannot find knss kubectl context, aborting" >&2
66-
exit 1
71+
if [[ "$AMBIENT_CONTEXT" == 0 ]]; then
72+
_ctx=$(kubectl config get-contexts -o name | grep knss)
73+
if [[ -z "$_ctx" ]]; then
74+
echo "Cannot find knss kubectl context, aborting" >&2
75+
exit 1
76+
fi
6777
fi
6878

6979
function kctl() {
80+
if [[ "$AMBIENT_CONTEXT" == 0 ]]; then
81+
echo "[playground]" kubectl --context "$_ctx" "$@"
7082
kubectl --context "$_ctx" "$@"
83+
elif [[ "$AMBIENT_CONTEXT" == 1 ]]; then
84+
echo "[ambient-context]" kubectl "$@"
85+
kubectl "$@"
86+
fi
7187
}
7288

7389
cd "${SCRIPT_DIR}/.."
@@ -88,6 +104,8 @@ if [[ "$OPT_IN_MODE" == 0 || "$WITH_WORKLOAD" == 1 ]]; then
88104
kind: DaemonSet
89105
metadata:
90106
name: sleep-daemonset
107+
labels:
108+
app: sleep
91109
spec:
92110
selector:
93111
matchLabels:
@@ -99,11 +117,19 @@ if [[ "$OPT_IN_MODE" == 0 || "$WITH_WORKLOAD" == 1 ]]; then
99117
node-specific-sizing.manomano.tech/enabled: "true"
100118
annotations:
101119
node-specific-sizing.manomano.tech/request-memory-fraction: "0.05"
102-
node-specific-sizing.manomano.tech/limit-memory-fraction: "0.09"
120+
node-specific-sizing.manomano.tech/limit-memory-fraction: "0.06"
103121
node-specific-sizing.manomano.tech/minimum-memory: "40M"
104-
node-specific-sizing.manomano.tech/maximum-memory: "742M"
122+
node-specific-sizing.manomano.tech/maximum-memory: "600M"
105123
spec:
106124
terminationGracePeriodSeconds: 0
125+
priorityClassName: system-node-critical
126+
tolerations:
127+
- effect: NoSchedule
128+
key: node.kubernetes.io/role
129+
operator: Exists
130+
- effect: NoSchedule
131+
key: node.kubernetes.io/arch
132+
operator: Exists
107133
containers:
108134
- name: sleep-a
109135
image: alpine

bin/print_resources.sh

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,95 @@
11
#!/usr/bin/env bash
22

33
set -eo pipefail
4+
NAMESPACE=${NAMESPACE:-default}
5+
WATCH=${WATCH:-0}
6+
WATCH_DELAY=${WATCH_DELAY:-0.5}
7+
LABEL=${LABEL:-"app=sleep"}
48

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]
712
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.
915
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'
1318
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.
2021
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
2539
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

Comments
 (0)