Skip to content

Commit e146f43

Browse files
committed
fix(backups): exec into a CLI Pod in the nats demo
nats_cli seeded and verified JetStream streams through a throwaway `kubectl run -i` Pod per call. That Pod's stdout returns over an attach that only carries what the container writes after the attach registers, so a CLI that finishes first comes back empty with exit 0 -- the empty-read defect fixed for the redis demo. NATS ships no long-lived pod carrying the `nats` CLI, so run one nats-box Pod and `kubectl exec` into it: an exec'd process owns its pipes, so its output cannot be lost that way, and stderr stays attached so a failed read says why. cleanup.sh removes the Pod. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 1b2bd57 commit e146f43

3 files changed

Lines changed: 102 additions & 7 deletions

File tree

‎examples/backups/nats/00-helpers.sh‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export RESTOREJOB_INPLACE_NAME="${RESTOREJOB_INPLACE_NAME:-nats-restore-inplace}
2828
export RESTOREJOB_TOCOPY_NAME="${RESTOREJOB_TOCOPY_NAME:-nats-restore-to-copy}"
2929
# natsio/nats-box ships the `nats` CLI plus curl + tar + sh - everything the
3030
# generic Job strategy needs, with no purpose-built backup image. The seed /
31-
# verify helpers below run it as a throwaway Pod via `kubectl run`.
31+
# verify helpers below run it as a long-lived Pod they `kubectl exec` into.
3232
export NATS_BOX_IMAGE="${NATS_BOX_IMAGE:-natsio/nats-box:0.14.5}"
33+
# Name of that long-lived CLI Pod; cleanup.sh removes it.
34+
export NATS_CLI_POD="${NATS_CLI_POD:-nats-cli}"
3335

3436
log_info() { echo -e "${BLUE}i${NC} $*" >&2; }
3537
log_success() { echo -e "${GREEN}OK${NC} $*" >&2; }
@@ -88,14 +90,31 @@ nats_url() {
8890
echo "nats://${NATS_USER}:${NATS_PASSWORD}@${app}.${NAMESPACE}.svc:4222"
8991
}
9092

91-
# Run a `nats` CLI invocation against an application instance from a throwaway
92-
# nats-box Pod. Args after the app name are passed verbatim to `nats`.
93-
# Example: nats_cli "$NATS_NAME" stream ls
93+
# Ensure the long-lived nats-box CLI Pod exists and is Ready, so nats_cli can
94+
# exec into it. Idempotent: the Pod is reused across calls and across the
95+
# numbered demo scripts, and cleanup.sh removes it.
96+
nats_cli_pod() {
97+
kubectl -n "$NAMESPACE" get pod "$NATS_CLI_POD" >/dev/null 2>&1 \
98+
|| kubectl -n "$NAMESPACE" run "$NATS_CLI_POD" --image="$NATS_BOX_IMAGE" \
99+
--restart=Never --command -- sleep infinity >/dev/null
100+
kubectl -n "$NAMESPACE" wait --for=condition=Ready "pod/$NATS_CLI_POD" \
101+
--timeout=5m >/dev/null
102+
}
103+
104+
# Run a `nats` CLI invocation against an application instance. Args after the app
105+
# name are passed verbatim to `nats`. Example: nats_cli "$NATS_NAME" stream ls
106+
#
107+
# The command runs by `kubectl exec` in the long-lived nats-box Pod, not a
108+
# throwaway `kubectl run -i` Pod per call. A throwaway Pod's stdout comes back
109+
# over an attach that only carries what the container writes after the attach
110+
# registers, so a CLI that finishes in between returns empty with exit 0. An
111+
# exec'd process owns its pipes, so its output cannot be missed that way, and
112+
# kubectl's and the CLI's stderr stay attached so a failed read says why.
94113
nats_cli() {
95114
local app="$1"; shift
96-
kubectl -n "$NAMESPACE" run "nats-cli-$RANDOM" \
97-
--image="$NATS_BOX_IMAGE" --restart=Never --rm -i --quiet \
98-
--command -- nats --server "$(nats_url "$app")" "$@"
115+
nats_cli_pod
116+
kubectl -n "$NAMESPACE" exec -i "$NATS_CLI_POD" -- \
117+
nats --server "$(nats_url "$app")" "$@"
99118
}
100119

101120
# Number of messages currently stored in a JetStream stream, or "" if the

