Files
cozystack/packages
Andrey Kolkov 7f31011b99 fix(backups): delete the Velero backup of a failed BackupJob (#5364)
<!-- 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, security (a fix that comes through a security
advisory), 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.
- Fixes merged to `main` reach the release lines the backport batch runs
for automatically, so do not open backport PRs by hand:
https://github.com/cozystack/cozystack/blob/main/docs/release/backports.md
-->

## What this PR does

<!-- Link the issues this PR addresses. For a complete fix targeting the
default branch, use Closes #N (Closes owner/repository#N for another
repository). Otherwise use a non-closing reference; release-branch PRs
need manual issue linking and closure when appropriate. -->

Closes #5355

A BackupJob whose Velero backup ends `Failed` or `PartiallyFailed`
leaves that `backup.velero.io` object and its data in the backup bucket
(partial resources, logs) until Velero's TTL expires it, 720h by
default. No `Backup` is created for a failed run, and the `Backup`
finalizer was the only code that asked Velero to delete backup data;
deleting the BackupJob does not remove it either, because the Velero
backup is only labelled for the job, not owned by it.

The first commit requests deletion of the failed Velero backup through a
`DeleteBackupRequest`, and turns the reconcile of a `Failed` BackupJob,
which returned at once, into a sweep over the Velero backups still
labelled for the job. That sweep also cleans up backups leaked by
earlier versions after an upgrade. The request creation is shared with
the `Backup` finalizer and named after the Velero backup, so repeated
reconciles converge on one request. The failure message is unchanged.

The second commit narrows that sweep and makes it converge:

- Only `Failed`, `PartiallyFailed` and `FailedValidation` Velero backups
are deleted. A `Completed` one is never touched from a job-level
failure: a job can fail after Velero completed (creating or recording
the `Backup` failed), and that Velero backup may be the only copy of
good data.
- Deletion is requested only after `Failed` is persisted. The strategy
no longer sends the request inline, so a failed status write cannot
leave a retried run without the backup it reports the failure from. The
BackupJob watch has no predicate, so the `Failed` status update itself
triggers the sweep.
- While a failed backup still exists, the job is requeued every 5
minutes. Nothing watches `DeleteBackupRequest`, so this is how a request
Velero processed with errors is noticed and replaced. A backup that is
still running is rechecked every minute.
- Backups that reference the Velero backup are read through the uncached
API reader before anything is deleted.
- The ClusterRole of `backupstrategy-controller` gains `delete` on
`deletebackuprequests`, needed to replace a processed-with-errors
request. The `Backup` finalizer shares the helper, and without the verb
that path would fail with 403.

The third commit makes a deletion that Velero could not finish converge,
and makes it visible:

- A Velero backup in `Deleting` goes through the same request path.
Velero sets that phase before it removes the data and leaves it there
when removal fails, so skipping it left the data in place until the TTL.
- Our own request that Velero processed while the backup still exists is
replaced, whether it carries errors or not. The second case is a backup
that the BSL sync brought back.
- Each replacement emits a `VeleroBackupDeletionFailed` Warning event on
the BackupJob with Velero's `status.errors`, and doubles the wait before
the next replacement, 5 minutes up to 1 hour (enforced from the recorded
attempt time since the fourth commit). The count is kept in the
`backups.cozystack.io/velero-deletion-retries` annotation on the
BackupJob. A missing, read-only or unavailable BSL therefore shows up in
`kubectl describe` instead of being retried silently every 5 minutes.

The fourth commit makes the backoff binding rather than advisory:

- Each request the sweep creates records its time in the
`backups.cozystack.io/velero-deletion-attempted-at` annotation, next to
the retry counter. A request Velero has processed is replaced only once
the backoff for the current retry count has passed since that time.
Writing the annotations triggers a reconcile at once, because the
BackupJob watch has no predicate; that reconcile now only computes the
remaining wait. Before, with a BSL that fails instantly, it could
replace the request again right away.
- A request Velero has not processed yet, and a Velero backup still in
`New` or with no phase, are checked again after as long as they have
already waited: between 5 minutes (1 minute for a running backup) and 1
hour. Waiting emits no event.
- If the job was deleted before the annotations are written, the sweep
stops quietly. Any other error writing them is logged, and the computed
requeue is kept, because the request already exists.

Points for the reviewer:

- The sweep matches on the BackupJob labels, not on the strategy kind,
because a `Failed` job's BackupClass may be gone. The Kubernetes
strategy labels its Velero backup the same way and has the same leak on
failure, so a failed Kubernetes strategy run is cleaned up by the sweep
as well.
- The Velero strategy still treats a `FailedValidation` Velero backup as
in progress and requeues the BackupJob indefinitely. That is unchanged
here; the sweep already treats `FailedValidation` as deletable.
- After the upgrade, the Velero backups of BackupJobs that already
failed are deleted. An operator who wants one of them for a manual
`velero restore` must copy it first.

### 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. -->

N/A — no UI changes.

### 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.
-->

The diff touches Go code in `internal/backupcontroller/` and one chart
file, `packages/system/backupstrategy-controller/templates/rbac.yaml`,
which adds the `delete` verb on `deletebackuprequests` to the
controller's ClusterRole. Walked against the trigger map:

- website: no package, platform value, variant, component, release
asset, ApplicationDefinition, Talos or tooling change. The ClusterRole
is not documented there.
- terraform-provider-cozystack: no app, schema field, enum, default,
`kind`/`plural`, prefix, or `Package`/`Plan`/`RestoreJob` CRD change.
- ansible-cozystack, talm, examples: no installer value, namespace,
variant, platform value, node prerequisite, or object the role waits on.
- ccp, external-apps-example: nothing under `hack/`, no package layout,
`-rd` path, variant, `tags.yaml`, `ApplicationDefinition` CRD or
commit-convention change.
- cozyhr, cozy-proxy, cozystack-telemetry-server, community: none of
their annotations, labels, metrics, `hack/package.mk` targets, PR
template or commit convention change.

No entry applies.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[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(backups): a BackupJob whose Velero backup ends Failed, PartiallyFailed or FailedValidation now asks Velero to delete that backup and its data in the bucket, instead of leaving them until Velero's TTL expires. A Velero backup that completed is never deleted this way. If Velero cannot carry out the deletion (for example, the backup storage location is missing, read-only or unavailable), the BackupJob gets a `VeleroBackupDeletionFailed` Warning event with Velero's errors, and the deletion is retried at a growing interval of up to one hour. After upgrading, the Velero backups of BackupJobs that already failed are deleted as well; to keep one for a manual `velero restore`, copy it before the upgrade. The backupstrategy-controller ClusterRole gains `delete` on `deletebackuprequests`.
```
2026-11-20 01:07:55 +07:00
..
2026-11-18 13:38:20 +03:00