Files
cozystack/packages
Aleksei Sviridkin 8f0082a1c8 fix(clickhouse): delete the archive when its Backup is deleted (#5224)
<!-- Thank you for making a contribution! Here are some tips for you:
- Use Conventional Commits for the PR title: `type(scope): description`
- Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore
- Scopes are not an exhaustive list — pick the most specific scope for
the change and extend the list when a genuinely new area appears.
Examples:
- System components: dashboard, platform, operator, cilium, kube-ovn,
linstor, fluxcd, cluster-api
- Managed apps: postgres, mariadb, redis, kafka, clickhouse,
virtual-machine, kubernetes
- Development and maintenance: api, hack, tests, ci, docs, maintenance
- Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or
add a `BREAKING CHANGE:` footer
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats,
even if it's still a draft.
- Add the label `kind/backport` if it's a bugfix that needs to be
backported to a previous version.
-->

## What this PR does

Builds on #5229 (merged), which gives every backup run an archive of its
own.

Deleting a `Backup` taken with the Altinity (ClickHouse) strategy
released its finalizer at once and left the archive in the bucket. On
the system bucket nothing else prunes it, so every ClickHouse backup
stayed until someone called the sidecar by hand, and the route the chart
and the examples documented for that, `DELETE /backup/<name>/remote`,
does not exist in clickhouse-backup 2.7 and answers 404.

The driver names each archive `<application>-<start time as
YYYYMMDDHHMMSS>-<first 12 hex digits of the BackupJob UID>` (#5229) and
records it on the `Backup` under
`altinity.strategy.backups.cozystack.io/archive`. This PR also records,
under `altinity.strategy.backups.cozystack.io/use-system-bucket`, the
application's `backup.useSystemBucket` as it read when the backup Job
was created: the setting is stamped on the backup Job and copied onto
the `Backup`, because the application may switch before the controller
sees the Job complete. A Job created before the stamp existed falls back
to the application as it reads at completion. For a system-bucket
application the bucket the projected credentials (`cozy-backups-creds`)
name is recorded the same way, under
`altinity.strategy.backups.cozystack.io/bucket`. The `Backup` is created
with its cleanup finalizer already set, so a `Backup` deleted before the
`Backup` reconciler first sees it still has its archive deleted.

On `backup.useSystemBucket: true`, deleting the `Backup` runs a one-shot
cleanup Job that calls `POST /backup/delete/remote/<name>` on the
application's sidecar and holds the finalizer until it completes. The
sidecar refuses a delete with 423 while any other operation runs and
cannot be reached while its Pod restarts, so the Job waits both out
within its deadline; an archive that is already gone counts as deleted,
and any other answer fails the Job, which is reported as an
`ArtifactCleanupFailed` Warning Event carrying the reason the Job failed
and retried after a wait that starts at 30 seconds and doubles with each
failure up to 30 minutes (kept on the `Backup` under
`altinity.strategy.backups.cozystack.io/cleanup-failures` and
`.../cleanup-retry-after`, since the failed Job is deleted to make room
for the next). The Job starts only once the projected credentials have
reached the sidecar, as the backup and restore Jobs do. It is built by
the controller, not rendered as a whole from the strategy template,
because the shipped and the example templates run `restore_remote` for
every mode other than `backup`; only the image, how to pull it and where
the Pod may be scheduled (node selector, tolerations, affinity, priority
class) come from the template, rendered as for the strategy's own Pods.

When the delete cannot or must not run, the `Backup` is released with an
`ArtifactNotDeleted` Warning Event naming the archive left behind: the
namespace is terminating, the strategy, the application or its API
Secret is gone, the application no longer uses the system bucket (also
when it leaves while the delete runs), the system bucket is no longer
the one the `Backup` records or cannot be read (also when it changes
while the delete runs; a sidecar on another bucket would answer "not
found" and the delete would count as done), another Job holds the
cleanup Job name, or the archive has the older
`clickhouse-<application>-<YYYYMMDDTHHMMSSZ>` form, without its
BackupJob's UID. Such a name comes from a controller that predates
per-run names; two runs of one application started within the same
second share it, and a run still in flight when the controller is
upgraded records it later, so it is never deleted.
`backups.cozystack.io/skip-artifact-cleanup: "true"` releases a `Backup`
without deleting anything. An application on its own bucket keeps the
archive's lifecycle, as with MongoDB. The wrong delete route is
corrected in the chart comments and the restore scenario.

A BackupJob or RestoreJob no longer takes the outcome of a batch Job of
its name that it does not control. A Job left by an earlier owner of the
same name is waited out, with the `Ready` condition
(`PredecessorJobPending`) naming it, until the garbage collector removes
it; any other Job holding the name fails the BackupJob or RestoreJob
with a message naming the Job. The condition is dropped once the run's
own Job starts.

The example strategy under `examples/backups/clickhouse` now uploads
under the archive name the controller chose and restores the archive the
`Backup` records, as the shipped strategy does, keeping its clock-made
name and the newest archive only as the fallback for a run given no
name; `hack/clickhouse-example-strategy.bats` runs it against a stubbed
sidecar. The backup scenario no longer says the archive is expired by
the upstream tool's retention, which the chart does not configure.

Known limitations: a `Backup` taken before #5208, or by a custom
strategy that does not use `.ArchiveName`, does not name its archive and
keeps it; a `Backup` that names its archive but not the bucket setting,
as every `Backup` taken before this change does (a 1.7.x release that
carries #5208 among them), keeps its archive too and is released without
an Event; a `Backup` that records no bucket, taken before buckets were
recorded, is deleted without the bucket check; a `Backup` deleted during
an application uninstall can lose its cleanup Job to the chart's own
`<release>-cleanup` hook and is then released with a Warning Event,
leaving the archive; the recorded setting and bucket are read when the
backup Job is created, not observed from the upload, so a switch made
just before a backup, while the sidecar still runs on the previous
bucket, is recorded for an archive written to the previous one.

Related gap outside this PR, in main today: an archive uploaded by a run
whose `Backup` name is already taken is recorded by nothing, so no
`Backup` deletes it.

Verified with unit tests that drive the cleanup path through a fake
client and the cleanup script against a stub `curl`, with mutation
checks on every guard, and on a test cluster running v1.7.0 with the
backup controllers built from this branch (before #5229 shortened the
run name to its current form, which leaves the deletion side unchanged),
against the real clickhouse-backup sidecar and SeaweedFS: a backup
records a per-run archive name and the bucket setting on a `Backup`
created with its finalizer; deleting the `Backup` deletes the archive's
objects in the bucket; a restore from a `Backup` brings the data back; a
BackupJob recreated under a used name waits for the earlier Job and then
writes an archive of its own; a Job of that name created by anything
else fails the BackupJob; a `Backup` recording an archive named without
a UID is released with `ArtifactNotDeleted` and its objects stay. The
changes made after that run (the recorded bucket, the retry wait, the
credentials wait before the cleanup Job, the rendered cleanup Pod
settings, the cleared predecessor condition, the example strategy) are
covered by the unit tests and bats, not by a live run.

### Screenshots

<!-- REQUIRED for UI changes: attach screenshots or screen recordings
demonstrating
the visual impact of your changes. PRs with UI changes without
screenshots will not be merged. -->

### Downstream repositories

<!--
Cozystack is upstream for repositories that are NOT kept in sync with it
automatically, and nothing in CI compares the two sides, so a change
here can break them silently.

Which change forces what, and which file to touch, is the trigger map in
docs/agents/contributing.md under "Downstream Repositories". Walk it
before you tick anything.

IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM
TO FILL IN:

1. Walk the trigger map against the actual diff of this PR, file by
file. Do not decide from the PR title or from your memory of what you
changed.
2. For every repository you tick, open the follow-up PR there and link
it on the same line. A ticked box with no link claims work that does not
exist, which is worse than an empty box. Search that repository for an
open PR or issue covering it first, and link that instead of filing a
duplicate.
3. When the follow-up is out of scope, or needs a decision that is not
yours to make, open an issue in that repository instead, link that, and
say so in the PR body. Do not push a speculative PR into another
repository just to fill a line here.
4. Do not tick "No downstream repository is affected" to make the
checklist go away. If you are unsure, leave every box empty and say so
in the PR body, so a human decides.
-->

- [ ] No downstream repository is affected by this change
- [x] [cozystack/website](https://github.com/cozystack/website) -
follow-up: cozystack/website#809 (for cozystack/website#808; its `next/`
pages should merge after this PR)
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

<!--  Write a release note:
- Explain what has changed internally and for users.
- Start with the same `type(scope):` prefix as in the PR title
- Follow the guidelines at
https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md.
-->

```release-note
fix(clickhouse): deleting a Backup taken on the system bucket from this release on now deletes its clickhouse-backup archive and keeps the Backup until the delete finished, reporting an archive it cannot or must not delete (including one named without its BackupJob's UID, or in a system bucket the platform has left) as a Warning Event and retrying a failed delete with a growing wait; the cleanup Pod is scheduled as the strategy's own Pods; a BackupJob or RestoreJob no longer reports the outcome of a batch Job it did not create; the documented sidecar delete route is corrected to POST /backup/delete/remote/<name>.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* ClickHouse backups using the system bucket now remove their recorded
archive when the backup is deleted. Deletion waits for cleanup to
finish.
* Backup and restore operations use the controller-provided archive name
when available. Restores can fall back to the latest matching archive.
* **Bug Fixes**
* Temporary sidecar failures are retried with increasing delays. If
cleanup cannot safely remove an archive, it completes with a warning
rather than risking deletion of the wrong archive.
* **Documentation**
* Clarified that archives in tenant-owned buckets remain until manually
deleted or removed by bucket lifecycle rules.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-23 16:27:05 +03:00
..