Policy Attachment
A policy is a CRD that attaches to another object and changes how this project treats it,
without being that object’s own spec. CloudflareTunnelPolicy and CloudflareAccessPolicy are
the two policy kinds that exist today. Both follow the same attachment model, borrowed directly
from the Kubernetes Gateway API’s own policy-attachment convention — a targetRefs list on the
policy’s own spec, and a status.ancestors[] array reporting, per target, whether the
attachment took effect.
Policies versus infrastructure objects
Section titled “Policies versus infrastructure objects”Not every Cloudflare-facing CRD is a policy. CloudflareTunnel and CloudflareDNSRecord are
infrastructure objects: each one is the thing being managed — a real tunnel, a real DNS
record — with its own lifecycle, its own Cloudflare-side identity, and (normally) the controller
as its sole author. A policy is different: it never represents a Cloudflare resource on its own.
Instead it attaches to, and modifies the behavior of, some other object this project already
manages. CloudflareTunnelPolicy has no Cloudflare-side resource of its own at all — it only
ever changes how the CloudflareTunnel behind a Gateway gets configured. CloudflareAccessPolicy
is the same shape one layer further out: winning a hostname generates a
CloudflareAccessApplication as a side effect, but the policy itself still isn’t that
application — it’s the input that decides whether one exists and what it says.
That distinction is also why an infrastructure object can never be a policy’s target: its
lifecycle is already owned by the controller that generates it, not by whichever tenant happens
to write a policy. A policy’s targetRefs can only ever name a Gateway, or (for
CloudflareAccessPolicy) an HTTPRoute/GRPCRoute as well — never a CloudflareTunnel or a
CloudflareDNSRecord.
Attaching a policy: targetRefs
Section titled “Attaching a policy: targetRefs”Every policy carries spec.targetRefs, a list of same-namespace references — a policy can only
target objects in its own namespace, never across namespaces. The two policy kinds don’t accept
the same target shapes, because their fields don’t have the same scope:
CloudflareTunnelPolicyaccepts only whole-Gatewaytargets. SettingsectionNameon an entry is rejected outright:protocol,replicas,terminationGracePeriodSeconds, andautoscalingare whole-connector properties of the sharedcloudflaredprocess, not something a single listener could meaningfully override on its own.originRequestis scoped per-Gateway rather than per-connector — see the shared-connector note below — but a single listener still can’t carry its own value, sosectionNamestays rejected for the same reason.CloudflareAccessPolicyaccepts aGateway, anHTTPRoute, or aGRPCRoute, andsectionNameis allowed — but only on aGatewaytarget, where it names one listener.sectionNameon a Route target would have to name a route rule, and nothing about Access protection is scoped that finely today, so it’s rejected there too.
Both kinds reject repeated entries that could only ever resolve to the same attachment slot —
naming the same target (and, where relevant, the same sectionName) twice is a configuration
mistake the API server catches for free, rather than something that silently produces a
duplicate status entry.
A targetRefs entry naming an object that doesn’t exist in the policy’s namespace reports
Reason=TargetNotFound on that entry’s ancestor status — see
TargetNotFound. Naming a Gateway this project doesn’t
manage (one whose GatewayClass names a different controller entirely) is declined instead: no
ancestor entry is written for it at all, since Gateway API’s own convention is to report status
only for the targets a controller is actually responsible for — but it isn’t silent. Both
policy reconcilers emit a Warning TargetDeclined Event naming the target, which is the signal
to watch for here rather than the ancestor status.
Conflicts: oldest wins
Section titled “Conflicts: oldest wins”Two policies of the same kind can target the same object — nothing stops that — but only one of
them actually takes effect. The rule is oldest CreationTimestamp wins; on an exact tie, the
policy that sorts first alphabetically by namespace/name wins. The winning policy’s ancestor
entry reports Accepted=True; every losing policy’s entry for that same target reports
Accepted=False, Reason=Conflicted, naming the policy it lost to — see
Conflicted. This is the same oldest-wins rule, with the same
tiebreak, that this project already uses for colliding GatewayClass objects and for two
authors claiming the same DNS hostname — one conflict model, reused everywhere a collision can
happen, rather than a different rule invented per CRD.
The detail worth knowing before you attach two policies to a shared target on purpose:
conflicts have a first layer that’s scoped per attachment slot, not per object. A policy
targeting a Gateway as a whole object and a policy targeting one of that same Gateway’s
listeners by sectionName are not in conflict at this first layer — they occupy independent
slots there. Only two policies claiming the exact same slot (the same object, and the same
sectionName or lack of one) contend at this layer. This first layer is the only layer for
CloudflareTunnelPolicy (which doesn’t allow sectionName at all — see above), so for that
kind, a whole-Gateway and a section-scoped policy genuinely can both be Accepted at once —
except for the connector-level wrinkle below.
CloudflareAccessPolicy has a second layer on top of the first, because Access’s real
unit of contention is a hostname, not a targetRefs slot. Resolving a targetRefs entry (a
Gateway, listener, or Route) to the hostname(s) it covers happens first; then, for every
hostname the targetRefs of two different policies both resolve to — regardless of whether
those targetRefs occupy the same slot — only the policy with the oldest CreationTimestamp
wins that hostname.
A whole-Gateway policy and a section-scoped policy on one of that Gateway’s own listeners are
independent at the first layer, but if that listener carries a hostname, the whole-Gateway
policy’s resolved set includes it too — so the two do contend at the second layer, and the
younger one loses outright, reporting Accepted=False, Reason=Conflicted with a message like
lost the (hostname, ACCESS) claim for "example.com" to policy default/other-policy — the same
oldest-wins tiebreak as the first layer, with no preference for the more specific attachment.
The same applies across different kinds of target: an Access policy on a Gateway and
another on an HTTPRoute/GRPCRoute that happen to resolve to the same hostname contend too,
despite having entirely different slots. The only way two Access policies attached to
overlapping slots are both Accepted is when they resolve to no overlapping hostnames at all —
for example, a whole-Gateway policy and a section-scoped policy on a listener that carries no
hostname.
CloudflareTunnelPolicy has its own second layer, over a different unit: the shared connector
rather than a hostname. It isn’t obvious from targetRefs alone: two policies on two
different Gateways can still collide if both Gateways share one CloudflareTunnel through
tunnel sharing. Each Gateway’s own first-layer
attachment is adjudicated independently — both can be Accepted there — but protocol,
replicas, terminationGracePeriodSeconds, and autoscaling (the connector-wide fields) are
adjudicated a second time, over every Gateway bound to that tunnel. If every bound Gateway’s
effective settings for those four fields agree, nothing conflicts and all of them apply. While
autoscaling is enabled, replicas is ignored, so two policies that differ only in replicas
do not conflict; an absent autoscaling, enabled: false, and enabled: false with leftover
numbers all count as the same “off”. The moment two
of them disagree, the same oldest-wins rule picks one policy’s settings for the whole tunnel,
and every other policy bound to it reports Accepted=False, Reason=Conflicted against its own
Gateway — even one whose settings happened to coincidentally match the winner’s. originRequest
sits outside this second layer entirely: it stays scoped to each Gateway’s own routes, so two
Gateways sharing a tunnel can carry different originRequest values without conflicting — a
losing policy’s originRequest is simply withheld along with the rest of its settings.
status.ancestors[]
Section titled “status.ancestors[]”Every policy reports its attachment outcome through status.ancestors[] rather than a
top-level status.conditions field — one entry per target it actually got to report on, each
carrying a single Accepted condition. This is covered in full, including the complete Reason
vocabulary and why policies don’t carry this project’s usual Accepted/Synced/Ready triple,
in Conditions and in
Status and Conditions — it isn’t repeated here. The short version: check
status.ancestors[0].conditions[?(@.type=="Accepted")].status before assuming a policy actually
took effect on its target, rather than assuming kubectl apply succeeding means it did.
No inherited defaults today
Section titled “No inherited defaults today”There is no default-and-override mechanism between policies of the same kind. When a policy
wins a target, its spec applies in full — there is no field-level merging with any other policy,
and no namespace-wide “default policy” that a more specific one can selectively refine. A
losing policy’s fields never partially apply either: losing a conflict loses it outright, not
field by field. If you want one setting cluster-wide and a narrower override per Gateway, the
only mechanism today is CloudflareGatewayConfiguration.spec.tunnelDefaults (a configuration
field, not a policy) together with a CloudflareTunnelPolicy whose own fields take precedence
over it where set — and only for the three fields both levels carry (replicas,
terminationGracePeriodSeconds, autoscaling); see Tunnel Tuning for exactly which
fields have this chain and which don’t. A genuine inherited/hierarchical policy mechanism,
where one policy’s defaults get selectively overridden by another, is deliberately not built —
it’s reserved as a possible future addition, not something in place to configure around today.
Tunnel Tuning walks through every CloudflareTunnelPolicy field in
detail, with worked examples.