Files
cozystack/examples/backups
Andrey Kolkov ef32ff8d71 fix(postgres): bound a backup whose cnpg Backup stalls in pending (#5320)
<!-- 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

Part of #5209. The other part, a Plan that does not start a run while
its previous BackupJob is still running, follows in a separate PR.

A Postgres BackupJob whose `cnpg.io/Backup` never left `pending` stayed
`Running` with no bound. In CloudNativePG 1.30.0 a Backup is pending for
one of two reasons: it is queued behind a started or running backup of
the same Cluster (`waitIfOtherBackupsRunning`), which is healthy, or the
instance to run it on cannot be found or is not ready
(`startBackupManagedByInstance`), which lasts for as long as the Cluster
is fenced. A restart on another instance goes through the second path
too.

The driver tells the two apart only by whether a backup of the Cluster
is `started` or `running`; it does not model which pending Backup
CloudNativePG lets through first. Each poll reads the Cluster's backups
from the cache, and the job reads them again from the apiserver right
before it gives up, since the cache can still show pending a backup that
has just started. Behind a started or running backup the wait stays
uncapped and the BackupJob says which backup it waits for. Otherwise the
BackupJob records a `CNPGBackupStalled` condition, and once that has
lasted the backup deadline (30 minutes, the same bound as the plugin
rollout wait) the BackupJob fails with the cause, and its own
`cnpg.io/Backup` is deleted so it does not run later with no Cozystack
`Backup` recording it. The delete is conditional on the UID and
resourceVersion the driver read, so a Backup CloudNativePG started in
between is kept. The give-up is recorded before the delete, so a Backup
found missing later fails the job only when the driver removed it; a
Backup deleted by hand is replaced and its stall counts afresh. The
stall also ends, and a later one counts afresh, as soon as the Backup
moves to any other phase.

The reason CloudNativePG gives (`BackupPending: Backup target pod not
ready: ...`) exists only as an event on the Backup, so the condition and
the failure message carry the newest Warning event recorded on the job's
own Backup, or, when it carries none, on another pending backup of the
same Cluster. The events are listed by the Backup's UID through the
uncached reader, so the controller gains `list` on events and no watch;
when the read fails, the message says so instead of omitting the reason.

A job is no longer held on the plugin rollout another job started when
the Cluster is fenced and every instance pod postdates that attach: a
fenced Cluster is neither healthy nor ready until the fence is lifted,
so waiting there would only delay the bound by up to the rollout
deadline. An instance pod older than the attach still holds the job, so
a Backup never starts on a pod without the plugin.

The postgres e2e (`examples/backups/postgres/run-all.sh` step 26) fences
the source Cluster, checks that a BackupJob reports the stall with the
reason CloudNativePG recorded, which exercises the `involvedObject.uid`
selector and the events grant against a real apiserver and replaces a
helm-unittest that only matched the rendered rule, then lifts the fence
and checks that the same job completes with the stall ended. Failing at
the deadline is covered by the unit tests.

Known limit, left as is: a backup of the Cluster in `finalizing` does
not count as running, which only a hand-made `volumeSnapshot` Backup can
reach, since platform flows create plugin backups only.

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

- [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(postgres): a Postgres BackupJob whose cnpg.io/Backup stays pending because its target instance is not ready, for example while the Cluster is fenced, now fails after 30 minutes with the reason CloudNativePG gives, and the abandoned cnpg.io/Backup is removed. A Backup queued behind a running backup of the same Cluster is not affected.
```


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

## Summary by CodeRabbit

* **Bug Fixes**
* Pending backups that stall for 30 minutes now fail with a recorded
reason. Backups queued behind an executing backup, progressing, or
finished are not incorrectly treated as stalled.
* Backup handling accounts for concurrent progress, external deletion,
and retries, and reports the newest available warning for a pending
backup.
* Fenced clusters now use instance-pod creation time when evaluating
plugin rollout readiness.

* **Tests**
* Expanded coverage for stalled backups, warning reporting, queue
handling, and fenced-cluster backup recovery.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-26 15:59:36 +07:00
..