<p>I want to fetch the list (if any) of <code>kubernetes</code> nodes that <strong>do not</strong> contain a label.</p>
<p>I have managed to do the opposite using <code>client-go</code></p>
<pre><code>    labelSelector := metav1.LabelSelector{
        MatchLabels: map[string]string{
            &quot;somelabel&quot;: &quot;somevalue&quot;,
        },
    }
    labelMap, err := metav1.LabelSelectorAsMap(&amp;labelSelector)
    if err != nil {
        return false, errors.Wrap(err, &quot;error converting node label selector to map&quot;)
    }
    nodeListOptions := metav1.ListOptions{
        LabelSelector: labels.SelectorFromSet(labelMap).String(),
    }
    nodes, err := clientset.CoreV1().Nodes().List(nodeListOptions)
    if err != nil {
        return false, errors.Wrap(err, &quot;error getting node list&quot;)
    }
</code></pre>
<p>What I want to achieve is to convert the following <code>kubectl</code> invocation</p>
<pre><code>kubectl get ns --selector='!label_name'
</code></pre>
<p>into <code>client-go</code> code.</p>
<p>Is this feasible?</p>

<p>I think one has to use the <code>Requirement</code> <a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/labels#Requirement" rel="nofollow noreferrer">type</a> of the <code>apimachinery</code>'s <code>labels</code> package and create <code>Selector</code> <a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/labels#Selector" rel="nofollow noreferrer">constructs</a> based on this.</p>
<pre class="lang-golang prettyprint-override"><code>nodeReq, err := labels.NewRequirement(&quot;label_name&quot;, selection.DoesNotExist, []string{})
    if err != nil {
        return false, errors.Wrap(err, &quot;error creating selector requirement&quot;)
    }
    selector := labels.NewSelector()
    selector = selector.Add(*nodeReq)
    if err != nil {
        return false, errors.Wrap(err, &quot;error converting node label selector to map&quot;)
    }
    nodeListOptions := metav1.ListOptions{
        LabelSelector: selector.String(),
    }
    nodes, err := clientset.CoreV1().Nodes().List(nodeListOptions)


</code></pre>
