The Destructive-Plan Guard¶
A TerraformCluster is mutable: a new image tag, an edited field or a drift remediation re-applies its module. A module release that renames a resource, or a different module pointed at the same state, can plan to destroy infrastructure nobody meant to destroy. The guard stops that. It is on for every TerraformCluster apply under applyPolicy: Automatic, and it cannot be turned off. A TerraformMachinePool has a narrower form of the same guard, for applies that render a change of the cluster’s exports (see Machine pools). The command to approve is in Plan Approval.
The flow¶
flowchart TD
A["Apply Job starts<br/>(guard on)"] --> B["init, validate,<br/>plan, show"]
B -->|no changes| S["Skip the apply step,<br/>Job succeeds"]
B -->|changes| D{"Does any change<br/>delete or replace?"}
D -->|no| AP["Apply the saved plan"]
D -->|yes| H{"Is a TerraformPlan of<br/>exactly this plan approved?"}
H -->|yes| AP
H -->|no| BL["Stop: nothing applied<br/>TerraformPlan Pending<br/>DestructivePlanBlocked"]
BL -->|"approve the TerraformPlan"| A The guarded apply runs init, validate, plan -detailed-exitcode -out, show -json and then applies the saved plan, instead of applying directly.
- A change counts as destructive when its planned actions include a delete: a removal, or a replacement in either order. A
forget, animportor amoveis not destructive. - If the plan has no changes, the apply step is skipped and the Job succeeds, adopting the inputs hash and pinning the image digest as a full apply would.
- If the plan only creates or updates in place, it applies without any approval.
- If it deletes or replaces anything and no approved
TerraformPlannames exactly this plan, the Job stops before the apply step. Nothing changes and the state is untouched. The runner reports the plan, and the manager creates aTerraformPlanwith the reasonDestructive, in the phasePending, withspec.inputsHashset to the inputs hash of that apply.
The guard applies to every reason for an apply on a TerraformCluster: the first apply, changed inputs, a retry, a drift remediation and a state with no inputs hash. Under Manual it is replaced by the plan approval: a plan you approved is run as it is, and the plan has the reason Manual.
What a block looks like¶
status.lastRun.error.kindisblocked.ApplyJobSucceededisFalse/DestructivePlanBlocked. The message names the affected addresses and actions (<address> (delete)or(replace)), says nothing was applied, and names theTerraformPlanto approve with the command to do it.status.pendingPlanRef.namenames the same plan; the controller updates it at the end of every reconcile pass, paused ones too, from the live plan. Afterclusterctl move, which moves plans but not Jobs, a wait with no blocked Job has a message that startsTerraformPlan <name> plans inputs hash ...and emits no event.- A
DestructivePlanBlockedwarning event is emitted once per blocked Job, in place of the generic failure event, and names the plan and the command. - The Job is annotated
captf.io/destructive-plan-blocked, and, once it is bookkept,captf.io/plan-hashwith the hash of the plan it made. - A blocked Job counts toward neither the retry backoff nor the remediation failure cap, and does not mark the last apply as failed.
While the newest apply of the current inputs hash is blocked, no apply of that hash starts, and the controller re-checks at least every ten minutes. The apply waits for the plan’s approval even when no Job exists any more: after clusterctl move, which moves plans but not Jobs, the moved plan still gates the apply. A blocked apply whose result has no plan creates no TerraformPlan: ApplyJobSucceeded says “The runner reported no plan to approve, so the apply plans again later”, and the guarded apply runs again RetryMax (ten minutes) after the block. What else pauses depends on why the apply was due:
- A blocked input change pauses drift and health checks too, since they would render the unapplied inputs.
- A blocked drift remediation leaves drift and health checks running.
A new inputs hash starts a new guarded apply at once, and so does approving the blocked one. The old plan is superseded as soon as the inputs no longer hash to its spec.inputsHash.
Approving¶
Read the plan first: kubectl logs job/<name> -c source has its human-readable output, and the condition message lists what it deletes or replaces. Then approve the TerraformPlan the message names:
kubectl patch terraformplan <plan> -n <namespace> --type merge \
-p '{"spec":{"approved":true,"approvedBy":"<your username>"}}'
The approval binds the exact plan, not only the inputs. The apply runs with --expect-plan=<spec.planHash>: it plans again and applies only if the new plan hashes the same. Two things follow:
- If something changed in the meantime and the approved apply finds a different plan, nothing is applied. The plan becomes
Failed, and the next guarded apply plans again. If that plan is still destructive, it blocks again and creates a newTerraformPlanto approve. - An approval is consumed with its plan: once the plan is
Applied,SupersededorFailedit cannot be approved again, so a later destructive plan of the same inputs always needs its own approval. A drift remediation, which re-applies inputs the state already records, is no exception.
Machine pools¶
A TerraformMachinePool has no applyPolicy, but its apply is guarded in one case: when it renders cluster exports (captf_cluster_outputs) that differ from those of the pool’s last successful apply. The guard is the same: the apply stops before a plan that deletes or replaces anything. Nothing else about a pool is guarded on its own: the first apply, bootstrap rotations, version rolls, replica changes and spec edits all apply as before while the exports are unchanged. Machines are never guarded. See Limits.
The controller keeps a record of the exports of each successful apply in the pool’s durable inputs Secret, and compares the current exports with it. A pool with no record is covered in Pools that applied before the record existed.
The approval hash¶
A pool’s plan has the reason ExportsChange, and its spec.inputsHash is the pool’s approval hash: the inputs hash without bootstrap_data. It survives bootstrap rotations, which change bootstrap_data roughly every few minutes, and changes on any other input change. Unlike a cluster’s, a pool’s approval binds the approval hash, not the exact plan: once approved, the apply runs with --allow-deletes-hash=<approval hash>, so a rotation in the meantime does not invalidate it. ApplyJobSucceeded names the plan and the command, and status.pendingPlanRef.name of the pool names it too:
kubectl patch terraformplan <plan> -n <namespace> --type merge \
-p '{"spec":{"approved":true,"approvedBy":"<your username>"}}'
Approval is RBAC on terraformplans; see Who can approve. The approval ends with the plan, after the successful apply it approved.
When it is blocked: the change is held¶
A blocked apply of a change of the exports is held, not retried:
- The pool holds the change only while its
ExportsChangeplan waits for approval (Pending) and the plan’s approval hash is the current one. While it holds, bootstrap rotations keep applying at once with the exports of its last successful apply, so the pool keeps working. Any other input change (a version roll, a replicas or spec edit) changes the approval hash, so the change is guarded again: see the warning below. - Refresh and drift render the held exports, so the waiting change does not read as drift.
ApplyJobSucceededstaysFalse/DestructivePlanBlocked, with what the plan would delete or replace and the approval hash, so the pool’sReadyisFalsewhile a change is held.- One
Warningevent,DestructivePlanBlocked, is emitted per blocked Job. - The change is recorded in the durable Secret (
captf.io/pending-cluster-outputs).
Withdrawn and superseded¶
- Withdrawn. If the exports return to the applied ones, nothing waits. The condition reports the last apply’s real outcome and names the withdrawn Job, and the plan is
Superseded, so an approval of that change is ignored. If the exports return to that change later, it is not held: its apply is guarded again, the block makes a new plan, and only then is the change held and approvable. - Superseded. If the exports move to a different change, or the pool’s spec changes the approval hash, the old plan is
Superseded. An approval is for one change.
An edit other than a rotation makes a held pool re-guard
The tradeoff of binding the approval hash: a version roll, a replicas change or a spec edit while a change is held changes the hash, and only bootstrap rotations do not. The change is then guarded again: one more guarded apply runs with the new exports and blocks, its new TerraformPlan supersedes the old one, and the pool holds again and applies that upgrade or edit with the held exports. The old approval does not carry over.
Partly applied¶
If a guarded apply fails after it started, or its Job vanishes mid-run, the state may hold part of the change. The controller records it (captf.io/partial-cluster-outputs).
Until an apply succeeds, a partly applied pool holds nothing
Until an apply succeeds, the pool holds nothing: it cannot fall back to the last successful apply’s exports, and every apply is guarded by its own approval hash, rotations included. A destructive one waits for approval. This matters most for modules whose rotations replace resources, such as an instance configuration.
Unrecorded¶
The record of the applied exports shares the durable Secret’s budget (about 1,000,000 bytes) with the rendered inputs. If it did not fit while a change is pending, the pool cannot hold the change and waits, like a cluster, until it is approved or an input other than bootstrap_data changes. A hash annotation (captf.io/applied-cluster-outputs-hash) survives the record being dropped, so a later change is still guarded.
Pools that applied before the record existed¶
A pool that applied before CAPTF recorded applied exports has no baseline. On its first reconcile after the upgrade, CAPTF records the exports it can prove the pool applied:
- its newest successful apply’s exports, when the durable inputs still hash to the state’s inputs hash; or
- its current exports, when no apply Job is retained and the current inputs hash to the state’s.
Without that proof (an apply Job was recorded as gone, or the Secret does not match the state), every apply of the pool is guarded until one succeeds. A plan that deletes or replaces nothing applies and records the baseline. A destructive plan waits for approval like a cluster’s, and ApplyJobSucceeded says the exports of the last successful apply are unknown and gives the approve command. See Upgrades.
An apply Job deleted while it ran¶
This applies to TerraformCluster and TerraformMachinePool, and the record itself is kept for every kind, TerraformMachine included (see Unconfirmed applies). If an apply Job is deleted while it runs (for example kubectl delete job), the Job may have applied part of its change. CAPTF confirms with live reads that the Job is gone and that the object’s live status still names it, then records it in the durable inputs Secret (captf.io/unconfirmed-apply=<job>). A Job that newly failed with no runner result after its runner started, or whose pod is gone (an OOM kill, a node loss), is recorded the same way. Then:
- An apply of the current inputs stays due, even if the inputs equal the state’s.
- That apply is guarded wherever the destructive guard applies: every cluster apply, and pool applies that change exports. A plan that would undo the lost Job’s work waits for approval. Under
applyPolicy: Manualthe due apply plans first and waits for plan approval as usual. ApplyJobSucceededisFalse/ApplyFailedwith the messageJob <name>: ended without a result or disappeared while it ran, and may have applied part of its change; an apply of the current inputs is due, followed by whether that apply is guarded. When the crashed Job is itself the newest apply, the condition reports that Job’s own failure (ApplyFailedorJobDeadlineExceeded) instead. This condition takes precedence over an older blocked or plan-changed apply: it names the vanished Job until an apply started after it (one carryingcaptf.io/after-interrupted-apply) reports its own outcome, so an earlier “approve hash …” message no longer hides it.- It clears when an apply started afterwards succeeds (those Jobs carry
captf.io/after-interrupted-apply).
Nothing is recorded for a stuck Job that CAPTF deleted itself. For a pool whose lost Job rendered a change of the cluster’s exports, the change is also recorded as partly applied (see above).
Limits¶
- A deleting pool applies nothing: its condition says no apply runs.
clusterctl movedoes not carry Jobs or status, but it carries theTerraformPlan, so a plan that waits for approval still gates the apply on the target cluster.
Deleting while blocked¶
A blocked change never applied, so deleting the object while it is blocked destroys the applied inputs, not the blocked ones (see Which record a destroy renders). The destroy is never gated by the guard or by a plan.
Blocked after a failed apply¶
For clusters and pools, an apply that was blocked after an earlier apply failed now waits for its approval, and the approval applies it. Before, such a blocked apply sat idle and the earlier failure was forgotten.
Prevent destroy¶
Use prevent_destroy for a resource that must never be replaced
lifecycle { prevent_destroy = true } in the module is the stronger control: it fails even an approved plan, under either policy.