<p>I want to get Secret object from k8s cluster using go-client API</p>

<p>I have function that looks like that</p>

<pre><code>func GetSecret( version string) (retVal interface{}, err error){
    clientset := GetClientOutOfCluster()
    labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"version":version}}

    listOptions := metav1.ListOptions{
        LabelSelector: labelSelector.String(),
        Limit:         100,
    }
    secretList, err := clientset.CoreV1().Secrets("namespace").List( listOptions )
    retVal = secretList.Items[0]
    return retVal, err
}
</code></pre>

<p>GetClientOutOfCluster is basically retrieves configuration from cluster or from local ~/.kube/config</p>

<p>I used metav1.LabelSelector just like i do when i generate new Deployment object.So i thought i was cool. But ListOptions.LabelSelector is a string.
When i run my function it fails.</p>

<pre><code>unable to parse requirement: invalid label key "&amp;LabelSelector{MatchLabels:map[string]string{version:": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName',  or 'my.name',  or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')
</code></pre>

<p>I cannot find example of usage of this function anywhere. Documentation assumes that you know what is LabelSelector.</p>

<p>What is format of LabelSelector for ListOptions?</p>

<p>Thanks</p>

<p>You can use the k8s provided function to do the toString operation</p>

<pre><code>import "k8s.io/apimachinery/pkg/labels"
...

func GetSecret(version string) (retVal interface{}, err error){
  clientset := GetClientOutOfCluster()
  labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"version":version}}

  listOptions := metav1.ListOptions{
    LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
    Limit:         100,
  }
  secretList, err := clientset.CoreV1().Secrets("namespace").List(listOptions)
  retVal = secretList.Items[0]
  return retVal, err
}
</code></pre>
