Skip to content

Lifecycle and Ownership

Every Kubernetes object this project creates on your behalf also has a Cloudflare-side counterpart: a tunnel, a DNS record, an Access application. This page covers the rules that keep those two sides in sync, and keep this project from ever touching a Cloudflare resource it doesn’t actually own.

Finalizers: cleanup happens before deletion, not after

Section titled “Finalizers: cleanup happens before deletion, not after”

Every Cloudflare-backed object (CloudflareTunnel, CloudflareDNSRecord, and the Access-related CRDs) carries a Kubernetes finalizer. A finalizer blocks Kubernetes from actually removing the object until the controller has run its own cleanup and removed the finalizer itself.

This matters because Kubernetes garbage collection and the Cloudflare API are two separate systems with no transaction spanning both. Without a finalizer, deleting a CloudflareTunnel object would remove it from Kubernetes immediately, and the real tunnel at Cloudflare would have no corresponding object left to drive its deletion — it would simply be abandoned. The finalizer makes the Cloudflare-side delete (or, with deletionPolicy: Orphan, the decision not to delete) happen first, while the object — and the credential reference it needs to authenticate that delete call — still exists.

deletionPolicy: delete the real resource, or leave it in place

Section titled “deletionPolicy: delete the real resource, or leave it in place”

Every external-resource object carries a deletionPolicy field, Delete or Orphan, defaulting to Delete. It can be set on the object itself, or as a default on the CloudflareGatewayConfiguration it resolves through (an explicit value on the object always wins over the configuration’s default).

  • Delete (the default): deleting the Kubernetes object also deletes the real Cloudflare-side resource.
  • Orphan: deleting the Kubernetes object leaves the Cloudflare-side resource in place. Use this when a tunnel or DNS record needs to outlive the cluster object that created it — for example, before decommissioning a cluster while keeping its DNS records live.

Either way, the finalizer still runs: Orphan changes what happens at delete time, not whether cleanup happens at all.

Ownership markers: never touch what we didn’t create

Section titled “Ownership markers: never touch what we didn’t create”

Every Cloudflare-side resource this project creates — a tunnel, a DNS record, an Access application — carries a small, deterministic marker identifying this cluster as its owner: a tunnel’s name, a DNS record’s comment field, or an Access application’s name, depending on the kind. Exactly what that marker contains for a given kind is part of the generated API Reference; this page doesn’t restate it.

What matters here is what the marker is for: the controller regenerates what the marker should look like and compares it against what’s actually there — the resource might belong to someone else entirely: a different cluster, a different tool, or something a person configured by hand in the Cloudflare dashboard. Never delete what we didn’t create is the rule this exists to enforce. How widely it’s checked differs by kind:

  • For CloudflareTunnel and CloudflareDNSRecord, the re-check happens only at delete time: the finalizer compares the live marker against the expected one immediately before calling the delete API, and refuses the delete on a mismatch. The repair path (periodic drift correction) does not re-check the marker at all — it compares live content against desired content and overwrites on any difference, tampered marker or not. A delete-time mismatch also doesn’t surface a condition: the finalizer’s error is simply returned, the object stays Terminating with the finalizer still attached, and Kubernetes retries with backoff until the mismatch is resolved by hand.
  • Access-related kinds re-verify more broadly: the marker is checked before writes as well as before deletes, not only at delete time.

