From 9605e8eef71c361aaf65e807392a48157c4dd918 Mon Sep 17 00:00:00 2001 From: sandert-k8s Date: Sun, 1 Nov 2026 14:33:31 +0100 Subject: [PATCH 1/3] feat: add a tiered networkpolicy design Signed-off-by: sandert-k8s --- content/dictionary.txt | 16 + .../operating/best-practices/networking.md | 707 +++++++++++++++++- 2 files changed, 714 insertions(+), 9 deletions(-) diff --git a/content/dictionary.txt b/content/dictionary.txt index bcb0812..af894d4 100644 --- a/content/dictionary.txt +++ b/content/dictionary.txt @@ -14,6 +14,8 @@ CaaS CapsuleConfiguration CapsuleConfigurationSpec ClusterIP +ClusterNetworkPolicies +ClusterNetworkPolicy ClusterRole ClusterRoles ConfigMap @@ -122,6 +124,7 @@ ServiceAccounts ServiceMonitor StorageClass StorageClasses +Tigera TLS TLS-terminated TenantResource @@ -176,6 +179,7 @@ init-time ipBlock k3d keycloak +kindnet kubeconfig kubectl kubernetes @@ -239,3 +243,15 @@ TenantOwners ValidatingAdmissionPolicy finalizers HelmRelease +Cilium +CiliumClusterwideNetworkPolicies +CiliumClusterwideNetworkPolicy +ClusterRoleBinding +GlobalNetworkPolicies +GlobalNetworkPolicy +Overridable +unlabeled +kube-apiserver +kube-network-policies +kube-ovn +ovn diff --git a/content/en/docs/operating/best-practices/networking.md b/content/en/docs/operating/best-practices/networking.md index 8aac411..138c5d6 100644 --- a/content/en/docs/operating/best-practices/networking.md +++ b/content/en/docs/operating/best-practices/networking.md @@ -177,19 +177,27 @@ Also review these paths outside the Service type rules: ## Network-Policies +Start with native Kubernetes NetworkPolicies. In most cases they are enough to +isolate Tenants: a default-deny baseline with allowances for the same Tenant, +DNS and platform ingress, created in every Tenant namespace with a +GlobalTenantResource. Use the +[advanced policies](#advanced-network-policies) only when tenant owners must +not be able to widen the access. + +### Native NetworkPolicies with GlobalTenantResource + Use [GlobalTenantResource](/docs/replications/global/) to distribute a common network profile across Tenant namespaces. Start with default-deny ingress and egress, then allow the flows applications need. The CNI must support and enforce native Kubernetes [NetworkPolicies](https://kubernetes.io/docs/concepts/services-networking/network-policies/). -### Native NetworkPolicies with GlobalTenantResource - Native NetworkPolicies are additive: an additional allow policy can widen the effective access. Protecting the replicated baseline alone is therefore not a mandatory isolation boundary. Restrict tenant permissions to create additional NetworkPolicies when the platform must own that boundary, or use an -administrator-enforced policy mechanism supported by your CNI. Both source -egress and destination ingress must allow a connection. See the Kubernetes +[administrator-enforced policy](#advanced-network-policies) supported by your +CNI. Both source egress and destination ingress must allow a connection. See the +Kubernetes [policy-combination rules](https://kubernetes.io/docs/concepts/services-networking/network-policies/#the-two-sorts-of-pod-isolation). This example creates four policies per selected namespace. Verify replication @@ -198,16 +206,14 @@ ingress before relying on the profile. Replication readiness confirms resource delivery; it does not prove the CNI has applied the rules. Tune `resyncPeriod` for the number of namespaces you manage. - - - This complete, cluster-scoped GlobalTenantResource creates four native `networking.k8s.io/v1` NetworkPolicies in every namespace of every selected Tenant. It has no `metadata.namespace`. `tenantSelector: {}` selects all Tenants; replace it with a label selector to roll out the profile gradually. Use `resources[].namespaceSelector` when only some namespaces need this profile. -The [replication identity](/docs/replications/global/#required-permissions) must -have permission to manage NetworkPolicies in the destination namespaces. +The default Capsule ServiceAccount (`cluster-admin`) can manage them. A +dedicated [replication ServiceAccount](/docs/replications/global/#required-permissions) +needs permission to manage NetworkPolicies in the destination namespaces. ```yaml apiVersion: capsule.clastix.io/v1beta2 @@ -393,6 +399,81 @@ DNS selector must match your DNS deployment. Test the profile with the Cilium version you run, for example with `cilium policy get` and connectivity checks between Tenants. +### Calico NetworkPolicies + +With Calico, the same baseline can be distributed as a single namespaced +Calico `NetworkPolicy` (`projectcalico.org/v3`) per namespace. Calico policies +select peers with selector expressions, so the Tenant and platform namespaces +are matched on their labels with `namespaceSelector`. The policy lives in +Calico's `default` tier, next to native NetworkPolicies. + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: GlobalTenantResource +metadata: + name: tenant-calico-baseline +spec: + scope: Namespace + tenantSelector: {} + resyncPeriod: 60s + resources: + - policy: + creation: Owner + protect: true + deletion: Remove + rawItems: + - apiVersion: projectcalico.org/v3 + kind: NetworkPolicy + metadata: + name: capsule-tenant-baseline + spec: + selector: all() # All Pods in the namespace + types: [Ingress, Egress] + ingress: + # Pods in any namespace of the same Tenant, including this one. + - action: Allow + source: + namespaceSelector: "capsule.clastix.io/tenant == '{{tenant.name}}'" + # Pods in trusted platform namespaces. + - action: Allow + source: + namespaceSelector: "company.com/system == 'true' && !has(capsule.clastix.io/tenant)" + egress: + # Pods in any namespace of the same Tenant, including this one. + - action: Allow + destination: + namespaceSelector: "capsule.clastix.io/tenant == '{{tenant.name}}'" + # Cluster DNS. + - action: Allow + protocol: UDP + destination: + namespaceSelector: "kubernetes.io/metadata.name == 'kube-system'" + selector: "k8s-app == 'kube-dns'" + ports: [53] + - action: Allow + protocol: TCP + destination: + namespaceSelector: "kubernetes.io/metadata.name == 'kube-system'" + selector: "k8s-app == 'kube-dns'" + ports: [53] +``` + +Once a Calico policy with `types: [Ingress, Egress]` selects a Pod, traffic in +that direction that no rule allows is denied, so this single policy has the same +effect as the four native policies above. The platform selector requires the +absence of the `capsule.clastix.io/tenant` label, so a Tenant owner cannot make +their namespace trusted by adding `company.com/system`. Still +[deny that label](#deny-namespace-metadata) on Tenant namespaces. DNS is limited +to port 53 on the DNS Pods. Check that the DNS selector matches your DNS Pods. + +Tenant owners have no Calico API permissions through Capsule, so they cannot +create, change or delete Calico policies, and `protect: true` also rejects +changes by cluster administrators. They can still create native +NetworkPolicies, and those are additive to this policy. An owner can widen the +access, including across Tenants, with an allow-all NetworkPolicy. When the +platform must own the isolation boundary, use the +[Calico tiers](#calico-tiers) below. + ### Deny Namespace Metadata The platform-ingress policy trusts namespaces carrying @@ -437,3 +518,611 @@ Metadata validation skips unchanged values on updates. This rule therefore does not remove an existing label or reject an update that leaves its value unchanged. Remove any existing tenant-controlled `company.com/system` labels before enabling the platform-ingress allowance. + +## Advanced Network Policies + +Native NetworkPolicies are namespaced, so a tenant owner with NetworkPolicy +permissions can always widen the access. When the platform must own the +isolation boundary, enforce it with a cluster-level policy that is evaluated +before any NetworkPolicy a tenant owner writes, and let Capsule create it once +per Tenant with a [GlobalTenantResource](/docs/replications/global/) using +`scope: Tenant`. The Tenant label `capsule.clastix.io/tenant` on every Tenant +namespace is what the policies select on, and Capsule resets changes to it. + +The mechanism depends on what enforces policies in your cluster: + +| Your cluster | Choose | Why | +|---|---|---| +| kube-network-policies, Calico, Cilium or kube-ovn, and you want one portable manifest | [ClusterNetworkPolicies](#clusternetworkpolicies) | One upstream API that all of these implement. The API is still alpha (`v1alpha2`). | +| Cilium and you prefer its stable API | [Cilium policies](#cilium) | `cilium.io/v2` is stable, but there are no tiers and `enableDefaultDeny` must be set carefully. | +| Calico and you prefer a stable API | [Calico tiers](#calico-tiers) | `projectcalico.org/v3` is stable. | +| Calico and tenants need their own ordered or deny rules | [Calico tier per Tenant](#calico-tier-per-tenant) | Owners get a tier they alone can use. Tenants can write deny rules and ordered rules that run before native NetworkPolicies and cannot override the platform tier. | + +All approaches below give the same isolation. See +[Verify the Behavior](#verify-the-behavior) for what to check on your cluster. + +### ClusterNetworkPolicies + +Native NetworkPolicies are namespaced, so a tenant owner can always create +another one that widens access. The upstream +[ClusterNetworkPolicy](https://network-policy-api.sigs.k8s.io/api-overview/) +API (`policy.networking.k8s.io/v1alpha2`) adds cluster-scoped policies that +are evaluated in tiers: + +| Tier | Evaluated | Overridable by tenant NetworkPolicies | +|---|---|---| +| `Admin` | First. `Accept` and `Deny` are final decisions. | No | +| NetworkPolicy | Second. Namespaced policies written by tenant owners. | n/a | +| `Baseline` | Last. Default connectivity for traffic nothing else decided. | Yes | + +Use the `Admin` tier for the isolation boundary the platform owns. Tenant +owners have no permissions on cluster-scoped `ClusterNetworkPolicy` objects +through Capsule, and nothing a tenant creates in its namespaces is evaluated +before the `Admin` tier. Do not grant `policy.networking.k8s.io` write access +to tenant users or groups in any other ClusterRole. + +#### Choose an Implementation + +The API is alpha, and the examples only use fields from the standard channel. +These policies only take effect when a component in your cluster implements the +API. A CNI that only enforces native NetworkPolicies accepts the objects but +ignores them without reporting an error, so always test that traffic is +actually denied. The upstream +[implementation list](https://network-policy-api.sigs.k8s.io/implementations/) +has more. + +| Implementation | What to enable | +|---|---| +| [kube-network-policies](https://github.com/kubernetes-sigs/kube-network-policies) | Adds ClusterNetworkPolicy support to a CNI that has none, and enforces native NetworkPolicies as well. Disable the CNI's own NetworkPolicy enforcement so it is the only enforcer, as explained below. Install the `ClusterNetworkPolicy` CRD, then its [`install-cnp.yaml`](https://github.com/kubernetes-sigs/kube-network-policies/blob/main/install-cnp.yaml) manifest. | +| [kube-ovn](https://github.com/kubeovn/kube-ovn), v1.16 and later | Supported, but disabled by default. Install with `ENABLE_ANP=true` (Helm: `func.ENABLE_ANP=true`). The installer does not ship the CRDs. Install the `ClusterNetworkPolicy` CRD yourself, plus the `AdminNetworkPolicy` and `BaselineAdminNetworkPolicy` v1alpha1 CRDs, because the controller watches all three. kube-ovn enforces native NetworkPolicies itself, so there is no second enforcer to disable. Priorities must be between 0 and 399 and a policy can have at most 25 rules. A policy outside these limits is only reported in the `kube-ovn-controller` log. | +| Calico, v3.32 and later | Supported. The Tigera operator installs the CRD, and the policies run in Calico's `kube-admin` tier. | +| Cilium, v1.20 and later | Disabled by default. Set the Helm value `k8sClusterNetworkPolicy.enabled=true`, install the [CRD](https://github.com/kubernetes-sigs/network-policy-api/tree/main/config/crd/standard) yourself because the chart does not ship it, and set `policyCIDRMatchMode: nodes` so CIDR rules match node IPs. See the [Cilium documentation](https://docs.cilium.io/en/stable/network/kubernetes/policy/#kubernetes-clusternetworkpolicy). | + +Let a single component enforce both ClusterNetworkPolicies and native +NetworkPolicies. If a second component enforces NetworkPolicies on its own, it +cannot see an `Accept` from the first and still drops the traffic, so the +allowances below have no effect. This matters when you add +kube-network-policies to a CNI that has its own NetworkPolicy enforcement, such +as kindnet in a default kind cluster. + +With Cilium, the agents read `policyCIDRMatchMode` at startup. A `helm upgrade` +does not restart them, so restart the `cilium` DaemonSet after changing it. Only +the value `nodes` is accepted; other values, such as `pods,nodes`, make the +agent exit on startup. + +#### Baseline per Tenant + +This cluster-scoped GlobalTenantResource creates two ClusterNetworkPolicies +for every Tenant, named after it. `scope: Tenant` renders the templates once +per Tenant instead of once per namespace, and `{{tenant.name}}` is replaced with +the Tenant name. `tenantSelector: {}` selects all Tenants; use a label +selector to roll the baseline out gradually. + +* `tenant--infrastructure` (priority `5`) accepts what every Tenant needs: + DNS, the Kubernetes API server, and traffic to and from platform namespaces. +* `tenant--isolation` (priority `10`) denies traffic to and from every + other Tenant, in both directions. + +Lower priorities win within a tier, so the `Accept` rules run before the `Deny` +rules. + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: GlobalTenantResource +metadata: + name: tenant-network-baseline +spec: + scope: Tenant + tenantSelector: {} + resyncPeriod: 60s + resources: + - policy: + deletion: Remove + rawItems: + - apiVersion: policy.networking.k8s.io/v1alpha2 + kind: ClusterNetworkPolicy + metadata: + name: tenant-{{tenant.name}}-infrastructure + spec: + tier: Admin + priority: 5 + subject: + namespaces: + matchLabels: + capsule.clastix.io/tenant: "{{tenant.name}}" + ingress: + - name: allow-platform-ingress + action: Accept + from: + - namespaces: + matchLabels: + company.com/system: "true" + matchExpressions: + - key: capsule.clastix.io/tenant + operator: DoesNotExist + egress: + - name: allow-platform-egress + action: Accept + to: + - namespaces: + matchLabels: + company.com/system: "true" + matchExpressions: + - key: capsule.clastix.io/tenant + operator: DoesNotExist + - name: allow-dns + action: Accept + to: + - pods: + namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + k8s-app: kube-dns + protocols: + - udp: + destinationPort: + number: 53 + - tcp: + destinationPort: + number: 53 + - name: allow-kube-api + action: Accept + to: + - networks: + - 10.0.0.10/32 + protocols: + - tcp: + destinationPort: + number: 6443 + + - apiVersion: policy.networking.k8s.io/v1alpha2 + kind: ClusterNetworkPolicy + metadata: + name: tenant-{{tenant.name}}-isolation + spec: + tier: Admin + priority: 10 + subject: + namespaces: + matchLabels: + capsule.clastix.io/tenant: "{{tenant.name}}" + ingress: + - name: deny-from-other-tenants + action: Deny + from: + - namespaces: + matchExpressions: + - key: capsule.clastix.io/tenant + operator: Exists + - key: capsule.clastix.io/tenant + operator: NotIn + values: ["{{tenant.name}}"] + egress: + - name: deny-to-other-tenants + action: Deny + to: + - namespaces: + matchExpressions: + - key: capsule.clastix.io/tenant + operator: Exists + - key: capsule.clastix.io/tenant + operator: NotIn + values: ["{{tenant.name}}"] +``` + +Replace `10.0.0.10/32` and port `6443` with the API server endpoint address +and port. The API server cannot be selected by labels, so the policy selects +it by address. CNIs evaluate the destination after Service translation, so use +the endpoint from +`kubectl get endpointslices -n default -l kubernetes.io/service-name=kubernetes`, +not the `kubernetes` Service IP. Update the policy if the endpoints change. +On Cilium without `policyCIDRMatchMode: nodes`, the `allow-kube-api` rule does +not match: DNS keeps working and API access stays blocked. The +[Calico](#calico-tiers) and [Cilium](#cilium) sections select the API server +without an address. As with the +[native DNS policy](#native-networkpolicies-with-globaltenantresource), check +the labels of your DNS Pods. + +What the policies do: + +* **Platform namespaces.** Namespaces labeled `company.com/system: "true"` and + without the `capsule.clastix.io/tenant` label are trusted in both + directions, for example monitoring or ingress controllers. The + `DoesNotExist` expression matters: without it, two Tenant owners could each + label their own namespace `company.com/system: "true"` and bypass the + isolation. Still reserve the label and + [deny it on Tenant namespaces](#deny-namespace-metadata). Narrow the peers + and ports for your platform components. +* **Isolation scope.** The standard API cannot compare a label value between + two namespaces, so each Tenant gets its own policy naming itself. A + namespace without the Tenant label, such as a platform namespace, is not + affected by the `Deny` rules. +* **Within a Tenant.** Traffic inside a Tenant, including between its + namespaces, stays under the control of the Tenant's NetworkPolicies. Add the + [native baseline](#native-networkpolicies-with-globaltenantresource) if you + also want default-deny and same-Tenant allowances. +* **New and removed Tenants.** A new Tenant gets its policies within seconds + of creation, and the existing Tenants are denied from it immediately. + Deleting a Tenant removes its policies (`deletion: Remove`). +* **Protection.** By default `protect: true` makes Capsule reject changes to + the generated policies through its admission webhook, including changes by + cluster administrators. Change the GlobalTenantResource instead. +* **Policy order.** Policies selecting the same Pod and the same connection + need different priorities because the order between equal priorities is + not defined. Policies of different Tenants can share a priority. + +### Cilium + +Cilium's own `CiliumClusterwideNetworkPolicy` (`cilium.io/v2`) is a stable +alternative to the alpha ClusterNetworkPolicy. It has no tiers, which changes +how you write the policies: + +* Any rule, including a deny rule, puts the selected Pods into default deny + for that direction. Without `enableDefaultDeny` set to `false` for both + directions, the policies would block all other traffic of every Tenant Pod, + including traffic within the Tenant. +* A deny rule takes precedence over allow rules, so tenant NetworkPolicies + cannot override the isolation. +* Selectors use the `io.cilium.k8s.namespace.labels.` prefix for namespace + labels. +* The `kube-apiserver` entity selects the API server without a CIDR, so + `policyCIDRMatchMode` is not needed. A `toServices` rule for the + `kubernetes` Service in the `default` namespace also works. + +This GlobalTenantResource creates the same baseline as the +[ClusterNetworkPolicies](#baseline-per-tenant), once per Tenant: + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: GlobalTenantResource +metadata: + name: tenant-network-baseline +spec: + scope: Tenant + tenantSelector: {} + resyncPeriod: 60s + resources: + - policy: + deletion: Remove + rawItems: + - apiVersion: cilium.io/v2 + kind: CiliumClusterwideNetworkPolicy + metadata: + name: tenant-{{tenant.name}}-infrastructure + spec: + endpointSelector: + matchLabels: + io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant: "{{tenant.name}}" + enableDefaultDeny: + ingress: false + egress: false + ingress: + - fromEndpoints: + - matchLabels: + io.cilium.k8s.namespace.labels.company.com/system: "true" + matchExpressions: + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: DoesNotExist + egress: + - toEndpoints: + - matchLabels: + io.cilium.k8s.namespace.labels.company.com/system: "true" + matchExpressions: + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: DoesNotExist + - toEndpoints: + - matchLabels: + io.kubernetes.pod.namespace: kube-system + k8s-app: kube-dns + toPorts: + - ports: + - port: "53" + protocol: ANY + - toEntities: + - kube-apiserver + + - apiVersion: cilium.io/v2 + kind: CiliumClusterwideNetworkPolicy + metadata: + name: tenant-{{tenant.name}}-isolation + spec: + endpointSelector: + matchLabels: + io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant: "{{tenant.name}}" + enableDefaultDeny: + ingress: false + egress: false + ingressDeny: + - fromEndpoints: + - matchExpressions: + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: Exists + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: NotIn + values: ["{{tenant.name}}"] + egressDeny: + - toEndpoints: + - matchExpressions: + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: Exists + - key: io.cilium.k8s.namespace.labels.capsule.clastix.io/tenant + operator: NotIn + values: ["{{tenant.name}}"] +``` + +Tenant owners have no permissions on `ciliumclusterwidenetworkpolicies`. + +### Calico Tiers + +Calico evaluates policies in ordered tiers, like the ClusterNetworkPolicy +tiers, but with the stable `projectcalico.org/v3` API. Policies in a tier that +is ordered before the `default` tier, where Calico stores native +NetworkPolicies, cannot be changed or overridden by tenant owners. Prefer this +over ClusterNetworkPolicies on Calico if you want a stable API. + +Create one tier for the platform. `order: 500` places it after Calico's own +`calico-system` tier (100) and before `kube-admin` (1000, the tier used for +ClusterNetworkPolicies). `defaultAction: Pass` is required: the default is +`Deny`, which blocks all tenant traffic that no policy in the tier matches. + +```yaml +apiVersion: projectcalico.org/v3 +kind: Tier +metadata: + name: capsule-tenancy +spec: + order: 500 + defaultAction: Pass +``` + +This GlobalTenantResource creates two GlobalNetworkPolicies per Tenant. Policy +names must start with the tier name. The `services` destination selects the +endpoints of the `kubernetes` Service and follows them when they change, so +there is no API server address to maintain. DNS is selected by Pod labels and +limited to port 53, because Calico does not allow `ports` together with +`services`; a `services` rule for `kube-dns` would open every port of the DNS +Pods, including their metrics port. Check the labels of your DNS Pods. The +platform namespaces and the isolation work like in the +[ClusterNetworkPolicy baseline](#baseline-per-tenant), including the +`!has(capsule.clastix.io/tenant)` guard. + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: GlobalTenantResource +metadata: + name: tenant-network-baseline +spec: + scope: Tenant + tenantSelector: {} + resyncPeriod: 60s + resources: + - policy: + deletion: Remove + rawItems: + - apiVersion: projectcalico.org/v3 + kind: GlobalNetworkPolicy + metadata: + name: capsule-tenancy.{{tenant.name}}-infrastructure + spec: + tier: capsule-tenancy + order: 5 + namespaceSelector: "capsule.clastix.io/tenant == '{{tenant.name}}'" + selector: all() + types: [Ingress, Egress] + ingress: + - action: Allow + source: + namespaceSelector: "company.com/system == 'true' && !has(capsule.clastix.io/tenant)" + egress: + - action: Allow + destination: + namespaceSelector: "company.com/system == 'true' && !has(capsule.clastix.io/tenant)" + - action: Allow + protocol: UDP + destination: + namespaceSelector: "kubernetes.io/metadata.name == 'kube-system'" + selector: "k8s-app == 'kube-dns'" + ports: [53] + - action: Allow + protocol: TCP + destination: + namespaceSelector: "kubernetes.io/metadata.name == 'kube-system'" + selector: "k8s-app == 'kube-dns'" + ports: [53] + - action: Allow + destination: + services: + name: kubernetes + namespace: default + + - apiVersion: projectcalico.org/v3 + kind: GlobalNetworkPolicy + metadata: + name: capsule-tenancy.{{tenant.name}}-isolation + spec: + tier: capsule-tenancy + order: 10 + namespaceSelector: "capsule.clastix.io/tenant == '{{tenant.name}}'" + selector: all() + types: [Ingress, Egress] + ingress: + - action: Deny + source: + namespaceSelector: "has(capsule.clastix.io/tenant) && capsule.clastix.io/tenant != '{{tenant.name}}'" + egress: + - action: Deny + destination: + namespaceSelector: "has(capsule.clastix.io/tenant) && capsule.clastix.io/tenant != '{{tenant.name}}'" +``` + +Tenant owners cannot create policies in this tier. Calico denies the request +unless the user may `get` the tier, which Capsule does not grant. Note that +`kubectl auth can-i` only checks the resource and reports `yes`; the real +request is forbidden. + +#### Calico Tier per Tenant + +Let each Tenant also have a tier that its owners can use. Owners then write +Calico `NetworkPolicy` objects with explicit `Allow`, `Deny` and `Pass` rules +and an order, which native NetworkPolicies cannot express. Tier order `2000` +places the Tenant tiers after the platform tier and `kube-admin`, and before +the `default` tier. + +Calico needs two permissions, and they have different scopes: + +* `get` on the Tier. Tiers are cluster-scoped, so this needs a + ClusterRoleBinding. Limit it with `resourceNames` to the Tenant's own tier. +* Access to `tier.networkpolicies` with `resourceNames: [".*"]`. Bind + this role in the Tenant namespaces only. A ClusterRoleBinding would let the + owner create policies in the namespaces of other Tenants, where a deny rule + could break their traffic. + +A RoleBinding to a role that allows `get` on the Tier does not work: Calico +denies the request with `user cannot get tier`. + +This GlobalTenantResource creates, per Tenant, the Tier, both ClusterRoles, and +a ClusterRoleBinding for the Tenant owners. The template reads the owners +from the Tenant. + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: GlobalTenantResource +metadata: + name: tenant-calico-tiers +spec: + scope: Tenant + tenantSelector: {} + resyncPeriod: 60s + resources: + - policy: + deletion: Remove + rawItems: + - apiVersion: projectcalico.org/v3 + kind: Tier + metadata: + name: tenant-{{tenant.name}} + spec: + order: 2000 + defaultAction: Pass + + - apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRole + metadata: + name: tenant-{{tenant.name}}-calico-tier + rules: + - apiGroups: ["projectcalico.org"] + resources: ["tiers"] + resourceNames: ["tenant-{{tenant.name}}"] + verbs: ["get"] + + - apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRole + metadata: + name: tenant-{{tenant.name}}-calico-policies + rules: + - apiGroups: ["projectcalico.org"] + resources: ["tier.networkpolicies"] + resourceNames: ["tenant-{{tenant.name}}.*"] + verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] + generators: + - missingKey: error + template: | + apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRoleBinding + metadata: + name: tenant-{{ $.tenant.metadata.name }}-calico-tier + roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tenant-{{ $.tenant.metadata.name }}-calico-tier + subjects: + {{- range $.tenant.spec.owners }} + - kind: {{ .kind }} + name: {{ .name | quote }} + apiGroup: rbac.authorization.k8s.io + {{- end }} +``` + +Then bind the policy role in the Tenant namespaces with +`additionalRoleBindings`. Capsule creates the RoleBinding in every namespace of +the Tenant, including future ones. The ClusterRole is named after the Tenant, +so every Tenant references its own: + +```yaml +apiVersion: capsule.clastix.io/v1beta2 +kind: Tenant +metadata: + name: solar +spec: + owners: + - kind: User + name: solar-owner + additionalRoleBindings: + - clusterRoleName: tenant-solar-calico-policies + subjects: + - kind: User + name: solar-owner + apiGroup: rbac.authorization.k8s.io +``` + +The owner can now create namespaced policies in the `tenant-solar` tier, for +example to deny `solar-dev` towards `solar-prod` without a default-deny policy: + +```yaml +apiVersion: projectcalico.org/v3 +kind: NetworkPolicy +metadata: + name: tenant-solar.deny-dev-to-prod + namespace: solar-prod +spec: + tier: tenant-solar + order: 10 + selector: all() + types: [Ingress] + ingress: + - action: Deny + source: + namespaceSelector: kubernetes.io/metadata.name == 'solar-dev' +``` + +Owners can only use their own tier, and only in their own namespaces. An +`Allow` in a tenant tier cannot override the platform `Deny`, because the +platform tier is evaluated first. A `Pass` defers to native NetworkPolicies. + +When to use it instead of the platform tier alone: + +* Tenants need deny rules, rule ordering or `Pass`, for example to block one + namespace from another without a default-deny policy that also blocks + everything else. +* Different Tenants need different tier semantics without the platform + editing policies for them. + +It does not give a stronger isolation boundary than the platform tier, and it +ties the tenant workloads' policies to Calico. + +### Verify the Behavior + +Support differs between CNIs, so test the baseline on your cluster, for example +with `curl` between Pods. Use at least two Tenants, a platform namespace +labeled `company.com/system: "true"`, and an unlabeled namespace, each with a +web Pod, a Service and a client Pod. Expect: + +| Check | Expected result | +|---|---| +| Cross-Tenant traffic, by Pod IP and by Service, with the baseline | Denied in both directions | +| Cross-Tenant traffic after owners applied allow-all NetworkPolicies | Still denied | +| Traffic within a Tenant | Allowed, controlled by NetworkPolicies | +| DNS and API server access with an owner default-deny policy for ingress and egress | Allowed | +| Traffic from and to the platform namespace with that owner default-deny policy | Allowed in both directions | +| Traffic from and to an unlabeled namespace with that policy | Denied | +| Other egress from that namespace, such as the internet | Still blocked by the owner's policy | +| Owner labels own namespaces `company.com/system: "true"`, one Tenant or both | Still isolated | +| Owner `create`, `update`, `patch` and `delete` on the policy resources | Forbidden | +| Owner removing or changing the Tenant label on a namespace | Reverted by Capsule | +| New Tenant | Policies exist within seconds, and the Tenant is isolated from the others | +| Deleted Tenant, or deleted GlobalTenantResource | Generated policies removed | + +A cluster administrator cannot edit a generated policy either while +`protect: true` is set; change the GlobalTenantResource. From e7d130c069652946e0d8d51e3670a03c4129507b Mon Sep 17 00:00:00 2001 From: sandert-k8s Date: Fri, 13 Nov 2026 11:21:48 +0100 Subject: [PATCH 2/3] fix: feedback Signed-off-by: sandert-k8s --- .../docs/operating/best-practices/networking.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/content/en/docs/operating/best-practices/networking.md b/content/en/docs/operating/best-practices/networking.md index 138c5d6..e0b144b 100644 --- a/content/en/docs/operating/best-practices/networking.md +++ b/content/en/docs/operating/best-practices/networking.md @@ -610,7 +610,7 @@ rules. apiVersion: capsule.clastix.io/v1beta2 kind: GlobalTenantResource metadata: - name: tenant-network-baseline + name: tenant-clusternetworkpolicy-baseline spec: scope: Tenant tenantSelector: {} @@ -778,7 +778,7 @@ This GlobalTenantResource creates the same baseline as the apiVersion: capsule.clastix.io/v1beta2 kind: GlobalTenantResource metadata: - name: tenant-network-baseline + name: tenant-cilium-clusterwide-baseline spec: scope: Tenant tenantSelector: {} @@ -892,7 +892,7 @@ platform namespaces and the isolation work like in the apiVersion: capsule.clastix.io/v1beta2 kind: GlobalTenantResource metadata: - name: tenant-network-baseline + name: tenant-calico-platform-baseline spec: scope: Tenant tenantSelector: {} @@ -983,8 +983,11 @@ A RoleBinding to a role that allows `get` on the Tier does not work: Calico denies the request with `user cannot get tier`. This GlobalTenantResource creates, per Tenant, the Tier, both ClusterRoles, and -a ClusterRoleBinding for the Tenant owners. The template reads the owners -from the Tenant. +a ClusterRoleBinding for the Tenant owners. The template iterates over +`status.owners`, the resolved owner list, which also contains owners selected +through `spec.permissions.matchOwners`. A ServiceAccount owner is stored with +its canonical user name (`system:serviceaccount::`), so the +template binds it as a `User` subject. ```yaml apiVersion: capsule.clastix.io/v1beta2 @@ -1038,8 +1041,8 @@ spec: kind: ClusterRole name: tenant-{{ $.tenant.metadata.name }}-calico-tier subjects: - {{- range $.tenant.spec.owners }} - - kind: {{ .kind }} + {{- range $.tenant.status.owners }} + - kind: {{ if eq .kind "ServiceAccount" }}User{{ else }}{{ .kind }}{{ end }} name: {{ .name | quote }} apiGroup: rbac.authorization.k8s.io {{- end }} From 189c3235cdf5c4de653afd3b4bd7adcc8713e444 Mon Sep 17 00:00:00 2001 From: sandert-k8s Date: Fri, 13 Nov 2026 11:59:00 +0100 Subject: [PATCH 3/3] fix: comments Signed-off-by: sandert-k8s --- .../operating/best-practices/networking.md | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/content/en/docs/operating/best-practices/networking.md b/content/en/docs/operating/best-practices/networking.md index e0b144b..0e6de75 100644 --- a/content/en/docs/operating/best-practices/networking.md +++ b/content/en/docs/operating/best-practices/networking.md @@ -529,6 +529,17 @@ per Tenant with a [GlobalTenantResource](/docs/replications/global/) using `scope: Tenant`. The Tenant label `capsule.clastix.io/tenant` on every Tenant namespace is what the policies select on, and Capsule resets changes to it. +Replicating these resources needs permissions on the generated kinds. The +default replication identity can manage them. With strict RBAC or a dedicated +ServiceAccount (`spec.serviceAccount` or the +[default ServiceAccount](/docs/replications/global/#default-serviceaccount)), +grant it the [required permissions](/docs/replications/global/#required-permissions) +on `clusternetworkpolicies.policy.networking.k8s.io`, +`ciliumclusterwidenetworkpolicies.cilium.io`, or +`globalnetworkpolicies.projectcalico.org` and `tiers.projectcalico.org`, +depending on the approach. Without them the GlobalTenantResource reports +`Ready=False` with a `forbidden` message. + The mechanism depends on what enforces policies in your cluster: | Your cluster | Choose | Why | @@ -586,9 +597,12 @@ kube-network-policies to a CNI that has its own NetworkPolicy enforcement, such as kindnet in a default kind cluster. With Cilium, the agents read `policyCIDRMatchMode` at startup. A `helm upgrade` -does not restart them, so restart the `cilium` DaemonSet after changing it. Only -the value `nodes` is accepted; other values, such as `pods,nodes`, make the -agent exit on startup. +does not restart them, so restart the `cilium` DaemonSet after changing it. +`nodes` is all these policies need. To also match Pod IPs, which the API +requires for full CIDR conformance, set the Helm value as a list, +`policyCIDRMatchMode={pods,nodes}`, which renders `pods nodes`. A +comma-separated string renders `pods,nodes`, which the agent rejects with +`unknown CIDR match mode` and exits on startup. #### Baseline per Tenant @@ -769,7 +783,9 @@ how you write the policies: labels. * The `kube-apiserver` entity selects the API server without a CIDR, so `policyCIDRMatchMode` is not needed. A `toServices` rule for the - `kubernetes` Service in the `default` namespace also works. + `kubernetes` Service in the `default` namespace needs `nodes` in + `policyCIDRMatchMode`: the Service has no selector, so Cilium matches its + endpoints as CIDRs, and API access stays blocked without the setting. This GlobalTenantResource creates the same baseline as the [ClusterNetworkPolicies](#baseline-per-tenant), once per Tenant: @@ -822,6 +838,10 @@ spec: protocol: ANY - toEntities: - kube-apiserver + toPorts: + - ports: + - port: "6443" + protocol: TCP - apiVersion: cilium.io/v2 kind: CiliumClusterwideNetworkPolicy @@ -852,7 +872,12 @@ spec: values: ["{{tenant.name}}"] ``` -Tenant owners have no permissions on `ciliumclusterwidenetworkpolicies`. +Replace `6443` with the API server endpoint port from +`kubectl get endpointslices -n default -l kubernetes.io/service-name=kubernetes`. +The `kube-apiserver` entity matches the API server addresses, not only its +port. Without `toPorts`, the rule also allows every other port on those +hosts, such as the kubelet or etcd on a self-hosted control plane. Tenant +owners have no permissions on `ciliumclusterwidenetworkpolicies`. ### Calico Tiers @@ -989,6 +1014,14 @@ through `spec.permissions.matchOwners`. A ServiceAccount owner is stored with its canonical user name (`system:serviceaccount::`), so the template binds it as a `User` subject. +The replication identity also creates ClusterRoles and ClusterRoleBindings. +Because of RBAC escalation prevention, a dedicated ServiceAccount needs the +`escalate` and `bind` verbs on `clusterroles`, or must hold all permissions +the roles grant. For the `additionalRoleBindings` below, a controller running +with [strict RBAC](/docs/tenants/permissions/#strict) must be allowed to bind +each `tenant--calico-policies` ClusterRole, for example by listing it in +`manager.rbac.bindableClusterRoles`. + ```yaml apiVersion: capsule.clastix.io/v1beta2 kind: GlobalTenantResource