Skip to content

Commit 9cea7dc

Browse files
committed
fix(backups): exec into a CLI Pod in the kafka-metadata demo
kafka_run seeded and verified topics 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. Run one long-lived CLI 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 (the topic_meta / topic_partitions reads keep it) so a failed read says why. The Pod carries cozystack.io/backup-demo=kafka-metadata: kafka_cli_pod reuses only a Ready Pod the demo owns, replaces a leftover in a terminal phase, and refuses a same-named Pod it does not own; cleanup.sh reaps by that label with --grace-period=1. Unit tests under hack/ pin the exec path -- a throwaway run Pod or a re-added stderr redirect reddens them. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 7b94287 commit 9cea7dc

4 files changed

Lines changed: 154 additions & 16 deletions

File tree

‎examples/backups/kafka-metadata/00-helpers.sh‎

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ export S3_CA_KEY="${S3_CA_KEY:-ca.crt}"
5858
export CA_MOUNT_DIR="${CA_MOUNT_DIR:-/etc/ssl/kafka-backup-ca}"
5959
# The Cozystack chart names the Strimzi cluster kafka-<app>; its plaintext
6060
# bootstrap Service is kafka-<app>-kafka-bootstrap:9092.
61-
# KAFKA_IMAGE only drives the host-side throwaway CLI pods here (seed/verify).
62-
# The backup/restore Jobs no longer use it: the controller resolves the target
63-
# broker's own image at reconcile time and renders it as the strategy's
64-
# .ClientImage. Override this only to match your operator's image for the
65-
# seed/verify pods if it differs.
61+
# KAFKA_IMAGE runs the long-lived CLI Pod the seed/verify helpers exec into (see
62+
# kafka_run). The backup/restore Jobs no longer use it: the controller resolves
63+
# the target broker's own image at reconcile time and renders it as the
64+
# strategy's .ClientImage. Override this only to match your operator's image if
65+
# the CLI it carries differs.
6666
export KAFKA_IMAGE="${KAFKA_IMAGE:-quay.io/strimzi/kafka:0.45.1-rc1-kafka-3.9.1@sha256:ba52ed046b1dccdbd96f4e68057ce014d862a7c9c1fc670760c023b9aa09f23f}"
6767
export KAFKA_BIN="${KAFKA_BIN:-/opt/kafka/bin}"
68+
# Name of that long-lived CLI Pod; cleanup.sh removes it.
69+
export KAFKA_CLI_POD="${KAFKA_CLI_POD:-kafka-cli}"
6870

6971
log_info() { echo -e "${BLUE}i${NC} $*" >&2; }
7072
log_success() { echo -e "${GREEN}OK${NC} $*" >&2; }
@@ -200,18 +202,46 @@ kafka_wait_ready() {
200202
'{.status.conditions[?(@.type=="Ready")].status}' True "$NAMESPACE" "$timeout"
201203
}
202204

