<p>I have a Kubernetes controller written using client-go <a href="https://pkg.go.dev/k8s.io/client-go/informers" rel="nofollow noreferrer">informers</a> package. It maintains a watch on all Pods in the cluster, there are about 15k of these objects, and their YAML representation takes around 600 MB to print (I assume their in-memory representation is not that different.)</p>
<p>As a result, this (otherwise really small) controller watching Pods ends up with a huge memory footprint (north of 1 GiB). Even methods that you'd think offer a way of filtering, such as the one named like <a href="https://pkg.go.dev/k8s.io/client-go/informers#NewFilteredSharedInformerFactory" rel="nofollow noreferrer"><code>NewFilteredSharedInformerFactory</code></a> doesn't really give you a way to specify a predicate function that chooses which objects are stored in the in-memory <code>cache</code>.</p>
<p>Instead, that method in client-go offers a <a href="https://pkg.go.dev/k8s.io/client-go@v0.24.2/informers/internalinterfaces#TweakListOptionsFunc" rel="nofollow noreferrer"><code>TweakListOptionsFunc</code></a>. It helps you control <code>ListOptions</code> but my predicate unfortunately cannot be satisfied with a <code>labelSelector</code> or <code>fieldSelector</code>. I need to drop the objects when they arrive to the controller through a predicate function.</p>
<blockquote>
<p>Note: the predicate I have is something like <em>&quot;Pods that have an ownerReference by a DaemonSet&quot;</em> (which is <a href="https://stackoverflow.com/questions/59442065/how-to-use-kubernetes-fieldselector-to-query-ownerreferences">not possible with <code>fieldSelectors</code></a> –also another question of mine) and there's no <code>labelSelector</code> that can work in my scenario.</p>
</blockquote>
<p>How would I go about configuring an informer on Pods that only have DaemonSet owner references to reduce the memory footprint of my controller?</p>

<p>Here's an idea, you can get a list of all the <code>DaemonSets</code> in your cluster. Read the <code>spec.selector.matchLabels</code> field to retrieve the label that the <code>DaemonSet</code> pods are bound to have. Use those labels as part of your <code>TweakListOptionsFunc</code> with a logic like:</p>
<pre><code>Pods with label1 OR label2 OR label3 ...
</code></pre>
<p>I know it's a bit of toil, but seems to be a working approach. I believe there isn't a way to specify fields in <code>client-go</code>.</p>
