Plan Approval¶
TerraformCluster guards two things before it changes infrastructure: a plan that deletes or replaces a resource waits for an approval, and with spec.applyPolicy: Manual every plan waits for one. Each wait is a TerraformPlan object that you approve by setting spec.approved. This page is the how-to: the commands to find, review and approve it. How the gates work, what the plan hash binds and what they do not cover are in the chapter Approvals and Gates.
Before you begin
- A
TerraformClusterwhose apply you want to guard or preview. kubectlaccess to readterraformplansand to patch them, and to read the logs of the target’s Jobs. Approving needs no access to the target; see who can approve.
Find the plan¶
A waiting plan is named by status.pendingPlanRef.name on its target (a TerraformCluster or a TerraformMachinePool), and the field is omitted when no plan is live:
To list plans by label instead:
kubectl get terraformplans shows each plan’s target, reason, phase and counts. A target has at most one live plan, in the phase Pending or Approved. A newer plan, or a change that makes the plan moot, supersedes it.
Approve a plan¶
-
Read the plan: its counts and resources are in
spec.summary, and the human-readable plan is in the log of the Job the condition message names: -
Approve it, with your own username in
approvedBy:kubectl patch terraformplan <plan> -n <namespace> --type merge \ -p '{"spec":{"approved":true,"approvedBy":"'"$(kubectl auth whoami -o jsonpath='{.status.userInfo.username}')"'"}}'The admission webhook requires
approvedByto equal the username of the request, so the field says who approved. The plan moves toApproved, and the controller emitsPlanApprovedon the target when it first sees the approval. -
The apply runs with
--expect-plan=<spec.planHash>. It plans again and applies only if the new plan has the same hash. After it succeeds, the plan isApplied.
An approval cannot be undone, and a plan that is Applied, Superseded or Failed can no longer be approved.
The destructive-plan guard¶
Under the default applyPolicy: Automatic, every TerraformCluster apply, including a drift remediation, plans first and stops before applying if the plan deletes or replaces a resource. Nothing changes. The manager creates a TerraformPlan with the reason Destructive, and ApplyJobSucceeded turns False/DestructivePlanBlocked. Its message names the affected resources and the plan to approve, with the command. See The destructive-plan guard for the full behavior.
Approve the plan as above. The approval binds the exact plan, not only the inputs: if the approved apply plans something else, it applies nothing, the plan becomes Failed, and a new plan blocks again if it is still destructive.
Each destructive plan needs its own approval
A finished plan cannot be approved again, so the next destructive plan is blocked and needs a new plan and a new approval. lifecycle { prevent_destroy = true } in the module remains the stronger control for a resource that must never be replaced.
Plan preview: applyPolicy Manual¶
Set spec.applyPolicy: Manual on the TerraformCluster or its TerraformClusterTemplate to review every change except the first apply. Switching back to Automatic makes a waiting plan moot, and the apply runs.
- A change plans first. The manager creates a
TerraformPlanwith the reasonManual, andApplyJobSucceededbecomesUnknown/PlanAwaitingApproval. Nothing applies. A plan with no change creates no object and applies at once. - Find, review and approve the plan as above.
- If anything changed, the apply stops with
PlanChanged: the approved plan becomesFailed, and a newManualplan waits for approval. See Manual plan approval.
Approving a plan also approves its deletes and replacements
A Manual plan needs no separate destructive approval. A plan with no changes needs no approval. A plan that only changes outputs, or only imports or moves, does.
Caveats¶
- The plan hash binds what each change does, including old and new values, so a plan with different values needs its own approval. It reveals no value; read values in the plan Job’s log. See What the plan hash binds.
- A failed step of the approved apply keeps the plan
Approved: the apply is retried with the same expected plan. - Hashes start with
p2:. - Plans are objects of their own: they move with their target through
clusterctl move, and the moved plan still gates the apply, even though Jobs and status do not move. - Finished plans are kept for history. The manager keeps the 10 newest finished plans of each target.
- In a GitOps setup, do not keep
TerraformPlanobjects in Git: the manager creates them. A person approves one, or a pipeline does after its own review of the plan and the plan Job’s log.
Automate approvals¶
Because a plan is an object, a pipeline or a bot can list, watch and approve plans like any Kubernetes object, using the same patch. Limit what it may approve with RBAC on terraformplans, and with a ValidatingAdmissionPolicy that lets a bot approve non-destructive plans while a destructive plan needs a human group. See Tiered auto-approval for the policy and its binding.
What is not guarded¶
Neither gate applies to a TerraformMachine, and a TerraformMachinePool is guarded only when its apply renders a changed set of cluster exports (see Machine pools). A pool’s plan has the reason ExportsChange, and you approve it the same way. Destroy, restore, refresh and drift Jobs are never gated. See Approvals and Gates and Limits.
Confirm it worked¶
- After approving a plan,
kubectl get terraformplan <plan> -n <namespace>shows the phaseApproved, thenAppliedonce the apply succeeds. kubectl describe terraformcluster <name> -n <namespace>showsApplyJobSucceededback toTrue/ApplySucceeded, andstatus.pendingPlanRefis gone.