Exercise: the AND/OR selector trap
Host: controlplane · Time: 7 min · Domain: Cluster Setup
Task
In namespace prod:
- Policy A must allow traffic to
backend**from any Pod labelledtier=frontendin any namespace**. - Policy B must allow traffic to
backend**only from Pods labelledtier=frontendthat live in the namespace labelledapp=orion**.
Acceptance criteria
- A
tier=frontendPod in a namespace without theapp=orionlabel reachesbackendunder A, and does not reach it under B.
Solution
# A — two list items = OR (namespaceSelector empty means "any namespace")
ingress:
- from:
- podSelector: {matchLabels: {tier: frontend}}
---
# B — one list item with both selectors = AND
ingress:
- from:
- namespaceSelector: {matchLabels: {app: orion}}
podSelector: {matchLabels: {tier: frontend}}
The one-line difference: in B, podSelector is indented at the same level asnamespaceSelector, under the same -. In A it is a second -.
namespaceSelector: {} matches every namespace, including kube-system — which
is why "any namespace" also means "the one I did not think about".