ADR 0005: Journal ServiceToken create intent
- Status: Accepted
- Date: 2026-09-23
- Issue: #94
Context
Section titled “Context”Cloudflare returns a service token’s client secret only in the response to CreateServiceToken. Invariant G6 requires Flareway to capture such one-time credentials durably, in a controller-owned Secret, before it reports the resource Ready.
A create-then-write reconcile has a window between the remote create and the credential Secret write. If the Secret write fails in that window, for example on an API server 503, the pass returns an error with status.tokenId empty and no Secret (issue #94). A naive reconcile that retried CreateServiceToken on the next pass would have no record of the issued token (recovering the ID from an annotation on the credential Secret is impossible when the Secret does not exist), so the retry would strand the first token. Deleting the recorded status.tokenId would remove only what status knows, the account sweeper reports orphans without reclaiming them, and the unrecorded token would survive.
An unrecorded token is a provenance failure more than a Ready failure: the controller loses the identity of a remote object it created. The design must hold three properties:
- The controller never creates a second token for one attempt, even across a crash.
- It never adopts, rotates, or deletes a remote token whose provenance it cannot prove.
- It never drops a one-time credential it received.
Decision
Section titled “Decision”A managed fresh create records its intent in a durable journal before the remote create. Recovery resumes from the journal instead of creating blindly. The implementation is in internal/controller/servicetoken_recovery.go, with the entry points in servicetoken_controller.go.
The journal
Section titled “The journal”The journal is a Secret named flareway-st-intent-<ServiceToken UID>, controlled by the ServiceToken. Its keys are private to the controller package. It binds an immutable identity tuple:
- ServiceToken UID and the cluster ID (the
kube-systemNamespace UID); - CloudflareAccount name, UID, and account ID;
spec.zoneas written and the resolved zone ID;spec.nameand the credential destination Secret;- a random attempt nonce (8 bytes, hex-encoded) and the attempt name derived from it.
It also records the phase (prepared, dispatched, retiring, or rejected), the issued token ID once known, and the ID and name of a token being retired.
Only the managed fresh-create path writes a journal. AdoptById, ObserveOnly, and an established token (with status.tokenId set) never create one. The controller reads the cluster ID, the journal, and the credential Secret through the uncached API reader, which SetupWithManager initializes. If the cluster ID is unavailable or empty, the create fails closed.
Attempt names
Section titled “Attempt names”Each attempt creates the remote token under a name unique to the attempt:
flareway/<clusterUID>/<namespace>/<ServiceToken UID>/<spec.name>-<nonce>The name is capped at 200 characters by truncating spec.name. The prefix up to the ServiceToken UID lets a scoped listing find every token this ServiceToken started. Recovery acts only on an exact match of the full attempt name, nonce included; a prefix match can only block. After capture, the controller renames the token to its canonical name through UpdateServiceToken.
Fresh create
Section titled “Fresh create”- List tokens in scope. Any token whose name starts with this ServiceToken’s attempt prefix, or equals the canonical name, is an untracked collision: report
Conflictand stop. - Generate a nonce, add the attempt finalizer
flareway.bhyoo.com/service-token-attempt, and create the journal in phaseprepared. The finalizer covers the window before the journal exists. - Reserve the destination: create an empty Secret controlled by the ServiceToken and annotated as reserved. An existing Secret must be controlled by this ServiceToken, mutable, and accept a versioned metadata update. Otherwise report
Conflict. - Persist phase
dispatched, then callCreateServiceTokenwith the attempt name and client retries disabled (WithMaxRetries(0)). If Cloudflare returns a definite non-creation4xxresponse (any4xxexcept408,409, and429), persist phaserejected(with bounded retries on transient Kubernetes API failures while leavingdispatchedfail-closed if persistence still fails), record per-CR exponential create backoff (reset whenServiceTokengeneration,CloudflareAccountresource version, or resolved remote name changes) before local cleanup, remove any empty controller-reserved destination Secret carrying the pending-reservation annotation, release the attempt finalizer with an optimistic-lock fresh read, delete the journal Secret withUIDandResourceVersionpreconditions, and surface the API error (RemoteError, orRemoteMissingfor404) withRequeueAfterinstead of enteringRecoveryPending. - Record the issued token ID in the journal.
- Write the credential to the destination Secret. If the returned name differs from the attempt name, write the credential anyway and report
Conflict; the create is never repeated. - Align mutable fields and the canonical name, patch
status.tokenIdwithReady=True, then delete the journal and remove the attempt finalizer.
Recovery
Section titled “Recovery”When a journal exists and status.tokenId is empty, recovery runs after grant authorization and before the freshness gate — with one explicit exception: a managed rejected journal (validated to carry neither issuedID nor retiringID) finishes local cleanup (empty reserved destination via the journaled destination, optimistic-lock attempt finalizer removal, and version-checked journal deletion) even if grant authorization, zone verification, spec.zone, or spec.secretRef changed, making zero Cloudflare calls. Contradictory rejected journals carrying an issuedID or retiringID fail closed with Conflict (CleanupBlocked on deletion). Otherwise:
- The journal must match the live spec, account, and cluster ID. A
spec.zoneoraccountRefedit during a pending attempt reportsConflict. A verified-zone status flap does not: recovery resumes under the journaled zone ID. - A pending journal under
AdoptByIdreportsConflict. UnderObserveOnly, an unresolved non-rejectedjournal (or a contradictoryrejectedjournal) with nostatus.tokenIdreportsRecoveryPendingorConflictand performs no recovery mutation; a validrejectedjournal allows read-only observation viaexternalRefwhile leaving any destination Secret untouched (and on deletion underObserveOnly,rejectedcleanup removes only the attempt finalizer and journal Secret, leaving any destination Secret to Kubernetes garbage collection). - The controller reads the destination Secret first. A credential counts as committed only when the client ID, client secret, and token ID annotation are all non-empty. A committed credential converges without rotating or re-creating. If its token ID differs from the journaled issued ID, the controller reports
Conflict.
Otherwise the phase decides:
| Phase | Behavior |
|---|---|
prepared |
Reserve the destination and re-list. Send the create only when no token collides. |
dispatched, no exact match |
Report Ready=False, reason RecoveryPending, and re-list after 30 seconds. No create, rotate, or delete. |
dispatched, more than one exact match |
Report Conflict without mutation. |
dispatched, one exact match, account scope |
Record its ID, then RotateServiceToken to obtain a fresh credential, capture it, and converge. A failed rotate stays journaled for retry; there is no delete fallback. |
dispatched, one exact match, zone scope |
Zone-scoped tokens cannot rotate. Record the token as retiring and move to retiring. |
retiring |
Delete the retiring token and report RecoveryPending. Once GetServiceToken returns 404 and the listing no longer shows it, draw a new nonce and return to prepared. |
rejected |
Complete rollback locally with no Cloudflare calls: remove any empty reserved destination Secret, release the attempt finalizer, delete the journal Secret with UID/ResourceVersion preconditions, and requeue. |
A journal that remains after status.tokenId is recorded only needs removal: the controller deletes it and the attempt finalizer, and never uses it to resume or rotate.
Deletion and lifecycle
Section titled “Deletion and lifecycle”With deletionPolicy: Delete on a managed token, deletion removes the recorded status.tokenId, then removes any token recorded by a pending journal, using the journaled account and zone binding. If the attempt finalizer is present, the journal is gone, and status.tokenId is empty, the controller lists tokens in scope. It blocks if any name starts with this ServiceToken’s attempt prefix, because it cannot prove which attempt created that token, and otherwise releases the finalizer without a remote mutation. Any failure reports CleanupBlocked and keeps the finalizer. deletionPolicy: Orphan keeps remote tokens. ObserveOnly never mutates remote tokens, credentials, or journals.
An established token whose credential Secret disappears reports SecretMissing. The controller never rotates or re-creates it on its own; rotation requires an explicit spec.rotation.requestedAt.
Consequences
Section titled “Consequences”- A failed or interrupted credential write leaves a journal that names the issued token, so the retry recovers that token instead of creating another. The credential the controller finally captures belongs to the token recorded in status.
- A one-time credential the controller receives is written to the destination Secret even when the provider returns a divergent name.
- A
dispatchedattempt whose token never becomes visible staysRecoveryPendingand re-lists every 30 seconds. The controller cannot tell a lost create from a slow listing, so it refuses to guess. This non-convergence is deliberate. - Each pending create adds one Secret and one finalizer. Both are removed after status records the token.
- Until capture completes, the remote token carries the attempt name, which includes the cluster and ServiceToken UIDs.
- Provenance rests on the ownership-verified journal and the unguessable nonce. Remote service tokens have no tags or comment field to carry stronger provenance.
- A cluster restored from a clone with the same
kube-systemUID and journals, sharing one Cloudflare account, cannot be told apart from the original. If the journal, the ServiceToken, and the remote token’s provenance are all lost, the design fails closed on what remains; it cannot recover what no record describes.
Alternatives considered
Section titled “Alternatives considered”| Alternative | Reason rejected |
|---|---|
Record status.tokenId right after the create |
Moves the window without closing it. A failed status write strands the token exactly as a failed Secret write does. |
| Delete the token in the same reconcile when the Secret write fails | The rollback lives in memory. A crash between the failed write and the rollback strands the token. |
| Adopt a remote token by name on retry | A name cannot prove provenance. It can pick a duplicate, a token from another cluster, or a token in another scope, and it cannot recover the one-time secret. Violates G2 and G3. |
| Redesign the Accepted and Ready condition API for recovery states | Existing condition reasons (RecoveryPending, Conflict, CleanupBlocked, SecretMissing) express the recovery states; a redesigned condition API adds surface without adding information. |
| Sign attempt names with an HMAC against forgery | Speculative. A forged name still has to match the full name with a random nonce. |
| Match attempts by prefix alone | Weakens provenance. Any token under the prefix could qualify for rotation or deletion; exact attempt-name matching narrows mutation to the recorded attempt. |