test(clickhouse): delete the system-bucket Backup while its S3 host resolves

Deleting a system-bucket Backup now deletes its archive through the
clickhouse-backup sidecar, and the Backup is held until that delete is
answered. The delete reaches S3 at s3.example.org only through the
CoreDNS rewrite and the Ingress certificate the round-trip installs,
and the script's EXIT trap removes both. The round-trip therefore
deletes its Backup inside the script, before the trap runs, and waits
for the cleanup; the preconditions and the step's finally mark a
Backup they still find with skip-artifact-cleanup, since no S3 host is
mapped by then. The wait covers a cleanup Job that runs out its 600s
deadline, the 30s retry delay and a second Job, so the route is not
removed while a retried delete still needs it.

The test then checks the delete itself by listing the objects under
the archive's prefix in the system bucket, through the S3 Ingress with
the projected credentials and with folder entries left out: some before
the delete, none once the Backup is gone. clickhouse-backup's
list/remote is not used for this: it reports an archive for every
prefix at the top of the bucket, and SeaweedFS was seen to keep the
folders the delete emptied for minutes after all their objects were
gone.

Co-authored-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
Assisted-by: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
This commit is contained in:
Andrey KolkovandYan Bondarenko authored and Aleksei Sviridkin committed 2026-11-23 12:15:07 +03:00
1 parent a2f7ea18ee
commit 4fb8ea40e2
3 files changed
+80 -6

No files matched your search

