Skip to content

Lesson 4 of 9 · 22m

The Operator Pattern Is a Control Loop

Why operators exist, how reconciliation differs from scripts, and the level-triggered mental model that makes controllers predictable.

An operator is not "automation written in Go." It is a control loop that continuously compares desired state with observed state and acts to converge them. If you remember one thing, remember this: reconciliation is level-triggered, not event-triggered. The event is only a hint that it is time to look again.

The loop

observe desired object
      |
      v
read actual cluster/external state
      |
      v
compute difference
      |
      v
apply the smallest safe correction
      |
      +------ requeue and repeat

Kubernetes itself works this way. A Deployment controller does not remember a transaction script; it sees that desired replicas are three, actual ready Pods are two, and creates another Pod. Operators extend the same idea to custom domains: databases, certificate authorities, object storage buckets, game servers, or backup schedules.

Why not a script?

Scripts are edge-triggered: run once, mutate things, exit. They are poor at answering questions like "the external system was changed manually — should we repair it?" or "the apply succeeded halfway and then the worker died." A reconciler asks the same question every time: given everything I can observe now, what should exist?

func (r *WidgetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var widget examplev1.Widget
    if err := r.Get(ctx, req.NamespacedName, &widget); err != nil {
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }

    if err := r.ensureService(ctx, &widget); err != nil {
        return ctrl.Result{RequeueAfter: 30 * time.Second}, err
    }
    return ctrl.Result{}, nil
}

The function must be idempotent. It may run twice, run after partial success, or run because of an unrelated watch event. If ensureService creates blindly instead of comparing and patching, the controller will fight itself.

Events are hints, not truth

Do not design reconciliation around the exact event sequence. Events can be dropped, coalesced, delayed, or delivered after another change. Re-read authoritative state each reconcile. If the external system cannot be read, document that limitation clearly because the operator becomes a one-way writer with weaker guarantees.

Field notes

  • Requeue with backoff for transient errors; hot loops turn minor API failures into self-inflicted denial of service.
  • Rate limiting in controller-runtime is not optional politeness; it is how one bad object avoids degrading the entire controller.
  • "Reconcile succeeded" means desired work is complete, not that every downstream system is healthy.
  • Metrics matter from day one: reconcile count, error count, queue depth, and duration.

Was this lesson helpful?

Sign in to rate