Sub topic 10
AND versus OR when combining selectors
This chapter assumes you know how to write a NetworkPolicy with podSelector and namespaceSelector peers, and that your network plugin enforces policies. The subject is one YAML detail: whether two selectors sit in the same list item or in separate ones. That detail decides whether a rule is narrow or wide.
The rule
Each entry in a from or to list is a peer. Peers are combined with OR. Inside one entry, the selectors you put together are combined with AND.
- Separate list items (each starting with
-) mean OR. - One list item with several selectors means AND.
The only difference in the YAML is a dash and indentation.
AND: one list item
This policy comes from the Prometheus example. The namespaceSelector and podSelector share a single list item.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-prometheus-scrape
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheusTraffic is allowed only from pods labeled app=prometheus that are also in the monitoring namespace. A pod with that label in another namespace is denied.
OR: two list items
Now add a dash before podSelector.
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
- podSelector:
matchLabels:
app: prometheusThis allows two kinds of sources:
- Every pod in the
monitoringnamespace, whatever its labels. - Every pod labeled
app=prometheusin the policy's own namespace (demo). ApodSelectoron its own matches only the policy's namespace.
The second case is easy to miss. The result is much broader than intended, and the policy still applies without any error.
Comparison
| Layout | Meaning | Allowed sources |
|---|---|---|
| One item, `namespaceSelector` and `podSelector` | AND | Pods matching the label, in matching namespaces |
| Two items | OR | All pods in matching namespaces, plus matching pods in the policy namespace |
The same rule covers ipBlock. An ipBlock cannot be combined with a pod or namespace selector in one item, so it always sits in its own item and acts as an OR alternative.
Reviewing your own policies
You can review a policy for this mistake quickly:
- Find each
fromortolist. - Count the dashes at the peer level.
- Ask whether you want one narrow source or several separate ones.
Run kubectl get networkpolicy allow-prometheus-scrape -n demo -o yaml to see the policy as the API server stored it. Check the indentation there, not in your editor.
Exercise
A policy has from: with two items. The first is - namespaceSelector matching team=a. The second is - podSelector matching role=api. The policy lives in namespace demo. Which sources are allowed?
Show answer
Every pod in any namespace labeled team=a, plus every pod labeled role=api in demo. The two items are OR alternatives. To allow only role=api pods in team=a namespaces, put both selectors in one item by removing the dash before podSelector.