@@ -465,9 +465,13 @@ spec:
# Fixed names in a persistent namespace: a leftover Succeeded job
# would satisfy the waits in the run step without a new attempt, and
# a leftover Backup of the BackupJob's name, recording an earlier
# run's archive, fails the new BackupJob.
# run's archive, fails the new BackupJob. Its archive delete would go
# to an S3 host nothing maps yet, so it is skipped.
kubectl -n "$NAMESPACE" delete restorejob sysbucket-restore --ignore-not-found --wait=true --timeout=30s
kubectl -n "$NAMESPACE" delete backupjob sysbucket-backup --ignore-not-found --wait=true --timeout=30s
if kubectl -n "$NAMESPACE" get backup sysbucket-backup -o name >/dev/null 2>&1; then
kubectl -n "$NAMESPACE" annotate backup sysbucket-backup backups.cozystack.io/skip-artifact-cleanup=true --overwrite
fi
kubectl -n "$NAMESPACE" delete backup sysbucket-backup --ignore-not-found --wait=true --timeout=60s
kubectl -n "$NAMESPACE" delete secret cozy-backups-creds --ignore-not-found --wait=true --timeout=30s
# Chainsaw reclaims only what its apply created, so objects a killed
@@ -558,12 +562,15 @@ spec:
# 480s BackupJob to Succeeded, including the driver's 2m hold
# 480s RestoreJob to Succeeded; no hold by then, the stamp is
# already older than the backup's own 2m wait
# 30s listing the archive's objects before the delete
# 1260s Backup delete, including a second cleanup Job after a timeout
# 30s listing them again after it
# 90s the EXIT trap: Corefile replace plus a 60s Secret delete
# 1330s (22m10s)
# 25m leaves 2m50s for the SQL and kubectl calls between those waits, so
# 2650s (44m10s)
# 47m leaves 2m50s for the SQL and kubectl calls between those waits, so
# an inner wait is what fires and names what stalled.
- script:
timeout: 25m
timeout: 47m
content: |
set -eu
cd ../../..
@@ -596,5 +603,10 @@ spec:
kubectl -n "$NAMESPACE" delete restorejob sysbucket-restore --ignore-not-found --wait=true --timeout=30s
kubectl -n "$NAMESPACE" delete backupjob sysbucket-backup --ignore-not-found --wait=true --timeout=30s
# The driver names the Backup after the BackupJob; by name it is
# found even when status.backupRef was never written.
# found even when status.backupRef was never written. A Backup still
# here means the script failed, and its archive delete would now go
# to an S3 host the restore above unmapped, so it is skipped.
if kubectl -n "$NAMESPACE" get backup sysbucket-backup -o name >/dev/null 2>&1; then
kubectl -n "$NAMESPACE" annotate backup sysbucket-backup backups.cozystack.io/skip-artifact-cleanup=true --overwrite
fi
kubectl -n "$NAMESPACE" delete backup sysbucket-backup --ignore-not-found --wait=true --timeout=60s
@@ -39,6 +39,9 @@ PROBE_TIMEOUT=240
# Each covers the driver's 2m hold after the credentials change plus the
# strategy Job itself.
JOB_TIMEOUT=480
# Two 600s cleanup Jobs, the 30s retry delay and 30s to observe completion.
BACKUP_DELETE_TIMEOUT=1260
ARCHIVE_KEY=altinity.strategy.backups.cozystack.io/archive
workdir=$(mktemp -d)
@@ -172,3 +175,61 @@ sidecar_after=$(sidecar_identity)
[ "$sidecar_after" = "$sidecar_before" ] \
|| { log_error "the ClickHouse Pod or its clickhouse-backup container restarted during the round-trip (uid restarts: '${sidecar_before}' then '${sidecar_after}')"; exit 1; }
log_success "System-bucket round-trip verified: sentinel row ${sentinel} restored"
# Prints the keys of the objects under prefix in the system bucket, one per
# line, folder entries left out. The objects are read rather than
# clickhouse-backup's list/remote, which reports an archive for every prefix
# left in the bucket, and SeaweedFS can keep the folders a delete empties. The
# credentials go over stdin to stay out of the probe's argv.
list_archive_objects() {
local prefix=$1 creds out
creds=$(kubectl -n "$NAMESPACE" get secret cozy-backups-creds -o json \
| jq -r '.data | [.accessKey, .secretKey, .bucketName, .endpoint, .region] | map(. // "" | @base64d) | @tsv')
kubectl -n "$NAMESPACE" get secret "$CERT_SECRET" -o jsonpath='{.data.ca\.crt}' | base64 -d \
| timeout 30 kubectl -n "$NAMESPACE" exec -i "$PROBE_POD" -- sh -c 'cat >/tmp/ca.crt'
# An empty region means us-east-1 to S3 clients, and SeaweedFS accepts a
# signature for any region.
# shellcheck disable=SC2016 # expanded by the probe's shell
if ! out=$(printf '%s\n' "$creds" | timeout 30 kubectl -n "$NAMESPACE" exec -i "$PROBE_POD" -- sh -c '
IFS="$(printf "\t")" read -r ak sk bucket endpoint region
[ -n "$bucket" ] && [ -n "$endpoint" ] \
|| { echo "cozy-backups-creds names no bucketName or no endpoint" >&2; exit 1; }
curl -fsS --max-time 20 --cacert /tmp/ca.crt --user "${ak}:${sk}" \
--aws-sigv4 "aws:amz:${region:-us-east-1}:s3" --get \
--data-urlencode list-type=2 --data-urlencode "prefix=$1" \
"https://${endpoint}/${bucket}"' sh "$prefix" 2>"$workdir/list.err"); then
log_error "could not list ${prefix} in the system bucket: $(cat "$workdir/list.err")"
return 1
fi
printf '%s' "$out" | grep -o '<Key>[^<]*</Key>' | sed -e 's|<Key>||' -e 's|</Key>||' | grep -v '/$' || true
}
# The archive lives in the system bucket only while the Backup does: deleting
# the Backup has to delete it, and the delete goes to S3 through the rewrite
# and certificate the EXIT trap removes, so it runs before the trap.
archive=$(kubectl -n "$NAMESPACE" get backup "$backup_ref" -o json | jq -r --arg k "$ARCHIVE_KEY" '.spec.driverMetadata[$k] // ""')
[ -n "$archive" ] || { log_error "Backup ${backup_ref} records no archive under ${ARCHIVE_KEY}"; exit 1; }
s3_path=$(kubectl -n "$NAMESPACE" get pod "chi-clickhouse-${CLICKHOUSE_NAME}-clickhouse-0-0-0" \
-o jsonpath='{.spec.containers[?(@.name=="clickhouse-backup")].env[?(@.name=="S3_PATH")].value}')
[ -n "$s3_path" ] || { log_error "the clickhouse-backup container sets no S3_PATH"; exit 1; }
archive_prefix="${s3_path%/}/${archive}/"
list_archive_objects "$archive_prefix" >"$workdir/before.txt"
[ -s "$workdir/before.txt" ] \
|| { log_error "the system bucket holds no object under ${archive_prefix}, the archive of Backup ${backup_ref}"; exit 1; }
log_step "Deleting Backup ${backup_ref} and its archive ${archive}"
if ! kubectl -n "$NAMESPACE" delete backup "$backup_ref" --wait=true --timeout="${BACKUP_DELETE_TIMEOUT}s"; then
log_error "Backup ${backup_ref} was not deleted within ${BACKUP_DELETE_TIMEOUT}s"
kubectl -n "$NAMESPACE" describe backup "$backup_ref" >&2 || true
kubectl -n "$NAMESPACE" logs "job/${backup_ref}-cleanup" --all-containers >&2 || true
exit 1
fi
# The finalizer holds the Backup until the cleanup Job saw the delete
# answered, so the objects are gone by the time the Backup is.
list_archive_objects "$archive_prefix" >"$workdir/after.txt"
if [ -s "$workdir/after.txt" ]; then
log_error "Backup ${backup_ref} is gone but the system bucket still holds under ${archive_prefix}: $(tr '\n' ' ' <"$workdir/after.txt")"
kubectl -n "$NAMESPACE" get events --field-selector "involvedObject.name=${backup_ref}" >&2 || true
exit 1
fi
log_success "Deleting Backup ${backup_ref} removed its archive ${archive} from the system bucket"
@@ -35,7 +35,8 @@ spec:
containers:
- name: probe
image: docker.io/alpine/k8s:1.36.2@sha256:44ef4942e171939b9c665a4a84beb80e2dcdb9a24330d4651cfdfd2e9deecc47
command: [sleep, "3600"]
# Outlives the step timeouts from here through the run step (68m30s).
command: [sleep, "4800"]
securityContext:
allowPrivilegeEscalation: false
capabilities: