go - 如何通过 client-go 使用复杂的 LabelSelector 列出我的 k8s 作业?
问题描述
我想通过client-go使用标签选择器列出我的 k8s 作业,如下所示:
$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
我查看了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(),
})
}
然后我得到了错误:
&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]')
我哪里做错了?如何使用复杂表达式的标签选择器列出我的作业?
解决方案
使用 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)",
})
}
但是,如果您想要hello-world in (London, China, NewYork)
从编程对象动态生成,那是另一个问题,这里的 StackOverflow 已经回答了这个问题。
推荐阅读
- joomla - Joomla 中使用的 PHP 方法是什么(OOP 或程序)?
- android - 别名的 Room/SQLite 静态值
- tensorflow - TensorFlow 的 Estimator 停止训练
- java - @Query 注释和带引号的字符串中的命名参数
- matlab - 2016年如何将simulink matlab功能块内存设置为动态
- elasticsearch - .xls 的 ElasticSearch 摄取管道解析错误
- docusignapi - 使用 SendOnBehalfOf 功能时,我们可以隐藏 Docusign 信封的发送电子邮件地址吗?
- angular - 如何在 router.navigate 上关闭 ngx-bootstrap 模式
- react-native - 我们如何在 React Native 应用程序中设置 .env?
- python - How to access deleted VSI's data in SoftLayer