Files
cozystack/cmd/cozystack-controller
Aleksei Sviridkin fc618c1dcb feat(workloadmonitor): report failed Jobs that opt in as not operational (#5505)
## What this PR does

Refs #5459

A chart that runs workload-critical work in a Job outside its Helm
release has no way to report a failed run on the Application today:
`Ready` mirrors the HelmRelease, which is already applied, and
`WorkloadsReady` only looks at pods, PVCs, Services, BucketClaims and
DataVolumes. The immediate case is #5479, where the postgres collation
repair and PostGIS update move out of a blocking hook into a Job started
outside the release. If that Job fails (duplicates under a UNIQUE index,
a PostGIS update that does not go through), the indexes stay wrong while
the Application shows `Ready=True` and `WorkloadsReady=True`.

This PR implements the proposal in
https://github.com/cozystack/cozystack/issues/5459#issuecomment-7268007229
by adding a generic rule to the WorkloadMonitor controller: a `batch/v1`
Job in the monitor's namespace that carries the monitor's selector
labels plus `workloads.cozystack.io/report-failure: "true"`, and has
`Failed=True`, makes the monitor not operational, with reason
`JobFailed` and a message such as `Job postgres-x-collation-repair
failed: BackoffLimitExceeded: Job has reached the specified backoff
limit`. cozystack-api passes it through unchanged as
`WorkloadsReady=False` with that reason and message. The rule knows
nothing about postgres: a chart opts a Job in explicitly with that
label. A Job with only the selector labels, which every chart and every
operator inheriting labels from the workload may carry, never affects
the monitor. The postgres side (the labels on the repair Job, no
`ttlSecondsAfterFinished`) lands on #5479.

Behaviour:

- A Job that is deleted, or replaced by a run that does not fail, clears
the verdict. Of the Jobs one controller owns, only the newest counts, so
a CronJob's failed run that stays in its history after a later success
does not keep the monitor down.
- The message lists failed Jobs sorted by name, at most three, then `N
more Jobs failed`.
- Precedence: a DataVolume verdict comes first (unchanged; its stored
value is kept while DataVolumes cannot be read), then `JobFailed`, then
`BucketClaimNotReady`. `minReplicas` still sets `Operational=false`
without a reason, so a failed Job beside too few replicas reports
`JobFailed`.
- Jobs are watched and mapped to monitors the same way pods are. The
manager caches only Jobs that carry the opt-in label, with a transform
that keeps only metadata (labels, owners, timestamps) and conditions.
- RBAC: a `+kubebuilder:rbac` marker for `jobs.batch` get/list/watch
documents the need. The markers under `internal/controller` do not
generate YAML, and the cozystack-controller ClusterRole in
`packages/system/cozystack-controller/templates/rbac.yaml` already
grants get/list/watch on every resource, as it does for pods, PVCs,
BucketClaims and DataVolumes, so the chart needs no change.

No chart sets the opt-in label yet, so nothing reports until a chart
adopts it. The postgres chart sets the selector labels in the Cluster's
`inheritedMetadata`, so CNPG's initdb and join Jobs carry them; without
the opt-in they are ignored, as are backup CronJob runs and any other
labelled Job.

### Testing

- `go build ./...`, `go vet` on `./internal/controller/`,
`./cmd/cozystack-controller/`, `./api/v1alpha1/`: clean.
- `go test ./internal/controller/... ./pkg/registry/apps/application/...
./api/...`: pass.
- New unit tests (fake client): a failed Job sets `Operational=false`,
reason `JobFailed` and the message; a succeeded, running, `Failed=False`
or other-selector Job, and a failed Job without the opt-in label or with
it set to anything but `"true"`, has no effect; a failed Job deleted or
replaced by a successful run clears the verdict; a stale `JobFailed` is
cleared before the DataVolume reader is installed; precedence against
`minReplicas`, an unprovisioned BucketClaim and a DataVolume verdict;
message sorting, the three-Job bound, newest-run-per-controller and Jobs
being deleted; the cache transform keeps what the rule reads.
cozystack-api: a monitor with `JobFailed` yields `WorkloadsReady=False`
with that reason and message.
- `make generate`: no changes.
- Not run against a cluster: the Job watch and the transformed cache
were not exercised end to end.

### Downstream repositories

Walked the trigger map against the diff
(`api/v1alpha1/workloadmonitor_types.go` constant only, no CRD schema
change; `internal/controller`; `cmd/cozystack-controller`; tests). No
package, platform values, ApplicationDefinition, installer, hack
tooling, telemetry or commit-convention change.

- [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

```release-note
feat(workloadmonitor): a failed Job that carries a WorkloadMonitor's selector labels and `workloads.cozystack.io/report-failure: "true"` now makes the workload not operational, so the Application reports `WorkloadsReady=False` with reason `JobFailed` and a message naming the Job and its failure. Deleting the Job, or a later run that does not fail, clears it.
```

(cherry picked from commit 23b94d6d1a)
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
2026-12-01 10:40:08 +07:00
..