<p>In the kubernetes client-go API (or another library that uses it), is there a utility function to convert a <code>k8s.io/apimachinery/pkg/apis/meta/v1/LabelSelector</code> to a string to fill the field <code>LabelSelector</code> in <code>k8s.io/apimachinery/pkg/apis/meta/v1/ListOptions</code>?</p>

<p>I digged through the code of <code>client-go</code> but I can't find a function like that.</p>

<p>The <code>LabelSelector.Marshall()</code> nor <code>LabelSelector.String()</code> give me that (unsurprisingly, because that's not their purpose, but I tried it anyway).</p>

<h3>Background</h3>

<p>I have spec descriptions like <code>k8s.io/api/extensions/v1beta1/Deployment</code>, and want to use it's set of selector labels (i.e. the <code>Selector</code> field) to query it's pods using </p>

<pre><code>options := metav1.ListOptions{
    LabelSelector: &lt;stringified labels&gt;,
}

podList, err := clientset.CoreV1().Pods(&lt;namespace&gt;).List(options)
</code></pre>

bobbel
<p>You can use <code>LabelSelectorAsMap(LabelSelector)</code> function to convert the labelselector into <code>map[string]string</code> map. </p>

<p>Then, use <code>SelectorFromSet</code> function of package <code>k8s.io/apimachinery/pkg/labels</code> to convert <code>map</code> to selector/strings.</p>

<p>Pseudo code:</p>

<pre><code>import (
    "k8s.io/apimachinery/pkg/labels"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func ListPod(labelSelector metav1.LabelSelector) {

    labelMap := metav1.LabelSelectorAsMap(labelSelector)

    options := metav1.ListOptions{
        LabelSelector: labels.SelectorFromSet(labelMap).String(),
    }

    podList, err := clientset.CoreV1().Pods("&lt;namespace&gt;").List(options)

}
</code></pre>
