<p>Imagine I have some pods I need on separate k8s nodes, I could use something like this if I know both pods have a label <code>my/label=somevalue</code></p>
<pre class="lang-yaml prettyprint-override"><code>affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 2
        podAffinityTerm:
          labelSelector:
            matchLabels:
              my/label: somevalue
</code></pre>
<p>I have some pods I need separated, according to multiple values of the same label, which doesn't known up-front (it's a shared key which is calculated by an operator).</p>
<p>Is there a way of specifying a <code>podAffinityTerm</code> which applies to any pods sharing the same value of <code>my.label</code>, regardless of the actual value?</p>
<p>E.g.</p>
<pre><code>Pod a has my/label=x
Pod b has my/label=x
Pod c has my/label=y
Pod d has my/label=y
</code></pre>
<p>I'd need pods a &amp; b separated from each other, and pods c &amp; d separated form each other, but e.g. a and d can coexist on the same node</p>

Jon Bates
<p>As far as I know, there is no built-in way to specify affinity without knowing label values. At the stage of creating a pod you need to provide both key and value. In order for affinity to work properly, you need to know this value at the time of creation and put it in the appropriate yaml file.</p>
<p>Theoretically, you could create a custom script, e.g. in bash, which will take your value for you</p>
<blockquote>
<p>its a sharding key which is calculated by an operator</p>
</blockquote>
<p>and then replace it in yaml files. This way it will be set correctly when creating pod.</p>
<p>Additionally, you can also have a look at <a href="https://kubernetes.io/docs/reference/labels-annotations-taints/" rel="nofollow noreferrer">Well-Known Labels, Annotations and Taints</a>.</p>
<p>Depending on your exact situation, you can try to solve the problem with them. See also <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/" rel="nofollow noreferrer">this page</a>. There you will find everything about assigning pod to nodes.</p>
