<p>I'm trying to setup a network policy in kubernetes where a generic group of pods having the same label value for a key are allowed to have traffic on one port.</p>

<p>Say there are:</p>

<pre><code>Pod A:
 labels:
  meshId="d5ea1b48"
</code></pre>

<p>and</p>

<pre><code>Pod B:
 labels:
  meshId="d5ea1b48"
</code></pre>

<p>and more pods having the same label <code>meshId=d5ea1b48</code>.
The mesh represents a mesh network in which simply all included pods are allowed to have traffic on one port.</p>

<p>My goal interpreted in yaml:</p>

<pre><code>kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: example
spec:
  podSelector:
    matchLabels:
      meshId: %meshId of from-pod%
  ingress:
  - ports:
    - port 1234
    - protocol: TCP
  - from:
      - podSelector:
          matchLabels:
            meshId: %meshId of to-pod%
</code></pre>

<p>So if I am not wrong this config says: I want to deny incoming traffic on port 1234 for those who does not have the same <code>meshId</code>.</p>

<p>Is there a way of creating a kubernetes NetworkPolicy once for all possible <code>meshId</code>s? How would such solution look in yaml?</p>

<p>I don't want to create a new NetworkPolicy for every new <code>meshId</code> because there will be a ton of them and I also think that would harm the performance of the cluster network if there are like 50.000+ NetworkPolicies.</p>

Robin B
<p>There is no way to generalize label selectors in a manner you have put forth. Note that Network Policies are meant to harden the base so that only bonafide traffic gets in and out. </p>

<p>I would also like to point you to the fact that why would you even end up with 50000's additional policies. This means that you have bad selector strategy for pods. Think of changing this rather than the other way around. You are almost there to get this going!</p>

<pre><code>kind: NetworkPolicy
apiVersion: networking.k8s.io/v1    
metadata:
 name: example
spec:
 podSelector:
  matchLabels:
   meshId: value1
 ingress:
 - ports:
  - port 1234
  - protocol: TCP
 - from:
  - podSelector:
      matchLabels:
        meshId: value1
</code></pre>
