Skip to content

Lesson 18 of 19 · 18m

The Reconcile Loop: Controllers, Finalizers, and Stuck Deletes

Every controller is the same watch-compare-act-write-status loop; finalizers are why resources refuse to die, and knowing the loop beats memorizing fixes.

Every Kubernetes controller — Deployment, Job, your operator — runs the same loop: watch for changes, compare desired state to observed state, act on the difference, write status, repeat. It is deliberately level-based: reconcile the current state, don't react to which event fired. A missed event doesn't matter; the next reconcile sees the same desired-vs-observed gap and closes it. That is the entire durability model — and it also explains every "Kubernetes is haunted" bug report.

Deleting a pod doesn't delete it

Delete a pod from a Deployment and a new one appears. Not a retry — a reconcile. The controller's desired state says replicas: 3; observed says 2; it creates one. Same reason kubectl scale edits survive restarts and manual pod deletes don't. If your runbook says "delete the pod to clear the bug," you were always restarting it — the controller did the work; you just supplied the same intent the API had already recorded.

Finalizers: the lock on the way out

metadata.finalizers is a list of cleanup obligations. When you kubectl delete an object carrying finalizers, the API doesn't remove it — it stamps deletionTimestamp and holds the object until each finalizer's controller finishes and removes its entry. kubernetes.io/pvc-protection keeps a PVC alive while pods mount it; CSI drivers add their own to detach volumes; operators add entries like my.app/cleanup to delete cloud resources the object owns.

The classic shape: a namespace stuck Terminating forever because something inside it carries a finalizer whose controller is dead or looping. kubectl get ns <ns> -o json shows spec.finalizers; kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -n <ns> finds what's still in it. Editing finalizers to [] force-removes the object — it works, and it skips whatever cleanup the finalizer was doing. Leaked PVCs and orphaned cloud volumes are the price. Do it when the cleanup is already done or provably gone.

Deletion propagation is a separate knob: ownerReferences drive cascading delete. --cascade=background (default) deletes dependents without ordering; foreground deletes dependents first, then the owner; orphan detaches dependents and leaves them running.

The machinery under a controller

A real controller runs a workqueue — deduplicated object keys with exponential-backoff rate limiting — fed by informer watches, not raw API calls on every event. Informers keep a shared in-memory cache per controller process (SharedInformer), which is why a well-built operator reads local state and only writes to the API — LISTs are for cold starts and the relist recovery, not the hot path. Status convention: metadata.generation counts spec changes; the controller writes status.observedGeneration so you can tell whether status reflects the spec you just sent or a stale one. Controllers needing a single active replica elect through a coordination.k8s.io Lease (controller-runtime's LeaseLock) — the same compare-and-swap machinery as everything else.

CRDs extend the loop to your own types: apiextensions.k8s.io/v1, served and storage versions, conversion webhooks when stored versions differ. An operator is nothing more than a CRD plus this loop plus domain knowledge — which is also why a crashed operator is silent: nothing reconciles, desired state drifts, and nobody pages you until the gap matters.

What to actually do

  • Before writing an operator, the test is "does drift need correcting continuously?" Helm templates once; a controller reconciles forever. Cert rotation, cross-resource bookkeeping, state that must self-heal — that's the loop's job.
  • Debug any stuck delete by reading deletionTimestamp and finalizers before touching anything.
  • Instrument the loop, not just the app: workqueue depth, reconcile duration, and error rate are where controller problems announce themselves — a controller that returns errors on every key is a queue full of retrying work, not a stopped one.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.

What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"

Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.

Article

The World now is Serverless and Container based

A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,

Article

PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.

PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...

Article

Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works

Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.

Article