[英]How to list my k8s jobs with a complex LabelSelector by client-go?
I want to list my k8s jobs with a label selector by client-go like this command:我想通过client-go使用 label 选择器列出我的 k8s 作业,如下所示:
$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
I looked through the source code of client-go, then I wrote some code like this:我查看了client-go的源代码,然后我写了一些这样的代码:
func listJobs(cli *kubernetes.Clientset) (*batchv1.JobList, error) {
label := metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "hello-world",
Operator: metav1.LabelSelectorOpIn,
Values: []string{
"London",
"China",
"NewYork",
},
},
},
}
fmt.Println(label.String())
return cli.BatchV1().Jobs("default").List(context.TODO(), metav1.ListOptions{
LabelSelector: label.String(),
})
}
and then I got the error:然后我得到了错误:
&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]')
Where did I go wrong?我在哪里 go 错了? How do I list my jobs with a label selector which is a complex expression?如何使用复杂表达式的 label 选择器列出我的工作?
Using the Kubernetes client-go
library, you can simply write label selectors the same way you would with kubectl
.使用 Kubernetes client-go
库,您可以像使用kubectl
一样简单地编写 label 选择器。
Writing hello-world in (London, China, NewYork)
should work just fine. hello-world in (London, China, NewYork)
应该没问题。
func listJobs(cli *kubernetes.Clientset) (*batchv1.JobList, error) {
return cli.BatchV1().Jobs("default").List(context.TODO(), metav1.ListOptions{
LabelSelector: "hello-world in (London, China, NewYork)",
})
}
If what you want is to dynamically generate hello-world in (London, China, NewYork)
from a programmatic object however, that is another question, which is already answered on StackOverflow here .但是,如果您想要从程序化的 object 动态生成hello-world in (London, China, NewYork)
,那是另一个问题, 这里的 StackOverflow 已经回答了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.