fix(clickhouse): make the example strategy use the archive its Backup records

The example strategy minted its own archive name from the clock and
restored the newest archive of the source application. The controller
then found no archive name in the backup Job and recorded none, so a
restore of an older Backup silently restored the latest archive and
deleting a Backup left its archive in the bucket.

The example now uploads under the name the controller chose and
restores the archive the Backup records, as the shipped strategy does;
the clock-made name and the newest archive remain the fallback for a
run the controller gave no name. The backup scenario no longer says the
archive is expired by the upstream tool's retention, which the chart
does not configure.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov authored and Aleksei Sviridkin committed 2026-11-23 12:15:06 +03:00
1 parent 084fed9e2f
commit 8cf04bdd0f
3 files changed
+154 -9

No files matched your search

@@ -78,6 +78,11 @@ spec:
# \${API_USERNAME} / \${API_PASSWORD} come from the
# backup-api-auth Secret mounted via env (see above).
auth="-u \${API_USERNAME}:\${API_PASSWORD}"
# The archive the controller named for this run and records on
# its Backup, so that the restore and the delete of that Backup
# reach the archive the run wrote. Empty when the controller
# gave no name; the names below are the fallback for that case.
archive="{{ .ArchiveName | default "" }}"
# Sidecar may still be coming up when the strategy Pod fires.
# Block on /backup/status until it answers (max ~5 minutes).
@@ -101,18 +106,21 @@ spec:
# also lands on the correct archive.
if [ "{{ .Mode }}" = "backup" ]; then
prefix="clickhouse-{{ .Release.Name }}-"
target="\${prefix}\$(date -u +%Y%m%dT%H%M%SZ)"
target="\${archive:-\${prefix}\$(date -u +%Y%m%dT%H%M%SZ)}"
echo "create_remote \${target}"
curl -fsS \${auth} -X POST "\${api}/backup/create_remote?name=\${target}" >/dev/null
else
prefix="clickhouse-{{ .Backup.ApplicationRef.Name }}-"
# Pick the latest archive whose name starts with the
# source-release prefix. Falls through to "no remote
# backup found" if S3_PATH is misconfigured AND none of
# the listed names match.
target=\$(curl -fsS \${auth} --max-time 60 "\${api}/backup/list/remote" \\
| jq -rs --arg prefix "\${prefix}" \\
'[.[] | select((.name // "") | startswith(\$prefix))] | sort_by(.created // "") | last.name // ""')
# A Backup that records no archive gets the latest archive
# whose name starts with the source-release prefix. Falls
# through to "no remote backup found" if S3_PATH is
# misconfigured AND none of the listed names match.
target="\${archive}"
if [ -z "\${target}" ]; then
target=\$(curl -fsS \${auth} --max-time 60 "\${api}/backup/list/remote" \\
| jq -rs --arg prefix "\${prefix}" \\
'[.[] | select((.name // "") | startswith(\$prefix))] | sort_by(.created // "") | last.name // ""')
fi
if [ -z "\${target}" ] || [ "\${target}" = "null" ]; then
echo "no remote backup found" >&2
exit 1
@@ -42,5 +42,5 @@ This scenario assumes the admin steps (`01-...`, `02-...`) have already run.
| Resource | Where | Purpose |
|---|---|---|
| `BackupJob/<bj-name>` | tenant namespace | Records the run; `status.phase=Succeeded`, `status.backupRef` points at the Backup |
| `Backup/<bj-name>` | tenant namespace | The restorable artifact reference (the actual data lives in S3 under the upstream tool's retention) |
| `Backup/<bj-name>` | tenant namespace | The restorable artifact reference; the data lives in S3 as the archive the Backup records. Nothing expires it: on `backup.useSystemBucket: true` deleting the `Backup` deletes the archive, and on the tenant's own bucket the archive stays until the tenant removes it, through the sidecar's `POST /backup/delete/remote/<name>` or the bucket's lifecycle rules |
| `batch/v1.Job/<bj-name>-backup` | tenant namespace | The completed strategy Pod |
+137
View File
@@ -0,0 +1,137 @@
#!/usr/bin/env bats
# Behavioural tests for the example ClickHouse (Altinity) strategy that
# examples/backups/clickhouse/01-create-strategy.sh applies. The script runs
# against a stubbed kubectl, which captures the Strategy it applies; the
# in-Pod script of that Strategy is then rendered the way the backup
# controller renders it and EXECUTED against a stubbed clickhouse-backup
# sidecar API.
#
# The contract pinned here is which archive the example touches: the one the
# controller named for the run (.ArchiveName), so that the Backup records the
# archive the run wrote, its restore reads that archive back and deleting it
# removes that archive. The name the script mints from the clock, and the
# newest archive of the source application, are only the fallbacks for a run
# the controller gave no name.
load test_helper
EXAMPLE=examples/backups/clickhouse/01-create-strategy.sh
# Capture the Strategy the example applies into $1, with kubectl stubbed so
# no cluster is touched: `get` answers with a client image, so the example
# does not fall back to installing tools at run time, and `apply -f -` writes
# its manifest.
capture_strategy() {
out=$1
stub=$(mktemp -d)
cat > "$stub/kubectl" <<STUB
#!/bin/bash
case "\$1" in
get) echo "client-image:test" ;;
apply) cat > "$out" ;;
esac
STUB
chmod +x "$stub/kubectl"
PATH="$stub:$PATH" bash "$EXAMPLE" >/dev/null 2>&1
[ -s "$out" ] || return 1
}
# Render the captured Strategy's in-Pod script into $2 as the controller
# would: $3 mode, $4 the application (release and source), $5 the archive
# name the controller chose or the Backup recorded (empty for none).
render_example() {
manifest=$1; out=$2; mode=$3; app=$4; archive=${5-}
yq eval '.spec.template.spec.containers[0].args[0]' "$manifest" \
| sed -e "s#{{ .Mode }}#${mode}#g" \
-e "s#{{ .Release.Name }}#${app}#g" \
-e "s#{{ .Release.Namespace }}#tenant-x#g" \
-e "s#{{ .Backup.ApplicationRef.Name }}#${app}#g" \
-e "s#{{ .ArchiveName | default \"\" }}#${archive}#g" > "$out"
[ -s "$out" ] || return 1
if grep -q '{{' "$out"; then
echo "rendered script still holds a controller placeholder" >&2
return 1
fi
}
# A curl stub standing in for the sidecar, first on PATH next to a no-op
# sleep. Every request is logged to $2/requests; an accepted POST makes the
# action log report that operation as succeeded; list/remote lists two
# archives of app "ch", the newer one second.
make_sidecar() {
bin=$1; state=$2
mkdir -p "$bin" "$state"
cat > "$bin/curl" <<STUB
#!/bin/bash
state="$state"
method=GET
url=
prev=
for a in "\$@"; do
if [ "\$prev" = "-X" ]; then method=\$a; fi
case "\$a" in http://*) url=\$a ;; esac
prev=\$a
done
echo "\$method \$url" >> "\$state/requests"
case "\$method \$url" in
"POST "*create_remote\?name=*) name=\${url#*name=}; echo "create_remote \$name" > "\$state/op" ;;
"POST "*/restore_remote/*) echo "restore_remote \${url##*/}" > "\$state/op" ;;
"GET "*/backup/actions) [ -s "\$state/op" ] && printf '{"command":"%s","status":"success","start":"2026-01-01 00:00:00"}\n' "\$(cat "\$state/op")" ;;
"GET "*/backup/list/remote)
printf '{"name":"clickhouse-ch-20260101T000000Z","created":"2026-01-01 00:00:00"}\n'
printf '{"name":"clickhouse-ch-20260102T000000Z","created":"2026-01-02 00:00:00"}\n'
;;
esac
exit 0
STUB
printf '#!/bin/sh\nexit 0\n' > "$bin/sleep"
chmod +x "$bin/curl" "$bin/sleep"
}
setup() {
strict_setup
work=$(mktemp -d)
capture_strategy "$work/strategy.yaml"
make_sidecar "$work/bin" "$work/state"
}
run_example() {
render_example "$work/strategy.yaml" "$work/script.sh" "$1" ch "${2-}"
API_USERNAME=u API_PASSWORD=p PATH="$work/bin:$PATH" bash "$work/script.sh"
}
@test "backup uploads under the archive name the controller chose" {
run_example backup ch-20260304050607-0f3c9a1e5b7d
if ! grep -qx 'POST http://chi-clickhouse-ch-clickhouse-0-0.tenant-x.svc:7171/backup/create_remote?name=ch-20260304050607-0f3c9a1e5b7d' "$work/state/requests"; then
echo "backup did not upload under the controller's name:" >&2
cat "$work/state/requests" >&2
exit 1
fi
}
@test "backup without a controller name falls back to a name from the clock" {
run_example backup ""
if ! grep -qE '^POST .*/backup/create_remote\?name=clickhouse-ch-[0-9]{8}T[0-9]{6}Z$' "$work/state/requests"; then
echo "backup without a name did not mint one from the clock:" >&2
cat "$work/state/requests" >&2
exit 1
fi
}
@test "restore reads back the archive the Backup recorded, not the newest one" {
run_example restore clickhouse-ch-20260101T000000Z
if ! grep -qx 'POST http://chi-clickhouse-ch-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-ch-20260101T000000Z' "$work/state/requests"; then
echo "restore did not read the recorded archive:" >&2
cat "$work/state/requests" >&2
exit 1
fi
}
@test "restore of a Backup that records no archive takes the newest one" {
run_example restore ""
if ! grep -qx 'POST http://chi-clickhouse-ch-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-ch-20260102T000000Z' "$work/state/requests"; then
echo "restore without a recorded archive did not take the newest one:" >&2
cat "$work/state/requests" >&2
exit 1
fi
}