Endpoints and selectors
About 1010 words
An endpoint is a catalog entity whose pods run on the policy's cluster. Each one gets a CiliumClusterwideNetworkPolicy of its own that selects its pods and allows its flows. This guide covers how an entity becomes an endpoint and how to make its selector match all of its pods, and nothing else.
The examples come from docs/examples/prod, a catalog of a cluster with a web shop and the usual platform components.
What makes an endpoint
A Component or Resource is an endpoint of a policy when:
- it
dependsOnthe Resource the policy names inspec.cluster, and - it says where its pods are: a namespace, a label selector, or both.
APIs, Systems and other kinds are never endpoints. A Component that depends on the cluster but has neither a namespace nor a selector isn't one either; as far as the catalog says, it has no pods. The cluster Resource itself is skipped.
| Annotation | Says |
|---|---|
backstage.io/kubernetes-namespace | the namespace of its pods |
specht-labs.de/cilium-namespaces | its pods' namespaces, comma-separated, when there are several; replaces the one above |
backstage.io/kubernetes-label-selector | which pods are its own |
specht-labs.de/cilium-selector | its pods, when the label selector above doesn't select them all; replaces it. * is every pod in the namespaces |
The two backstage.io/ annotations are the ones Backstage's Kubernetes plugin reads, so an entity that shows its pods on the Kubernetes tab usually has them already.
With a namespace and no selector, the endpoint is every pod in the namespace, as with cilium-selector: '*'.
How the selector renders
The namespace becomes the io.kubernetes.pod.namespace label, next to the selector's own labels. For component:orders, with backstage.io/kubernetes-namespace: orders and backstage.io/kubernetes-label-selector: app=orders:
endpointSelector:
matchLabels:
io.kubernetes.pod.namespace: orders
app: ordersThe same selector stands for the endpoint wherever another policy names it, in fromEndpoints or toEndpoints.
The label selector takes the Kubernetes syntax: app=x (or app==x), tier!=db, env in (a,b), env notin (a,b), key (the label exists) and !key (it doesn't), separated by commas. Equality goes to matchLabels, the rest to matchExpressions.
The policy is named after the entity: allow-<kind>-<name>, with the entity's namespace between kind and name when it isn't default, in lowercase, with anything but letters, digits, dots and hyphens turned into -.
When the Kubernetes tab's selector isn't enough
The Kubernetes tab only has to find the pods worth showing. A network policy has to cover every pod that sends or receives the endpoint's traffic, or the ones it misses are denied. cert-manager, for example, runs a controller, a webhook and a CA injector, and its Helm chart labels them differently. When every pod in the namespace belongs to the endpoint, say so:
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: cert-manager
annotations:
backstage.io/kubernetes-namespace: cert-manager
backstage.io/kubernetes-label-selector: app.kubernetes.io/instance=cert-manager
specht-labs.de/cilium-selector: '*'
specht-labs.de/cilium-ports: tcp:10250
specht-labs.de/cilium-allow: entity:kube-apiserver, entity:remote-node
spec:
type: service
lifecycle: production
owner: group:platform
consumesApis: [kubernetes-api, letsencrypt-acme]
dependsOn: [resource:prod]endpointSelector:
matchLabels:
io.kubernetes.pod.namespace: cert-managerThe Kubernetes tab keeps its selector; only the network policy uses cilium-selector. Quote the *: in YAML, a bare * starts an alias.
Operators that run their own proxies or instances are the other common case: the operator's Helm release labels the operator, while the pods it creates carry labels of their own. Either widen the selector with cilium-selector, or model what the operator creates as an entity of its own (a database Resource for each CloudNativePG cluster, for instance) and connect the two with a rule entity.
Pods in several namespaces
Some components are spread over namespaces: Cluster API's core and each of its providers live in their own. List them all in specht-labs.de/cilium-namespaces:
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: cluster-api
annotations:
specht-labs.de/cilium-namespaces: capi-system, capi-kubeadm-bootstrap-system, capi-kubeadm-control-plane-system
specht-labs.de/cilium-allow: entity:kube-apiserver, entity:remote-node
specht-labs.de/cilium-ports: tcp:9443
spec:
type: service
lifecycle: production
owner: group:platform
consumesApis: [kubernetes-api]
dependsOn: [resource:prod, resource:workload-cluster-api]Several namespaces render as an In expression:
endpointSelector:
matchExpressions:
- key: io.kubernetes.pod.namespace
operator: In
values:
- capi-system
- capi-kubeadm-bootstrap-system
- capi-kubeadm-control-plane-systembackstage-cilium-render and the console write this selector as {capi-system,capi-kubeadm-bootstrap-system,capi-kubeadm-control-plane-system}/*: the namespaces, a slash, then the labels, or * for every pod.
Things to avoid
- A selector without a namespace. It selects matching pods in every namespace, and the policy warns:
no backstage.io/kubernetes-namespace or specht-labs.de/cilium-namespaces: its policy selects app=worker in every namespace. - Namespaces that are only partly excluded. If some of an endpoint's namespaces are in
spec.defaultDeny.excludeand some aren't, its pods are treated differently depending on where they run, and the policy warns. An endpoint counts as excluded only when all of its namespaces are. - Two endpoints selecting the same pods. Each gets its own policy; Cilium applies both, and a pod gets the union of their allows. The plugin doesn't detect overlaps.
- Pods on the host network. Cilium doesn't treat them as endpoints of their own: their traffic comes from the node, as
hostorremote-node. Selecting them in the catalog renders a policy that never applies. See Cilium identities you'll meet.
The endpoint's own pods
With spec.allowWithin left at its default, true, every endpoint's pods reach each other on every port: replicas of a database talking to each other, a controller and its webhook. Each policy has a rule for it, commented its own pods. Set allowWithin: false on the policy to leave it out; an endpoint then gets a policy only if it has flows to or from something else.