203-
# Run a bash snippet in a throwaway Strimzi Kafka Pod, with $BOOT / $BIN / $TOPIC
204-
# pre-set (values injected via printf %q so the snippet needs no nested quoting).
205-
# Host-side analogue used only to seed and verify — the backup/restore Jobs are
206-
# created by the controller from the strategy.
205+
# Ensure the long-lived kafka-cli Pod exists and is Ready, so kafka_run can exec
206+
# into it. Idempotent: a Ready Pod this demo owns is reused across calls and
207+
# across the numbered demo scripts; a leftover in a terminal phase (Succeeded /
208+
# Failed) is replaced rather than waited on; and a same-named Pod this demo does
209+
# not own is refused rather than hijacked or deleted. cleanup.sh removes it.
210+
kafka_cli_pod() {
211+
local phase owner
212+
phase=$(kubectl -n "$NAMESPACE" get pod "$KAFKA_CLI_POD" -o jsonpath='{.status.phase}' 2>/dev/null || true)
213+
owner=$(kubectl -n "$NAMESPACE" get pod "$KAFKA_CLI_POD" -o jsonpath='{.metadata.labels.cozystack\.io/backup-demo}' 2>/dev/null || true)
214+
if [ -n "$phase" ] && [ "$owner" != "kafka-metadata" ]; then
215+
log_error "Pod $NAMESPACE/$KAFKA_CLI_POD exists but this demo does not own it; refusing to use or delete it"
216+
return 1
217+
fi
218+
if [ "$phase" != "Running" ] && [ "$phase" != "Pending" ]; then
219+
kubectl -n "$NAMESPACE" delete pod "$KAFKA_CLI_POD" --grace-period=1 --ignore-not-found >/dev/null
220+
kubectl -n "$NAMESPACE" run "$KAFKA_CLI_POD" --image="$KAFKA_IMAGE" \
221+
--labels=cozystack.io/backup-demo=kafka-metadata \
222+
--restart=Never --command -- sleep infinity >/dev/null
223+
fi
224+
kubectl -n "$NAMESPACE" wait --for=condition=Ready "pod/$KAFKA_CLI_POD" \
225+
--timeout=5m >/dev/null
226+
}
227+
228+
# Run a bash snippet against the source Kafka, with $BOOT / $BIN / $TOPIC /
229+
# $PARTITIONS pre-set (values injected via printf %q so the snippet needs no
230+
# nested quoting). Host-side analogue used only to seed and verify — the
231+
# backup/restore Jobs are created by the controller from the strategy.
232+
#
233+
# The snippet runs by `kubectl exec` in the long-lived CLI Pod, not a throwaway
234+
# `kubectl run -i` Pod per call. A throwaway Pod's stdout comes back over an
235+
# attach that only carries what the container writes after the attach registers,
236+
# so a CLI that finishes in between returns empty with exit 0. An exec'd process
237+
# owns its pipes, so its output cannot be missed that way, and kubectl's and the
238+
# CLI's stderr stay attached so a failed read says why instead of reading as ''.
207239
kafka_run() {
208240
local app="$1"; shift
209241
local snippet="$1"
210242
local boot="kafka-${app}-kafka-bootstrap.${NAMESPACE}.svc:9092"
211-
kubectl -n "$NAMESPACE" run "kafka-cli-$RANDOM" \
212-
--image="$KAFKA_IMAGE" --restart=Never --rm -i --quiet \
213-
--pod-running-timeout=5m \
214-
--command -- bash -c "set -eu
243+
kafka_cli_pod
244+
kubectl -n "$NAMESPACE" exec -i "$KAFKA_CLI_POD" -- bash -c "set -eu
215245
BOOT=$(printf %q "$boot")
216246
BIN=$(printf %q "$KAFKA_BIN")
217247
TOPIC=$(printf %q "$TOPIC")
@@ -262,7 +292,7 @@ topic_partitions() {
262292
line=$("$BIN"/kafka-topics.sh --bootstrap-server "$BOOT" --describe --topic "\Q$TOPIC\E" 2>/dev/null | head -1) || exit 0
263293
[ -n "$line" ] || exit 0
264294
printf "%s" "$line" | grep -oE "PartitionCount: [0-9]+" | awk "{print \$2}"
265-
' 2>/dev/null | tr -d '\r\n'
295+
' | tr -d '\r\n'
266296
}
267297

268298
# Print "<partitions> <retention.ms>" for the demo topic, or "" if absent.
@@ -274,5 +304,5 @@ topic_meta() {
274304
parts=$(printf "%s" "$line" | grep -oE "PartitionCount: [0-9]+" | awk "{print \$2}")
275305
ret=$(printf "%s" "$line" | grep -oE "retention.ms=[0-9]+" | head -1 | cut -d= -f2)
276306
printf "%s %s\n" "$parts" "$ret"
277-
' 2>/dev/null | tr -d '\r'
307+
' | tr -d '\r'
278308
}

‎examples/backups/kafka-metadata/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The platform default backups stack must be installed: the `backupstrategy-contro
1818

1919
## Flow
2020

21-
- `00-helpers.sh` — shared bash helpers (waiters, kafka seed/verify via a throwaway CLI pod, the demo strategy derivation + S3 CA copy).
21+
- `00-helpers.sh` — shared bash helpers (waiters, kafka seed/verify via a long-lived CLI pod it execs into, the demo strategy derivation + S3 CA copy).
2222
- `03-backupclass.yaml` — the demo `BackupClass` (`kafka-metadata`) mapping `Kafka` to the derived `kafka-strategy-default`.
2323
- `05-kafka-src.yaml` — the source application (no `backup:` block; the strategy carries the coordinates).
2424
- `10-backupjob-adhoc.yaml` — an ad-hoc `BackupJob` routed to the demo `kafka-metadata` BackupClass.

‎examples/backups/kafka-metadata/cleanup.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ kubectl -n "$NAMESPACE" delete kafka.apps.cozystack.io "$KAFKA_SRC_NAME" --ignor
1919
kubectl delete backupclass.backups.cozystack.io "$BACKUPCLASS_NAME" --ignore-not-found
2020
kubectl delete kafka.strategy.backups.cozystack.io "$STRATEGY_NAME" --ignore-not-found
2121
kubectl -n "$NAMESPACE" delete secret "$CA_SECRET" --ignore-not-found
22+
kubectl -n "$NAMESPACE" delete pod -l cozystack.io/backup-demo=kafka-metadata --grace-period=1 --ignore-not-found
2223

2324
log_success "Cleanup complete."
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bats
2+
# Unit tests for kafka_run in examples/backups/kafka-metadata/00-helpers.sh, the
3+
# helper the kafka-3-metadata-roundtrip suite seeds and verifies topics through.
4+
#
5+
# kubectl and the Kafka CLI are stub executables on PATH. The kubectl stub
6+
# answers the calls kafka_run makes -- get the CLI Pod's phase and owner label,
7+
# create it when absent, wait for it, and `exec` into it -- and runs the exec'd
8+
# snippet locally against the kafka-topics.sh stub. STUB_PHASE / STUB_OWNER drive
9+
# the reuse guard, and STUB_EXEC_ERROR makes the exec fail so the "a failed read
10+
# says why" claim is pinned rather than asserted in prose.
11+
#
12+
# cozytest.sh's awk runner recognizes only @test blocks and a bare `}`; there is
13+
# no bats `run` or `$status`, and no setup/teardown, so each case calls
14+
# make_stubs itself and asserts with direct shell tests that exit non-zero. The
15+
# helper functions close on an indented brace because the runner rewrites a
16+
# column-0 `}` into `return 0`, which would mask run_topic_meta's exit status.
17+
18+
make_stubs() {
19+
stub=$(mktemp -d)
20+
cat > "$stub/kubectl" <<'EOF'
21+
#!/bin/bash
22+
printf '%s\n' "$*" >> "$STUB_DIR/kubectl.argv"
23+
case "$1 $2 $3" in
24+
"-n tenant-root get")
25+
case "$*" in
26+
*"jsonpath={.status.phase}"*) printf '%s' "${STUB_PHASE-Running}" ;;
27+
*"jsonpath={.metadata.labels"*) printf '%s' "${STUB_OWNER-kafka-metadata}" ;;
28+
esac
29+
exit 0 ;;
30+
"-n tenant-root delete")
31+
exit 0 ;;
32+
"-n tenant-root run")
33+
# Reachable only when the Pod is absent; a reuse that starts a Pod is a bug.
34+
[ -z "${STUB_PHASE-Running}" ] && exit 0
35+
echo "kafka_run started a Pod though a Ready one it owns exists" >&2; exit 95 ;;
36+
"-n tenant-root wait")
37+
exit 0 ;;
38+
"-n tenant-root exec")
39+
[ -n "${STUB_EXEC_ERROR:-}" ] && { echo "$STUB_EXEC_ERROR" >&2; exit 1; }
40+
shift 6 # drop: -n tenant-root exec -i <pod> --
41+
if [ "$1" != "bash" ] || [ "$2" != "-c" ]; then
42+
echo "unexpected exec payload: $*" >&2; exit 96
43+
fi
44+
shift 2
45+
exec bash -c "$1" ;;
46+
*)
47+
echo "unexpected kubectl call: $*" >&2; exit 97 ;;
48+
esac
49+
EOF
50+
cat > "$stub/kafka-topics.sh" <<'EOF'
51+
#!/bin/sh
52+
# Minimal --describe stub: one line carrying PartitionCount and retention.ms so
53+
# topic_meta has a realistic line to parse.
54+
printf 'Topic: orders\tPartitionCount: 3\tReplicationFactor: 1\tConfigs: retention.ms=123456\n'
55+
EOF
56+
chmod +x "$stub/kubectl" "$stub/kafka-topics.sh"
57+
: > "$stub/kubectl.argv"
58+
}
59+
60+
run_topic_meta() {
61+
STUB_DIR=$stub STUB_PHASE="${STUB_PHASE-Running}" STUB_OWNER="${STUB_OWNER-kafka-metadata}" \
62+
STUB_EXEC_ERROR="${STUB_EXEC_ERROR:-}" PATH="$stub:$PATH" NAMESPACE=tenant-root \
63+
KAFKA_BIN=$stub TOPIC=orders bash -c '
64+
set -euo pipefail
65+
. examples/backups/kafka-metadata/00-helpers.sh
66+
topic_meta demo
67+
'
68+
}
69+
70+
@test "kafka_run reuses a Ready Pod it owns and reads the reply by exec" {
71+
make_stubs
72+
out=$(run_topic_meta)
73+
[ "$out" = "3 123456" ]
74+
grep -q 'exec -i kafka-cli -- bash -c' "$stub/kubectl.argv"
75+
}
76+
77+
@test "kafka_run creates the CLI Pod when it is absent, then execs into it" {
78+
make_stubs
79+
export STUB_PHASE=
80+
out=$(run_topic_meta)
81+
[ "$out" = "3 123456" ]
82+
grep -q 'run kafka-cli --image=' "$stub/kubectl.argv"
83+
grep -q 'labels=cozystack.io/backup-demo=kafka-metadata' "$stub/kubectl.argv"
84+
grep -q 'exec -i kafka-cli -- bash -c' "$stub/kubectl.argv"
85+
}
86+
87+
@test "kafka_run refuses a same-named Pod the demo does not own" {
88+
make_stubs
89+
export STUB_OWNER=someone-else
90+
err=$(mktemp)
91+
if run_topic_meta 2>"$err"; then
92+
echo "kafka_run used a Pod it does not own" >&2
93+
exit 1
94+
fi
95+
grep -q 'does not own it' "$err"
96+
}
97+
98+
@test "kafka_run fails with kubectl's own error when the exec fails" {
99+
make_stubs
100+
export STUB_EXEC_ERROR="error: unable to upgrade connection: container not found"
101+
err=$(mktemp)
102+
if run_topic_meta 2>"$err"; then
103+
echo "a failed exec read as a successful empty reply" >&2
104+
exit 1
105+
fi
106+
grep -q 'unable to upgrade connection: container not found' "$err"
107+
}

0 commit comments

Comments
 (0)