我想使用go-client API从k8s集群中获取Secret对象

我有看起来像这样的功能

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基本上是从群集或本地〜/.kube/config中检索配置

我使用metav1.LabelSelector就像我生成新的Deployment对象时一样。所以我认为我很酷。但是ListOptions.LabelSelector是一个字符串。
当我运行我的功能时,它会失败。
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]')

我找不到任何地方使用此功能的示例。文档假定您知道什么是LabelSelector。

ListOptions的LabelSelector的格式是什么?

谢谢

最佳答案

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
}

08-16 07:05