Policy targeting

Uses: Kong Mesh
Related Documentation
Minimum Version
Kong Mesh - 3.0
Previous Versions of this page

A Kong Mesh policy carries up to three selectors. They answer different questions, and each accepts a different set of kinds.

Selector

Answers

spec.targetRef Which proxies the policy is installed on.
spec.to[] Which destination the configuration applies to, for traffic those proxies send.
spec.rules[] Which client the configuration applies to, for traffic those proxies receive.

Not every policy has all three. For more information, see each policy’s reference docs.

Policy scope

On Kubernetes, the namespace a policy is created in restricts which proxies it can reach. targetRef narrows within that reach, it can’t widen it. The control plane works out the reach from the policy’s shape and records it in the kuma.io/policy-role label, which it computes on every write.

Policy

kuma.io/policy-role

Reaches

Created on the global control plane system Every proxy in the mesh, in every zone.
Created in the system namespace, kong-mesh-system, of a zone control plane system Every proxy in that zone. It isn’t distributed to other zones.
Created in an application namespace, where every to[] item selects one local destination. Each item is a MeshService or MeshHTTPRoute selected by these three labels and no others:
  • kuma.io/display-name
  • k8s.kuma.io/namespace, set to the policy’s own namespace
  • kuma.io/zone, set to the local zone
producer Every client of that destination, in any namespace and any zone.
Created in an application namespace, with at least one to[] item selected any other way. For example, by kuma.io/display-name alone, or with k8s.kuma.io/namespace set to another namespace. consumer Only proxies in the policy’s own namespace.
Created in an application namespace, with no to[] at all workload-owner Only proxies in the policy’s own namespace.

A policy can’t mix producer and consumer items. A policy that names a single destination in one to[] item and something broader in another is rejected on admission.

targetRef.kind: Mesh does not select the whole mesh on its own. In a consumer or workload-owner policy it selects every proxy in that policy’s namespace. Creating it in kong-mesh-system widens it to the whole zone, not the whole mesh: a policy created on a zone control plane stays in that zone. Only the global control plane, or a producer policy, reaches every zone.

A selector with only kuma.io/display-name, or with any extra label, makes the item a consumer item.

A zone control plane sets the kuma.io/origin: zone label on the policies created there. You don’t need to set it, and if you do, any value other than zone is rejected.

Universal has no namespaces, so every policy is system. A policy created on the global control plane reaches every zone, and one created on a zone control plane reaches only that zone.

Policy precedence

When several policies of the same type apply to the same traffic, the control plane merges them in order of priority. A higher-priority policy overrides the fields it sets, and inherits the fields it leaves out. The following criteria decide the order, from lowest to highest priority, and each one applies only when the previous ones tie:

  1. Top-level targetRef.kind: Mesh, then Dataplane. An omitted targetRef counts as Mesh.
  2. A Dataplane targetRef with sectionName ranks above one without.
  3. Origin: created on the global control plane, then created on a zone control plane.
  4. Role: system, then producer, consumer, and workload-owner.
  5. For outbound configuration, the to[].targetRef.kind: Mesh, then a destination kind (a MeshService with sectionName ranks above one without), then MeshHTTPRoute.
  6. Name: the policy whose name comes first alphabetically has the highest priority.

Outbound destinations and inbound identities

A destination is something the proxy chooses to call. Your proxy decides to send a request to backend, so naming that service is enough.

A client is remote and describes itself. If an inbound rule selected a caller by label or by service tag, it would be trusting the caller’s own claim about what it is, and a workload can assert any tag. So inbound matches on the SPIFFE ID that MeshIdentity issued and mTLS proves, which the caller can’t forge.

Accepted values by field

spec.targetRef

  • Mesh: applies a policy across the mesh
  • Dataplane: with labels, narrows the policy to a set of proxies. targetRef.sectionName narrows further, to one named listener or inbound of the selected proxy. Without labels, it selects the same proxies as Mesh, but with a higher priority.

spec.to[].targetRef

spec.to[].targetRef is a narrower selector than the top level, and the accepted kinds vary by policy:

Kind

Accepted by

Mesh Every policy that has a to array, except MeshHTTPRoute and MeshTCPRoute, which must name a destination.
MeshService, MeshExternalService, MeshMultiZoneService Most policies with a to array.
MeshHTTPRoute MeshAccessLog, MeshLoadBalancingStrategy, MeshRetry and MeshTimeout only.

MeshRateLimit and MeshFaultInjection are even narrower: they accept kind: Mesh only, and only when the top-level targetRef is kind: Mesh or omitted. With kind: Dataplane, to must be empty.

spec.rules[]

spec.rules[] defines where a policy supports inbound configuration. Each rule may carry matches, and a match selects on one of two fields:

Field

Matches on

type

spiffeID The SPIFFE ID of the calling workload. Exact or Prefix
sni The SNI carried on the TLS connection. Used on zone egress, where the destination is identified by SNI rather than by client identity. Exact only

A match needs at least one of the two. A Prefix on a SPIFFE ID is how you address a group: the ID is structured as spiffe://<trust-domain>/ns/<namespace>/sa/<service-account>, so a prefix ending at the namespace selects every workload in it.

Some policies accept rules without matches, applying to all inbound traffic.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!