Schedule icon
Commands icon
Docker icon
Script icon
If icon
SlackIncomingWebhook icon

NamespaceJanitor - Kubernetes stuck namespace cleaner

Clears finalizers on namespaces stuck Terminating and reports the cleanup on Slack.

Categories
Infrastructure

How it works

  1. list_namespaces (io.kestra.plugin.scripts.shell.Commands) dumps all namespaces as JSON with kubectl.
  2. find_stuck (io.kestra.plugin.scripts.python.Script) keeps only namespaces in Terminating phase whose deletion timestamp is older than stuck_minutes. System namespaces (kube-system, kube-public, kube-node-lease) are skipped unconditionally. The stuck names land in stuck.txt.
  3. maybe_clean (io.kestra.plugin.core.flow.If) runs only when something is stuck. Inside it, do_clean runs only in live mode (dry_run: false): clear_finalizers GETs each stuck namespace, empties its finalizer list, and PUTs it back through the /finalize subresource, which is the only write the API accepts for a Terminating namespace. send_summary always posts the report to Slack.
  4. There is no KV state in this flow, and that is deliberate. A janitor does not need memory: once the namespace is gone it is gone, and dry-run mode is a report, not an alert.

What you get

  • Automatic recovery from the classic dead-finalizer hang, on a schedule, instead of the manual kubectl replace --raw .../finalize dance.
  • A Slack report every run that found something: which namespaces, how long they were stuck, and which finalizers were blocking.
  • Safety by default: dry_run is true unless you explicitly turn it off, and only namespaces already Terminating past the threshold are ever touched.
  • Quiet runs when nothing is stuck.

Who it's for

Anyone who has ever had kubectl delete namespace hang forever because a finalizer never cleared: operators, platform teams, and minikube experimenters alike.

Why orchestrate this with Kestra

The manual fix is a multi-step incantation people copy from Stack Overflow under pressure, which is exactly when mistakes happen. As a flow it is scheduled, audited, and safe by default: credentials stay in secrets, every run keeps its logs where you can see what was cleared, and the dry-run default means the first run can never surprise you.

Prerequisites

  • A Kubernetes cluster where you can list namespaces and update them through the finalize subresource (cluster-admin covers it).
  • A kubeconfig client certificate and key with those permissions.
  • Kestra with the Docker task runner available (used for the kubectl and Python steps).
  • A Slack Incoming Webhook URL.

Secrets

Secret What it is
CLIENT_CRT base64-encoded client certificate from your kubeconfig
CLIENT_KEY base64-encoded client key from your kubeconfig
MASTERURL base64-encoded Kubernetes API server URL, reachable from inside the Kestra container
SLACK_WEBHOOK base64-encoded Slack Incoming Webhook URL

In Kestra OSS, secrets come from environment variables prefixed with SECRET_ and must be base64-encoded. If the API URL uses 127.0.0.1 or localhost, replace it with host.docker.internal so the Kestra container can reach it.

Quick start

  1. Start Kestra with the four secrets above as SECRET_ environment variables.
  2. Import this flow and run it with dry_run: true: the Slack report lists stuck namespaces, and nothing is touched.
  3. Check the list. If it looks right, run once with dry_run: false: the finalizers are cleared, the namespaces finish deleting, and the Slack report says so.
  4. Run again: quiet, nothing stuck left.

How to extend

  • Lower stuck_minutes for dev clusters where you want faster recovery.
  • Alert PagerDuty instead of just Slack when a production namespace gets stuck.
  • Record which finalizer was blocking most often to find the misbehaving operator.
  • Run across multiple clusters by parameterizing the secrets per cluster.

Inputs

Input Type Default What it does
stuck_minutes INT 30 namespaces Terminating longer than this count as stuck
dry_run BOOL true when true, only reports what would be cleaned without touching anything
slack_webhook STRING (required) Incoming webhook URL of the Slack channel for the cleanup report

Links

Pitfalls

  • dry_run defaults to true for a reason. Flip it to false only after you have seen a dry-run report and you trust the list.
  • Clearing finalizers is a forceful act: anything the finalizer was protecting (like cloud resources provisioned by an operator) will not be cleaned up. Only run live mode when you understand why the namespace is stuck.
  • The protected system namespaces are hardcoded and no input overrides this. That is intentional.
  • A namespace stuck because the API server itself is unhealthy will not be fixed by clearing finalizers. Check control-plane health first.
See How

New to Kestra?

Use blueprints to kickstart your first workflows.