## What this PR does
On a cluster running v1.6.5 a tenant namespace that was deleted
directly, not through the Tenant app, stayed in `Terminating` for about
28 hours, held by the Kamaji finalizers described in #5002. Nothing
alerted on the namespace itself. The only signal was
`HelmReleaseNotReady` of the tenant release, which kept flapping.
kube-state-metrics already exports `kube_namespace_status_phase` and
`dashboards/main/capacity-planning.json` reads it, but no alert rule in
the repo does.
This adds `KubeNamespaceStuckTerminating`, a warning in a new
`namespace-deletion` group under
`packages/system/monitoring-agents/alerts/`. It fires when `max by
(cluster, namespace)
(kube_namespace_status_phase{job="kube-state-metrics",
phase="Terminating"}) == 1` has held for 30 minutes. kube-state-metrics
exports a `Terminating` series for every namespace, 0 while the
namespace is active and 1 while it is being deleted
([namespace.go#L109-L135](https://github.com/kubernetes/kube-state-metrics/blob/v2.15.0/internal/store/namespace.go#L109-L135)).
The scrape adds the node and pod of the kube-state-metrics pod as target
labels, so the rule groups by cluster and namespace and its timer keeps
running through a kube-state-metrics rollout to another node. A gap in
the series, for example while kube-state-metrics is down, starts the 30
minutes over. The description names the namespace and the cluster and
points at the namespace status conditions, which list the remaining
resource types and finalizers
([status_condition_utils.go#L101-L140](https://github.com/kubernetes/kubernetes/blob/v1.33.12/pkg/controller/namespace/deletion/status_condition_utils.go#L101-L140)).
Why 30 minutes. Healthy tenant deletions we watched on current versions
took under a minute from the delete request until the namespace was
gone, 61 of 61, and about 5 minutes for a tenant with a Kubernetes
cluster, a VM and Postgres inside. The namespace itself spent under a
minute in `Terminating` in every one of them. 30 minutes leaves wide
room and still reports a stuck namespace within the hour.
`hack/namespace-deletion-alerts.bats` replays `.spec` of the VMRule
through `vmalert-tool` v1.136.0 pinned by digest, the same harness as
#4963, #4985 and #4993.
`hack/testdata/namespace-deletion-alerts/unittest.yaml` has 5 cases with
11 checks. A namespace in `Terminating` is quiet at 29 minutes and fires
at 35, with labels and annotations pinned. A namespace deleted after 20
minutes in `Terminating` stays quiet at 19, 35 and 60 minutes, and an
active namespace stays quiet. When kube-state-metrics moves to another
node 15 minutes in, the alert still fires at 31 minutes. The same
namespace name in two clusters gives one alert per cluster, each with
its own timer.
```console
$ bats hack/namespace-deletion-alerts.bats
1..1
ok 1 namespace deletion alerts pass their vmalert-tool cases
```
With the expression replaced by `vector(1) == 0` the suite prints `not
ok 1` and vmalert-tool fails the 4 checks that expect an alert. On main,
where the rule file does not exist, the suite fails too. Each of the
following rule changes fails at least one check. They are `for: 15m`,
`for: 45m`, no `== 1`, `sum by` instead of `max by`, which only the
kube-state-metrics move catches, no `cluster` in the grouping,
`phase="Active"`, no aggregation, another severity and another
description.
With Bats 1.14.0 at the commit CI pins and Helm 3.17.3 on `PATH`, 18
suites pass with 325 of 325 tests, the new one together with
`alert-severity-contract`, `bats-strict-setup`, `bats-runner-coverage`,
`bats-runner-contract`, `unit-test-parallelism`, `image-pin-consistency`
and `check-commit-trailers` among others. The severity contract lists
the new alert as `warning`. helm-unittest 1.1.2 passes the
monitoring-agents chart with 19 of 19 tests, and `helm template` renders
the VMRule the same as the file. Like the migration suites, the new
suite needs docker. I have not run the rule in vmalert on a live
cluster.
The release-1.6 backport is a separate PR with one more commit that
drops the `load test_helper` line, because release-1.6 runs
`hack/*.bats` through `hack/cozytest.sh`.
### Screenshots
Not a UI change.
### Downstream repositories
A new alert file under `packages/system/monitoring-agents/alerts/`, a
Bats suite under `hack/` and its test data. No values, schema, README,
image or `ApplicationDefinition` change, and the website does not list
platform alerts by name, so no trigger map 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
```release-note
feat(monitoring): new warning alert KubeNamespaceStuckTerminating fires when a namespace has been in phase Terminating for more than 30 minutes.
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
- **New Features**
- Added an alert for Kubernetes namespaces that remain in the
`Terminating` phase for more than 30 minutes.
- Alerts identify the affected cluster and namespace and note that
remaining finalizers may be preventing deletion.
- **Tests**
- Added coverage for deletion timing, active namespaces, node changes
during termination, and identically named namespaces across clusters.
Test cases verify expected alert labels and annotations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->