如何通过 client-go 使用复杂的 LabelSelector 列出我的 k8s 作业?

Eri*_*han 3 go kubernetes client-go

我想通过client-go使用标签选择器列出我的 k8s 作业,如下命令:

$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
Run Code Online (Sandbox Code Playgroud)

我查看了client-go的源代码,然后我写了一些这样的代码:

$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
Run Code Online (Sandbox Code Playgroud)

然后我得到了错误:

&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]')
Run Code Online (Sandbox Code Playgroud)

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

Ull*_*kut 5

使用 Kubernetesclient-go库,您可以像使用kubectl.

写作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)",
    })
}
Run Code Online (Sandbox Code Playgroud)

然而,如果您想要hello-world in (London, China, NewYork)从编程对象动态生成,那就是另一个问题,StackOverflow 上已经对此进行了回答。