‎examples/backups/nats/cleanup.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ kubectl -n "$NAMESPACE" delete secret "${NATS_RESTORE_NAME}-backup-s3" --ignore-
1616
kubectl -n "$NAMESPACE" delete secret "${NATS_NAME}-backup-s3" --ignore-not-found
1717
kubectl -n "$NAMESPACE" delete nats "$NATS_RESTORE_NAME" --ignore-not-found
1818
kubectl -n "$NAMESPACE" delete nats "$NATS_NAME" --ignore-not-found
19+
kubectl -n "$NAMESPACE" delete pod "$NATS_CLI_POD" --ignore-not-found
1920
kubectl -n "$NAMESPACE" delete bucket "$BUCKET_NAME" --ignore-not-found
2021
rm -f "$SCRIPT_DIR/.bucket-info.env"
2122
kubectl delete backupclass "$BACKUPCLASS_NAME" --ignore-not-found

‎hack/nats-backup-demo-helpers.bats‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bats
2+
# Unit tests for nats_cli in examples/backups/nats/00-helpers.sh, the helper the
3+
# nats roundtrip suite seeds and verifies JetStream streams through.
4+
#
5+
# kubectl and the `nats` CLI are stub executables on PATH. The kubectl stub
6+
# answers only the calls nats_cli is expected to make -- get/wait for the
7+
# long-lived CLI Pod and an `exec` into it -- and runs the exec'd `nats`
8+
# invocation locally against the nats stub. A throwaway `kubectl run -i` Pod
9+
# (the old attach-racing path) makes the stub exit non-zero, so any test that
10+
# gets a correct reply proves the reply came back through exec, not that path.
11+
#
12+
# The helpers are Bash and this runner is POSIX sh, so each case drives them
13+
# through `bash -c` under the same `set -euo pipefail` the demo scripts use. The
14+
# helper functions close on an indented brace because the runner rewrites a
15+
# column-0 `}` into `return 0`, which would mask run_stream_count's exit status.
16+
17+
setup() {
18+
stub=$(mktemp -d)
19+
cat > "$stub/kubectl" <<'EOF'
20+
#!/bin/bash
21+
printf '%s\n' "$*" >> "$STUB_DIR/kubectl.argv"
22+
case "$1 $2 $3" in
23+
"-n tenant-root get")
24+
[ -n "${STUB_NO_POD:-}" ] && exit 1
25+
exit 0 ;;
26+
"-n tenant-root wait")
27+
exit 0 ;;
28+
"-n tenant-root run")
29+
# Only reachable when the CLI Pod was absent; otherwise nats_cli must never
30+
# start a Pod, and this exit fails the test if it does.
31+
[ -n "${STUB_NO_POD:-}" ] && exit 0
32+
echo "nats_cli started a throwaway run Pod instead of exec" >&2; exit 95 ;;
33+
"-n tenant-root exec")
34+
shift 6 # drop: -n tenant-root exec -i <pod> --
35+
exec "$@" ;;
36+
*)
37+
echo "unexpected kubectl call: $*" >&2; exit 97 ;;
38+
esac
39+
EOF
40+
cat > "$stub/nats" <<'EOF'
41+
#!/bin/sh
42+
# nats stub: only `stream info <name> --json` is exercised.
43+
case "$*" in
44+
*"stream info"*"--json"*)
45+
printf '{"state":{"messages":42}}\n'; exit 0 ;;
46+
esac
47+
echo "unexpected nats call: $*" >&2; exit 90
48+
EOF
49+
chmod +x "$stub/kubectl" "$stub/nats"
50+
: > "$stub/kubectl.argv"
51+
}
52+
53+
run_stream_count() {
54+
STUB_DIR=$stub STUB_NO_POD="${STUB_NO_POD:-}" PATH="$stub:$PATH" NAMESPACE=tenant-root \
55+
NATS_USER=demo NATS_PASSWORD=pw bash -c '
56+
set -euo pipefail
57+
. examples/backups/nats/00-helpers.sh
58+
stream_message_count demo orders
59+
'
60+
}
61+
62+
@test "nats_cli reads the reply by exec into the long-lived CLI Pod" {
63+
run run_stream_count
64+
[ "$status" -eq 0 ]
65+
[ "$output" = "42" ]
66+
grep -q 'exec -i nats-cli -- nats' "$stub/kubectl.argv"
67+
}
68+
69+
@test "nats_cli starts the CLI Pod when it is absent, then execs into it" {
70+
STUB_NO_POD=1 run run_stream_count
71+
[ "$status" -eq 0 ]
72+
[ "$output" = "42" ]
73+
grep -q 'run nats-cli --image=' "$stub/kubectl.argv"
74+
grep -q 'exec -i nats-cli -- nats' "$stub/kubectl.argv"
75+
}

0 commit comments

Comments
 (0)