简体   繁体   English

k8s中的秘密列表的LabelSelector

[英]LabelSelector for Secrets List in k8s

I want to get Secret object from k8s cluster using go-client API 我想使用go-client API从k8s集群中获取Secret对象

I have function that looks like that 我有看起来像这样的功能

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
}

GetClientOutOfCluster is basically retrieves configuration from cluster or from local ~/.kube/config GetClientOutOfCluster基本上是从集群或本地〜/ .kube / config中检索配置

I used metav1.LabelSelector just like i do when i generate new Deployment object.So i thought i was cool. 我使用metav1.LabelSelector就像我生成新的Deployment对象时一样。所以我觉得我很酷。 But ListOptions.LabelSelector is a string. 但是ListOptions.LabelSelector是一个字符串。 When i run my function it fails. 当我运行我的功能时,它会失败。

unable to parse requirement: invalid label key "&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]')

I cannot find example of usage of this function anywhere. 我找不到任何地方使用此功能的示例。 Documentation assumes that you know what is LabelSelector. 文档假定您知道什么是LabelSelector。

What is format of LabelSelector for ListOptions? ListOptions的LabelSelector的格式是什么?

Thanks 谢谢

func GetSecret( version string, param2 string) (retVal interface{}, err error){
    clientset := GetClientOutOfCluster()
    labelSelector := fmt.Sprintf("version=%s, param2=%s", version, param2)

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

You can use the k8s provided function to do the toString operation 您可以使用提供的k8s函数执行toString操作

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM