feat(monitoring): alert when a namespace stays in Terminating (#5040)

## 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 -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-27 03:40:22 +03:00
commit 6aa33e379e
3 files changed
+209

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bats
load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
VMALERT_TOOL_IMAGE="victoriametrics/vmalert-tool:v1.136.0@sha256:417346d7b6fdb66d8e3984887c2ad114535ec7d983eeef53381b56c776f333f1"
@test "namespace deletion alerts pass their vmalert-tool cases" {
docker info >/dev/null 2>&1 || {
echo "docker is required to run the cases in $VMALERT_TOOL_IMAGE" >&2
return 1
}
work=$(mktemp -d)
rc=0
yq '.spec' "$REPO_ROOT/packages/system/monitoring-agents/alerts/namespace-deletion.yaml" > "$work/rules.yaml" || rc=$?
cp "$REPO_ROOT/hack/testdata/namespace-deletion-alerts/unittest.yaml" "$work/unittest.yaml" || rc=$?
if [ "$rc" -eq 0 ]; then
docker run --rm --network none -v "$work:/w:ro" "$VMALERT_TOOL_IMAGE" \
unittest --files /w/unittest.yaml > "$work/out" 2>&1 || rc=$?
cat "$work/out"
fi
passed=$(grep -cx 'SUCCESS' "$work/out" 2>/dev/null || true)
rm -rf "$work"
[ "$rc" -eq 0 ]
[ "$passed" -eq 1 ]
}
+160
View File
@@ -0,0 +1,160 @@
rule_files:
- rules.yaml
evaluation_interval: 15s
tests:
- name: a namespace that stays in Terminating
interval: 1m
input_series:
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '0x60'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '1x60'
alert_rule_test:
- eval_time: 29m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- eval_time: 35m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
exp_alerts:
- exp_labels:
cluster: cozystack
namespace: tenant-a
severity: warning
exported_instance: cozystack/tenant-a
service: namespace-deletion
exp_annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace tenant-a in cluster cozystack has been in phase Terminating for more
than 30 minutes. Usually an object inside it still carries a finalizer. The
conditions in the namespace status list the remaining resource types and
finalizers.
- name: a namespace deleted after twenty minutes in Terminating
interval: 1m
input_series:
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '0x19 stale'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '1x19 stale'
alert_rule_test:
- eval_time: 19m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- eval_time: 35m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- eval_time: 60m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- name: an active namespace
interval: 1m
input_series:
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '1x120'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '0x120'
alert_rule_test:
- eval_time: 35m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- eval_time: 120m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- name: kube-state-metrics moves to another node while the namespace terminates
interval: 1m
input_series:
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-a", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '0x19 stale'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-a", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '1x19 stale'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-b", pod="kube-state-metrics-7d4b9c6f8-w5r8t"}'
values: '_x15 0x45'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-b", pod="kube-state-metrics-7d4b9c6f8-w5r8t"}'
values: '_x15 1x45'
alert_rule_test:
- eval_time: 29m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
- eval_time: 31m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
exp_alerts:
- exp_labels:
cluster: cozystack
namespace: tenant-a
severity: warning
exported_instance: cozystack/tenant-a
service: namespace-deletion
exp_annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace tenant-a in cluster cozystack has been in phase Terminating for more
than 30 minutes. Usually an object inside it still carries a finalizer. The
conditions in the namespace status list the remaining resource types and
finalizers.
- name: namespaces with one name in two clusters
interval: 1m
input_series:
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Active", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '0x60'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", phase="Terminating", node="node-c", pod="kube-state-metrics-7d4b9c6f8-k2x9p"}'
values: '1x60'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="kubernetes-example", namespace="tenant-a", phase="Active", node="node-a", pod="kube-state-metrics-5c8f6d9b7-q4m7z"}'
values: '1x19 0x40'
- series: 'kube_namespace_status_phase{job="kube-state-metrics", cluster="kubernetes-example", namespace="tenant-a", phase="Terminating", node="node-a", pod="kube-state-metrics-5c8f6d9b7-q4m7z"}'
values: '0x19 1x40'
alert_rule_test:
- eval_time: 35m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
exp_alerts:
- exp_labels:
cluster: cozystack
namespace: tenant-a
severity: warning
exported_instance: cozystack/tenant-a
service: namespace-deletion
exp_annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace tenant-a in cluster cozystack has been in phase Terminating for more
than 30 minutes. Usually an object inside it still carries a finalizer. The
conditions in the namespace status list the remaining resource types and
finalizers.
- eval_time: 51m
groupname: namespace-deletion
alertname: KubeNamespaceStuckTerminating
exp_alerts:
- exp_labels:
cluster: cozystack
namespace: tenant-a
severity: warning
exported_instance: cozystack/tenant-a
service: namespace-deletion
exp_annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace tenant-a in cluster cozystack has been in phase Terminating for more
than 30 minutes. Usually an object inside it still carries a finalizer. The
conditions in the namespace status list the remaining resource types and
finalizers.
- exp_labels:
cluster: kubernetes-example
namespace: tenant-a
severity: warning
exported_instance: kubernetes-example/tenant-a
service: namespace-deletion
exp_annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace tenant-a in cluster kubernetes-example has been in phase Terminating for
more than 30 minutes. Usually an object inside it still carries a finalizer. The
conditions in the namespace status list the remaining resource types and
finalizers.
@@ -0,0 +1,23 @@
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMRule
metadata:
name: alerts-namespace-deletion
spec:
groups:
- name: namespace-deletion
params: {}
rules:
- alert: KubeNamespaceStuckTerminating
expr: max by (cluster, namespace) (kube_namespace_status_phase{job="kube-state-metrics", phase="Terminating"}) == 1
for: 30m
labels:
severity: warning
exported_instance: '{{ $labels.cluster }}/{{ $labels.namespace }}'
service: namespace-deletion
annotations:
summary: A namespace has been terminating for more than 30 minutes.
description: >-
Namespace {{ $labels.namespace }} in cluster {{ $labels.cluster }} has been in phase
Terminating for more than 30 minutes. Usually an object inside it still carries a
finalizer. The conditions in the namespace status list the remaining resource types
and finalizers.