ADR 0004: One field manager for tunnel conditions
- Status: Accepted
- Date: 2026-09-23
- Issue: #92
Context
Section titled “Context”Two reconcilers write CloudflareTunnel status with server-side apply (SSA). The tunnel reconciler owns the remote tunnel data (tunnelId, dnsRecords, clients, addresses, and the identity fields) under the flareway-tunnel field manager. In Gateway configuration mode, the Gateway reconciler owns configVersion, hostnames, and listeners under flareway-gateway. Both reconcilers also author conditions: the Gateway reconciler reports ConfigApplied, and the tunnel reconciler reports TunnelReady, DNSReady, and the aggregate Ready.
Ready is a function of TunnelReady, ConfigApplied, and DNSReady. If the two writers each committed their own conditions from their own snapshots, independent commits could expose a Ready value inconsistent with its inputs, restore an obsolete transition timestamp, and generate unnecessary watch events (issue #92): a mid-pass ConfigApplied=False could reach consumers such as kubectl wait and GitOps health checks, a restored True could move lastTransitionTime backwards, and a Ready derived from a stale snapshot could persist alongside ConfigApplied=False. Every condition write would also bump resourceVersion and wake both reconcilers through their tunnel watches, so a transient False would feed its own churn.
Conditions must therefore merge against live state and commit together with Ready in one transaction; unchanged conditions must not trigger another write. The design must keep Ready consistent with its inputs in every persisted revision, keep a converged tunnel quiet, and keep crash windows around the Cloudflare configuration push fail-closed.
Decision
Section titled “Decision”All Flareway condition types on CloudflareTunnel are owned by one shared SSA field manager, flareway-tunnel-status (internal/controller/tunnel_conditions.go). Both reconcilers commit conditions through one optimistic-concurrency transaction. The data fields stay with the existing managers.
Ownership split
Section titled “Ownership split”| Manager | Owns |
|---|---|
flareway-tunnel-status |
Accepted, TunnelReady, ConfigApplied, DNSReady, PrivateListenerDegraded, Ready, CleanupBlocked, Conflict, DriftDetected |
flareway-tunnel |
Tunnel-owned data: remote identity, dnsRecords, clients, addresses |
flareway-gateway |
configVersion, hostnames, listeners (Gateway mode only) |
Data applies omit status.conditions. status.conditions is a +listType=map list keyed by type, so an entry missing from an apply document stays untouched and unowned. The shared manager never places a non-Flareway condition type in its document, and external conditions keep their owners.
The single-writer rule for tunnel configuration (invariant G4) is separate from this split. The shared manager decides who owns Kubernetes status entries; G4 decides who writes the remote Cloudflare configuration. In Direct mode the tunnel reconciler authors ConfigApplied through the same transaction, and the Gateway writers refuse Direct-mode tunnels. The tunnel reconciler refuses to author ConfigApplied outside Direct mode.
The conditions transaction
Section titled “The conditions transaction”patchTunnelConditions runs each attempt in this order:
- Read the live tunnel through the uncached API reader. The live
resourceVersionbecomes the basis for the write. - Check identity: the live object must match the observed UID,
metadata.generation, and configuration mode. A mismatch aborts without writing. - Run the caller’s guard, if any, against the live object.
- Merge the authored deltas onto the live Flareway set. Non-authored Flareway entries are carried from the live object unchanged, including their timestamps. An authored entry whose
statusmatches live keeps the livelastTransitionTime; a changed entry takes the caller’s clock. - Derive
Readyin the same document. - Skip the write if the merged set is semantically equal to live and the shared manager already owns every Flareway type in it.
- Otherwise apply with
ForceOwnershipand the liveresourceVersionpinned. A 409 Conflict restarts from step 1, up to three attempts. Remote actions are never replayed.
Callers pass only the deltas authored in this pass, never a Ready computed elsewhere and never entries copied from a cached read.
Ready rule
Section titled “Ready rule”deriveTunnelReadyCondition sets Ready=True only when TunnelReady, ConfigApplied, and DNSReady are all True in the merged document. The clamp runs on every commit, including a commit that authors nothing: a live Ready=True with an unmet input becomes Ready=False (reason Pending) in the same apply. A lifecycle override such as ObserveOnly’s Ready=False wins over derivation, but no override can produce Ready=True while an input is unmet.
Commit order
Section titled “Commit order”Every status update runs the conditions transaction before the data apply. The first commit claims ownership of the Flareway entries for the shared manager. SSA deletes fields that the applying manager owned but omitted, so a data-only apply that ran first could delete condition entries still owned solely by flareway-tunnel or flareway-gateway.
Promotions wait for their evidence. A True value for Ready or one of its inputs commits only after the status data that justifies it is durable:
- The tunnel reconciler’s
patchOwnedStatuscommits conditions with promotions withheld, applies its data, then commits the withheld promotions. - A Gateway demotion uses
patchTunnelGatewayStatus: conditions first, then data. - A Gateway promotion runs a conditions commit with no deltas (ownership claim and clamp), applies the data, then commits
ConfigApplied=TruethroughpatchTunnelGatewayConditionsValidatedwith the promotion guard.
Writer guards
Section titled “Writer guards”The Gateway reconciler re-runs gatewayTunnelConditionGuard against the fresh tunnel on every attempt of both its conditions and data writes. The guard requires the live Gateway to exist with the observed UID and no deletionTimestamp, the tunnel’s remote and owner identity to match what the pass observed, the Gateway to be the selected UID-bound owner with no drain pending, and the tunnel to be managed, verified, not remotely deleted, with a tunnelId and connector token Secret.
gatewayPromotionGuard adds the evidence check for ConfigApplied=True. Each domain is judged in its own units:
- the live
configVersionstill records this pass’s desired version, desired hash, and remote version; - the exact compiled xDS snapshot identity is ACKed;
- when the data-plane convergence gate applies, every active cloudflared Pod reports the desired Cloudflare configuration version.
Managed DNS and private routing readiness are independent of this ConfigApplied checkpoint. Gateway Programmed still requires every listener’s prerequisites. Public listeners require exactly one managed DNS record with state: Ready per canonical normalized DNS hostname; missing, empty, unknown, duplicate, or conflicted entries remain unready. Private listeners use their own VNet, HostnameRoute, and origin-JWT TLS-decryption contract. Missing prerequisites block the affected listener before publication without delaying another listener’s applied checkpoint. Account ownership and DeviceSettings remain shared private prerequisites. Tunnel Ready retains its documented three-condition formula, so Gateway/listener Programmed carries private routing availability. See Applied hostname status and Access transitions.
The xDS snapshot identity is a string and the Cloudflare configuration version is an int64. They are never compared with each other. If convergence evidence is lost between the gate and the commit, the pass demotes ConfigApplied and reports the Gateway pending. A provenance mismatch aborts without demoting, so a pass never writes to another owner’s or another generation’s object.
The tunnel reconciler has no ownership guard because it is the authority that sets ownershipVerified, gatewayRef, and deletedAt. Gating on those fields would block provisioning, ObserveOnly, conflict, and teardown writes.
Steady state
Section titled “Steady state”The Gateway reconciler writes ConfigApplied=False before the data-plane convergence gate only when the Cloudflare configuration step reports pending, drift, or a drift hold. A converged pass authors the same conditions it finds, so the conditions commits skip and the object keeps its resourceVersion.
Invalidation around the configuration push
Section titled “Invalidation around the configuration push”reconcileCloudflaredConfiguration takes the tunnel lock and reads the remote configuration. If the persisted desiredHash matches the compiled hash and the remote version equals the persisted desired version, the pass confirms the recorded version without a push. Otherwise, immediately before UpdateTunnelConfiguration, it commits ConfigApplied=False (reason Applying) through the conditions transaction. The derived Ready drops to False in the same document. The intended hash appears only in the message; configVersion.desiredHash keeps the hash of the last successful push until the push succeeds. After a successful push, one data apply records the provider-assigned version, hash, remote version, and appliedAt, and the data-plane convergence gate decides ConfigApplied promotion. A remote error demotes ConfigApplied before the error returns.
Remote identity capture
Section titled “Remote identity capture”CreateTunnel is not idempotent, and status.tunnelId is the only record of the created object. The tunnel reconciler commits its conditions before calling ensureRemoteTunnel, so the status subresource exists before any create. persistRemoteIdentity then records the identity with an RFC 6902 JSON Patch. The patch has one precondition, a test operation on /metadata/uid, and adds the identity fields the remote read returns: tunnelId, accountId, name, tunnelType, configSource, connectorState, createdAt, deletedAt, and ownershipVerified. It skips any that are absent rather than writing a JSON null. It carries no generation or resourceVersion precondition, so a spec edit or a concurrent status write cannot veto the capture. JSON Patch has no SSA ownership semantics, so the patch cannot delete or claim condition entries.
Watches
Section titled “Watches”The tunnel reconciler’s For(&CloudflareTunnel{}) and the Gateway reconciler’s tunnel watch carry no predicate. Both reconcilers depend on status-only wakeups: the Gateway reads tunnelId, ownershipVerified, connectorTokenSecretRef, deletedAt, and dnsRecords from tunnel status. Churn is removed at the source by skipping unchanged writes.
Consequences
Section titled “Consequences”- Every persisted revision keeps
Readyconsistent withTunnelReady,ConfigApplied, andDNSReady. The torn pair(ConfigApplied=False, Ready=True)can exist only in a writer’s memory. - A converged tunnel produces no condition changes, no
resourceVersionbumps from conditions, and no self-inflicted watch events. lastTransitionTimemoves only on a real status change.ConfigApplied=Falsewith reasonApplyingis visible for the duration of a configuration push. Consumers see a real pending state, not a flap.- A crash after the push and before the checkpoint leaves remote state ahead of status. The next pass sees a remote version different from its baseline and takes the drift path: the default policy re-pushes, and
DriftPolicy: Holdreports an out-of-band change. Delivery is at least once. - The optimistic-concurrency check covers only the
CloudflareTunnelobject. A Gateway spec change can race a tunnel commit; the Gateway watch requeues a correcting pass. - A status update costs up to three requests (conditions, data, promotion), each retried at most three times on conflict.
- A legacy co-owner that never releases its condition entries (a deleted Gateway, or a tunnel that moved from Gateway to Direct mode) does not force a write every pass. Once the shared manager covers the set, equal content skips.
Alternatives considered
Section titled “Alternatives considered”| Alternative | Reason rejected |
|---|---|
Fix only the Gateway’s early ConfigApplied=False write |
Stops the flap but leaves Ready derived by a different writer from a different snapshot, so the torn pair can still persist. |
Keep per-writer managers and add a fresh read with a resourceVersion check to each |
Each writer still commits its half separately. A reader between the two commits can observe a torn pair. |
Move Ready to the Gateway manager |
Gateway mode only. Direct mode and ObserveOnly have no Gateway writer, and Ready’s inputs TunnelReady and DNSReady stay with the tunnel reconciler. Switching the owner of a single entry by mode also rewrites managedFields on each switch. |
| One shared manager for all tunnel status | Freshness and clear-intent rules for configVersion, dnsRecords, and clients differ per writer. Merging them into one transaction widens every write and couples unrelated data. |
| Record the new desired hash before the push | Breaks the no-change fast path under the tunnel lock. If the push then fails, the persisted hash matches the compiled hash and later passes skip the push indefinitely. |
| Filter tunnel watches to generation changes | The Gateway and tunnel reconcilers depend on status-only events for tunnel ID assignment, ownership, credential, and DNS changes. A filter would trade churn for missed wakeups. |
| Capture remote identity through the conditions transaction | The transaction’s generation and resourceVersion checks could veto the capture after a non-idempotent create, losing the only record of the remote tunnel. |