k8s中的秘密列表的LabelSelector

时间:2018-06-29 17:39:19

标签: go kubernetes

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

我有类似的功能

def myajaxtestview(request):
    look_for = request.POST['object_type']
    #lets say you have model Items
    my_items= serializers.serialize('json', Items.objects.filter(object_type=look_for))
    return JsonResponse(my_items)

GetClientOutOfCluster基本上是从集群或本地〜/ .kube / config中检索配置

我使用metav1.LabelSelector就像我生成新的Deployment对象时一样。所以我觉得我很酷。但是ListOptions.LabelSelector是一个字符串。 当我运行函数时,它将失败。

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
}

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

ListOptions的LabelSelector的格式是什么?

谢谢

2 个答案:

答案 0 :(得分:3)

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
}

答案 1 :(得分:0)

您可以使用提供的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
}