Why declarative configuration matters

Both Infrastructure as Code (Infrastructure as Code Terraform and OpenTofu) and GitOps (GitOps Workflow Components and Tradeoffs)—as well as Kubernetes itself (Kubernetes Principles Controllers and Resource Model)—depend on shifting from imperative scripts to declarative configuration.

  • Imperative configuration (the “how”): You write an ordered sequence of commands telling the system step by step what to do (for example: “create VM 1, create VM 2, install Nginx, open port 80”). If a step fails halfway or you run the script a second time, it may create duplicate VMs or crash because the resource already exists.
  • Declarative configuration (the “what”): You describe only the desired end state of the system (“two Nginx VMs must exist with port 80 open”) without scripting the transition steps. The automation engine inspects current state, computes the difference, and performs only the actions needed to reach that target state.
DimensionImperative (“how”)Declarative (“what”)
What the file containsStep-by-step commands/proceduresDesired end state of the system
Running it twiceMay duplicate resources or fail on existing objectsIdempotent: converges to the same desired state
Ease of auditingMust trace script logic and runtime historyReads like a specification of what should exist
Recovery after failureHard to resume safely mid-scriptRe-apply the desired state to heal or rebuild

Idempotency in automated operations

Idempotency means that applying the same configuration multiple times produces the exact same system state as applying it once (PDF p. 36 / printed slide 36).

  • Added analogy (not from the slides): Pressing the “Floor 3” button in an elevator five times is idempotent—the elevator still just goes to Floor 3. Clicking “Add $10 item to cart” five times is not idempotent—you end up with $50 in your cart. (Limit: idempotency guarantees the final target state matches the declaration, not that zero transient work occurs if an external actor modified the environment between runs.)
  • Why automation requires it: Tools like Terraform/OpenTofu and ArgoCD run repeatedly—on every commit, pull request, or periodic sync loop. Without idempotency, every automated re-run would risk duplicating resources, drifting out of sync, or breaking production.

Six motivations for declarative configuration (Slides 36–37)

  1. Simplicity and clarity: Operators define what the final system must look like rather than maintaining complex procedural scripts to reach it.
  2. Idempotency: Repeatedly applying the same config yields the same state, eliminating inconsistencies from repeated automated runs.
  3. Version control and collaboration: Declarative text files store cleanly in Git, enabling pull-request review, clear history tracking, and straightforward rollbacks.
  4. Scalability and maintainability: Administrators can manage vast fleets of machines and applications consistently without hand-crafting per-server procedures.
  5. Better compliance and security: Security and compliance rules can be audited directly against the declared end state before and after deployment.
  6. Enhanced recovery and disaster preparedness: If a cluster or region fails, the system can be rapidly rebuilt to a known-good state directly from the declarative configuration files.

Common confusion: "Declarative" does not mean "runs only once"

A declarative file is not a one-time setup checklist. In GitOps and Kubernetes, controllers continuously re-read the declarative target and compare it with live reality so the system stays aligned over time, healing accidental drift or failures automatically.

Retrieval

  1. What does it mean for a configuration tool to be idempotent, and what goes wrong in a CI/CD or GitOps pipeline if an automation script is not idempotent?
    Idempotency means applying the same configuration multiple times always leaves the system in the exact same state. If an automation script is non-idempotent (e.g., an imperative create-vm script), running the pipeline twice either fails with “already exists” errors or creates duplicate, inconsistent infrastructure.
  2. Why does declarative configuration improve disaster recovery compared to manual administration or imperative setup scripts?
    Because the declarative files capture the complete known-good target state in version control, teams can provision fresh hardware or a new cluster and let the automation tool converge it directly to that exact state without reconstructing fragile step-by-step procedures.
  3. Contrast what you write in an imperative setup script versus a declarative configuration when deploying 3 web-server replicas.
    An imperative script lists actions (“start replica 1, start replica 2, start replica 3”), which breaks or over-provisions if 2 replicas already exist. A declarative config states only “replicas = 3”, leaving the tool to inspect the live count and create, keep, or remove replicas as needed.

Source: Lecture 14 - DevOps and GitOps, PDF pages 35–37 (printed slides 35–37).