Sometimes a Cloudflare-side resource matching what an object would create already exists — most commonly because it was created by hand, or by a prior cluster sharing the same Cloudflare account. By default, the controller refuses to touch it, and nothing is written — but which condition carries Reason=[AdoptionRefused](/reference/conditions/#adoptionrefused) differs by kind. For CloudflareTunnel and CloudflareDNSRecord, the spec itself is still valid (Accepted stays True, “spec is valid”) and the refusal shows up on Synced/Ready instead. For CloudflareAccessApplication, adoption refusal is a spec-level outcome and sets Reason=[AdoptionRefused](/reference/conditions/#adoptionrefused) on all three — Accepted, Synced, and Ready.

CloudflareTunnel and CloudflareDNSRecord both carry an adopt field (defaulting to false) as the explicit opt-in: set it to true and the controller adopts the existing resource instead of failing. There’s a real reason the default is refusal rather than adoption: a tunnel’s remote configuration write is a full replace, so silently adopting a hand-managed tunnel would overwrite its existing configuration on the very first reconcile. Some Access-related kinds go further and refuse adoption unconditionally, with no opt-in at all — see Access if you’re using that feature.

The controller doesn’t assume that what it wrote to Cloudflare stays that way. On a periodic resync (every few minutes, with a little jitter to avoid every object resyncing in lockstep), it compares the live Cloudflare-side state against what it last wrote and repairs any difference by re-applying the desired state.

CloudflareTunnel surfaces this explicitly: once a repair re-PUT succeeds, Synced is set to True with Reason=[DriftDetected](/reference/conditions/#driftdetected) (“drift detected and repaired”) — the reason names what just happened, not a pending action. If the re-PUT itself fails, the condition reports the actual failure (Reason=[CloudflareAPIError](/reference/conditions/#cloudflareapierror) or [RateLimited](/reference/conditions/#ratelimited)) instead. (Some Access-related kinds use the same reason for the same purpose.) CloudflareDNSRecord repairs the same way — comparing the live record against the desired one and updating on a mismatch — but reports a plain Synced=True once the repair lands rather than naming the drift separately. Either way, most drift clears itself on the next reconcile with no action needed; see DriftDetected for the one case that doesn’t self-heal.

CloudflareDNSRecord can be created two ways: directly, by a user, or automatically, by the controller translating a Gateway/Route’s own hostname. Both kinds of author compete for the same hostname through one shared, cluster-wide index — and the rule deciding who wins is oldest CreationTimestamp wins, not “the user-authored one always wins.” In practice, this usually looks like a user winning: a record created ahead of time, to reserve a hostname before any Gateway or Route exists, predates anything that could later compete for it. But it isn’t an authorship rule — if a controller-generated record for a hostname already exists by the time a user creates a conflicting CloudflareDNSRecord for the same hostname, the newer, user-authored one loses and reports Reason=[Conflicted](/reference/conditions/#conflicted).

Only routing records compete this way — a CNAME pointed at a tunnel, or similar. CloudflareDNSRecord.spec.recordType only accepts CNAME, A, or AAAA; it has no TXT or MX option. An orthogonal TXT or MX record on the same hostname — managed outside this controller entirely, through Terraform, the Cloudflare dashboard, or any other tool — was never competing for anything and never conflicts.

Deletion order, and the early-configuration-deletion orphan hazard

Section titled “Deletion order, and the early-configuration-deletion orphan hazard”

Deleting a Gateway cascades, through Kubernetes owner references, to the objects it generated — but that cascade is asynchronous: Kubernetes only garbage-collects an owned object once its owner is gone, and each owned object then has to run its own finalizer cleanup against the Cloudflare API before it actually disappears. The owning chain differs by mode:

Deleting the Gateway deletes its owned CloudflareTunnel, which in turn deletes its own owned CloudflareDNSRecord(s). Both finalizers have to finish before both objects are gone.

The hazard: if you delete the CloudflareGatewayConfiguration those objects authenticate through before their finalizers finish, an owned object that can no longer resolve its configuration has no credential left to call Cloudflare’s delete API with. Rather than get stuck forever, its finalizer is removed anyway — and the real Cloudflare-side resource is orphaned, left behind with no Kubernetes object pointing at it, needing manual cleanup. Both quickstarts’ Cleanup steps (Tunnel Mode, DNS Mode) wait for the owned objects to finish deleting before deleting the configuration, specifically to avoid this.

CloudflareTunnel and CloudflareDNSRecord are always created in their owning Gateway’s namespace, never the namespace of whatever Route triggered their creation — a Kubernetes owner reference can’t cross namespaces, so this is forced rather than a style choice. See Where generated objects are placed for the full reasoning.

Status and Conditions covers how to read the Accepted/Synced/Ready conditions these lifecycle events show up on.