<p>I would like to validate, that deployments, which have Pod- and NodeAffinities (+AntiAffinity) are configured according to internal guidelines.</p>
<p>Is there a possibility to get deployments (or Pods) using kubectl and limit the result to Objects, that have such an affinity configured?</p>
<p>I have played around with the jsonpath output, but was unsuccessful so far.</p>

<p>hope you are enjoying your Kubernetes journey !</p>
<p>If you need to use affinities (especially with <code>preferredDuringSchedulingIgnoredDuringExecution</code> (explications below)) and just want to just &quot;find&quot; deployments that actually have affinities, you can use this:</p>
<pre><code>❯ k get deploy  -o custom-columns=NAME:&quot;.metadata.name&quot;,AFFINITIES:&quot;.spec.template.spec.affinity&quot;
NAME                               AFFINITIES
nginx-deployment                   &lt;none&gt;
nginx-deployment-vanilla           &lt;none&gt;
nginx-deployment-with-affinities   map[nodeAffinity:map[preferredDuringSchedulingIgnoredDuringExecution:[map[preference:map[matchExpressions:[map[key:test-affinities1 operator:In values:[test1]]]] weight:1]] requiredDuringSchedulingIgnoredDuringExecution:map[nodeSelectorTerms:[map[matchExpressions:[map[key:test-affinities operator:In values:[test]]]]]]]]
</code></pre>
<p>Every <code>&lt;none&gt;</code> pattern indicates that there is no affinity in the deployment.</p>
<p>However, with affinities, if you want to get only the deployments that have affinities without the deployments that don't have affinities, use this:</p>
<pre><code>❯ k get deploy  -o custom-columns=NAME:&quot;.metadata.name&quot;,AFFINITIES:&quot;.spec.template.spec.affinity&quot; | grep -v &quot;&lt;none&gt;&quot;
NAME                               AFFINITIES
nginx-deployment-with-affinities   map[nodeAffinity:map[preferredDuringSchedulingIgnoredDuringExecution:[map[preference:map[matchExpressions:[map[key:test-affinities1 operator:In values:[test1]]]] weight:1]] requiredDuringSchedulingIgnoredDuringExecution:map[nodeSelectorTerms:[map[matchExpressions:[map[key:test-affinities operator:In values:[test]]]]]]]]
</code></pre>
<p>And if you just want the names of the deployments that have affinities, consider using this little script:</p>
<pre><code>❯ k get deploy  -o custom-columns=NAME:&quot;.metadata.name&quot;,AFFINITIES:&quot;.spec.template.spec.affinity&quot; --no-headers | grep -v &quot;&lt;none&gt;&quot; | awk '{print $1}'
nginx-deployment-with-affinities
</code></pre>
<p>But, do not forget that <code>nodeSelector</code> is the simplest way to constrain Pods to nodes with specific labels. (more info here: <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity</a>). Also remember that (according to the same link) the <code>requiredDuringSchedulingIgnoredDuringExecution</code> type of node Affinity functions like nodeSelector, but with a more expressive syntax !
So If you don't need <code>preferredDuringSchedulingIgnoredDuringExecution</code> when dealing with affinities consider using nodeSelector !</p>
<p>After reading the above link, if you want to deal with nodeSelector you can use the same mechanic I used before:</p>
<pre><code>❯ k get deploy  -o custom-columns=NAME:&quot;.metadata.name&quot;,NODE_SELECTOR:&quot;.spec.template.spec.nodeSelector&quot;
NAME                       NODE_SELECTOR
nginx-deployment           map[test-affinities:test]
nginx-deployment-vanilla   &lt;none&gt;
</code></pre>
