Around Go: DevOps and reliability · 3 MIN READ

Reconcile desired state without overwriting a newer observation

Separate observation, planning and application; use a version precondition and make a repeated successful reconciliation converge.

A controller repeatedly compares desired state with observed state. Network retries and concurrent operators make “run this command once” an unreliable model. A useful plan says exactly what will change, from which observed version, and what evidence will establish convergence.

Go for DevOps and reliability · A MAP OF THE IDEA

  1. Observe version and owner
  2. Plan the intended fields
  3. Apply with a precondition
  4. Read back and converge

Useful when

  • Separate observation, planning and application; use a version precondition and make a repeated successful reconciliation converge.
  • Make changes reviewable and verify the result against the original contract.

Watch out

  • A stale plan must be rejected and regenerated from a fresh observation. Use server-side preconditions; client-side checks alone cannot prevent a concurrent write.

Implementation & operation cost: Automation needs explicit ownership, bounded privileges, tests and recovery evidence; a shorter command is not sufficient.

01

Make observation and mutation separate operations

Represent an observed object, its version and the intended fields as data. Planning should be deterministic and read-only. Review the resource identifier, environment, version, desired fields and blast radius before applying. A plan computed for one namespace or cloud account must not silently apply to another. Re-read state after a conflict rather than forcing the old plan.

02

An in-memory compare-and-swap exercise

The example changes a replica count only if the observed version still matches. Applying an old change a second time returns a conflict. Planning again against the new observation finds no work. That is convergence; it does not mean every repeated API request is harmless.

go
package main

import (
 "errors"
 "fmt"
)
var ErrConflict = errors.New("observed version changed")
type State struct { Version, Replicas int }
type Change struct { ExpectedVersion, Replicas int }
func plan(desired int, observed State) (*Change, error) {
 if desired < 0 || desired > 20 { return nil, errors.New("replicas outside budget") }
 if observed.Replicas == desired { return nil, nil }
 return &Change{ExpectedVersion: observed.Version, Replicas: desired}, nil
}
func apply(state *State, change Change) error {
 if state.Version != change.ExpectedVersion { return ErrConflict }
 state.Replicas = change.Replicas
 state.Version++
 return nil
}
func main() {
 state := State{Version: 41, Replicas: 2}
 change, _ := plan(3, state)
 fmt.Println("planned", change.ExpectedVersion, change.Replicas)
 fmt.Println("applied", apply(&state, *change), state)
 fmt.Println("stale", errors.Is(apply(&state, *change), ErrConflict))
 again, _ := plan(3, state)
 fmt.Println("converged", again == nil)
}
Read the code

Expected lines: planned 41 3; applied <nil> {42 3}; stale true; converged true. This map-free model has no external infrastructure and can run in the app. A real API needs server-side version checks; a local if statement around two HTTP requests is not atomic.

03

Reconcile only the fields you own

A full-object replacement can erase another controller’s fields. Define ownership and use the API’s supported preconditions and patch semantics. In Kubernetes, resourceVersion is an opaque string, not a client-side integer counter; server-side apply also tracks field ownership. The integer here is only a teaching model. Conflicts are evidence of a newer observation, not permission to remove another writer’s changes.

04

Retry with budgets and outcome reconciliation

After a transport timeout, a write may have committed even though its response was lost. Read the authoritative object or query by a stable operation identity before trying a non-idempotent operation again. Retry only classified transient failures, with cancellation, a total deadline, a maximum attempt count and jitter where appropriate. An error caused by invalid configuration or authorization does not become valid after sleeping. Stop an automated loop when it cannot establish a safe outcome.

05

Practice a concurrent operator

Write tests where a second writer advances Version between plan and apply. Verify that the stale attempt changes neither replicas nor version. Test a no-op plan, an invalid desired value and a successful retry after a fresh observation. Extend the model with an environment identifier and prove that a plan for staging cannot mutate production. Only then adapt the interface to a disposable local API, using least-privilege credentials and an explicit apply action.

TAKE THIS WITH YOU

Separate observation, planning and application; use a version precondition and make a repeated successful reconciliation converge.

Check your understanding

A resource changed after you generated the plan. What is the next action?

Go to the source

Progress stays in this browser. No account needed.