doushun9875 2018-06-29 17:39
浏览 1271

k8s中的秘密列表的LabelSelector

I want to get Secret object from k8s cluster using go-client API

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

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.

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.

What is format of LabelSelector for ListOptions?

Thanks

  • 写回答

2条回答 默认 最新

  • donglaogu3788 2019-05-29 09:09
    关注

    You can use the k8s provided function to do the toString operation

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题