Skip to content

Commit 8326576

Browse files
authored
Add namespaceSelector:matchLabels example (#41140)
* Add namespaceSelector:matchLabels example * Change matchLabels for matchExpressions
1 parent 4697ee1 commit 8326576

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

content/en/docs/concepts/services-networking/network-policies.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ does not support the `endPort` field and you specify a NetworkPolicy with that,
300300
the policy will be applied only for the single `port` field.
301301
{{< /note >}}
302302

303+
## Targeting multiple namespaces by label
304+
305+
In this scenario, your `Egress` NetworkPolicy targets more than one namespace using their
306+
label names. For this to work, you need to label the target namespaces. For example:
307+
308+
```shell
309+
kubectl label namespace frontend namespace=frontend
310+
kubectl label namespace backend namespace=backend
311+
```
312+
313+
Add the labels under `namespaceSelector` in your NetworkPolicy document. For example:
314+
315+
```yaml
316+
apiVersion: networking.k8s.io/v1
317+
kind: NetworkPolicy
318+
metadata:
319+
name: egress-namespaces
320+
spec:
321+
podSelector:
322+
matchLabels:
323+
app: myapp
324+
policyTypes:
325+
- Egress
326+
egress:
327+
- to:
328+
- namespaceSelector:
329+
matchExpressions:
330+
- key: namespace
331+
operator: In
332+
values: ["frontend", "backend"]
333+
```
334+
335+
{{< note >}}
336+
It is not possible to directly specify the name of the namespaces in a NetworkPolicy.
337+
You must use a `namespaceSelector` with `matchLabels` or `matchExpressions` to select the
338+
namespaces based on their labels.
339+
{{< /note >}}
340+
303341
## Targeting a Namespace by its name
304342

305343
{{< feature-state for_k8s_version="1.22" state="stable" >}}
@@ -344,4 +382,3 @@ implemented using the NetworkPolicy API.
344382
walkthrough for further examples.
345383
- See more [recipes](https://github.com/ahmetb/kubernetes-network-policy-recipes) for common
346384
scenarios enabled by the NetworkPolicy resource.
347-

0 commit comments

Comments
 (0)