chakravarthi
chakravarthi

Reputation: 31

Kubernetes podSelector with regular expression

I have target pod definition like below.

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
  labels:
    app: my-app
spec:
  containers:
  - name: my-app-container
    image: nginx

And have two source pod definition like below.

apiVersion: v1
kind: Pod
metadata:
  name: my-other-app-pod1
  labels:
    app: my-other-app1
spec:
  containers:
  - name: my-other-app-container
    image: busybox
    command: [ "sleep", "3600" ]
apiVersion: v1
kind: Pod
metadata:
  name: my-other-app-pod2
  labels:
    app: my-other-app2
spec:
  containers:
  - name: my-other-app-container2
    image: busybox
    command: [ "sleep", "3600" ]

I want to write Network policy b/w source and target pods to allow connectivity. How to write Network Policy to allow traffic from both source pods to target pods to use regular expression like below.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-pod-to-pod
spec:
  podSelector:
    matchLabels:
      app: my-app
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
       matchLabels:
         app: my-other-app*

In above code, i want to use condition as app: my-other-app* to match both source pods.

Please help how to use regular expressions in podSelector sections?

Thanks

I have tried this and its working, but I need regular expression way for my usecase.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-pod-to-pod
spec:
  podSelector:
    matchLabels:
      app: my-app
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchExpressions:
        - {key: app, operator: In, values: [my-other-app1, my-other-app2]}

Upvotes: 2

Views: 1266

Answers (1)

Andromeda
Andromeda

Reputation: 1463

According to the Kubernetes documentation, you cannot specify regular expressions on label selectors, however you could use set-based requirements as you mentioned.

Upvotes: 0

Related Questions