From 5d54e94b8412cac76ff50ef7757aad2663b12991 Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:04:24 +0100 Subject: [PATCH] feat(monitoring): alert when a namespace stays in Terminating A namespace that never finished deleting raised no alert of its own. A tenant namespace deleted directly, not through its Tenant, stayed in Terminating for about 28 hours while the finalizer of a Kamaji TenantControlPlane inside it waited for a DataStore secret after the tenant etcd was gone. The only signal was HelmReleaseNotReady of the tenant release, which kept flapping. kube-state-metrics exports kube_namespace_status_phase with a Terminating series per namespace that is 1 while it is being deleted. The rule takes the maximum per cluster and namespace, so the timer survives kube-state-metrics moving to another node, which changes its node and pod target labels. Healthy deletions finish in under a minute, and in about five for a tenant with a Kubernetes cluster, a VM and Postgres inside, so 30 minutes leaves wide room and still reports a stuck namespace within the hour. Alerta builds the resource from exported_instance and deduplicates on environment, resource and event, without the cluster tag. With only the namespace there, two clusters with a stuck namespace of the same name became one Alerta alert, and the resolve from one cluster closed it while the other namespace was still terminating. The resource is now cluster/namespace. Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> --- hack/namespace-deletion-alerts.bats | 26 +++ .../namespace-deletion-alerts/unittest.yaml | 160 ++++++++++++++++++ .../alerts/namespace-deletion.yaml | 23 +++ 3 files changed, 209 insertions(+) create mode 100644 hack/namespace-deletion-alerts.bats create mode 100644 hack/testdata/namespace-deletion-alerts/unittest.yaml create mode 100644 packages/system/monitoring-agents/alerts/namespace-deletion.yaml diff --git a/hack/namespace-deletion-alerts.bats b/hack/namespace-deletion-alerts.bats new file mode 100644 index 000000000..63c5e3bfc --- /dev/null +++ b/hack/namespace-deletion-alerts.bats @@ -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 ] +} diff --git a/hack/testdata/namespace-deletion-alerts/unittest.yaml b/hack/testdata/namespace-deletion-alerts/unittest.yaml new file mode 100644 index 000000000..d61948bd8 --- /dev/null +++ b/hack/testdata/namespace-deletion-alerts/unittest.yaml @@ -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. diff --git a/packages/system/monitoring-agents/alerts/namespace-deletion.yaml b/packages/system/monitoring-agents/alerts/namespace-deletion.yaml new file mode 100644 index 000000000..0986e0ab1 --- /dev/null +++ b/packages/system/monitoring-agents/alerts/namespace-deletion.yaml @@ -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.