Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何列出我的k8s工作与一个复杂的LabelSelector的客户去?_Go_Kubernetes_Client Go - Fatal编程技术网

Go 如何列出我的k8s工作与一个复杂的LabelSelector的客户去?

Go 如何列出我的k8s工作与一个复杂的LabelSelector的客户去?,go,kubernetes,client-go,Go,Kubernetes,Client Go,我想通过以下命令列出带有标签选择器的k8s作业: $ kubectl get jobs -l 'hello-world in (London, China, NewYork)' 我浏览了client go的源代码,然后编写了如下代码: func列表作业(cli*kubernetes.Clientset)(*batchv1.JobList,错误){ 标签:=metav1.LabelSelector{ 匹配表达式:[]metav1.LabelSelector要求{ { 键:“你好,世界”, 操作员

我想通过以下命令列出带有标签选择器的k8s作业:

$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
我浏览了client go的源代码,然后编写了如下代码:

func列表作业(cli*kubernetes.Clientset)(*batchv1.JobList,错误){
标签:=metav1.LabelSelector{
匹配表达式:[]metav1.LabelSelector要求{
{
键:“你好,世界”,
操作员:metav1.LabelSelectorOpIn,
值:[]字符串{
“伦敦”,
“中国”,
“纽约”,
},
},
},
}
fmt.Println(label.String())
返回cli.BatchV1().Jobs(“默认”).List(context.TODO(),metav1.ListOptions{
LabelSelector:label.String(),
})
}
然后我得到了一个错误:

&LabelSelector{MatchLabels:map[string]string{},MatchExpressions:[]LabelSelectorRequirement{LabelSelectorRequirement{Key:hello-world,Operator:In,Values:[London China NewYork],},},}
2021/01/28 17:58:07 unable to parse requirement: invalid label key "&LabelSelector{MatchLabels:map[string]string{}": 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]')

我哪里出错了?如何使用复杂表达式的标签选择器列出我的作业?

使用Kubernetes
client go
库,您可以像使用
kubectl
一样简单地编写标签选择器

在(伦敦、中国、纽约)写《你好世界》应该可以

func列表作业(cli*kubernetes.Clientset)(*batchv1.JobList,错误){
返回cli.BatchV1().Jobs(“默认”).List(context.TODO(),metav1.ListOptions{
LabelSelector:“hello world in(伦敦,中国,纽约)”,
})
}

如果您想从编程对象动态生成
hello world in(伦敦,中国,纽约)
,那么这是另一个问题,StackOverflow已经回答了。

非常感谢!这对我来说很好。我想出了另一种方法,我只需要使用函数
metav1.FormatLabelSelector()
来格式化
LabelSelector
,就像这样:
selector:=metav1.FormatLabelSelector(label)
,然后将
selector
变量解析为
ListOptions
中的
LabelSelector
,这同样有效。我可以在
LabelSelectorRequirement
中动态生成我的参数。是的,这是这个答案所指向的解决方案,至少,您可以随意去那里投票,以便其他人在将来更容易找到答案。