如何在kubernetes中过滤完成的作业 [英] How to filter finished jobs in kubernetes
问题描述
我正在尝试按状态筛选使用golang kubernetes client-go lib完成的作业.
I'm trying to filter jobs that are complete using golang kubernetes client-go lib by their status.
我已经查看了其他答案,说明了如何使用kubectl来获得工作,例如:
I've checked other answers explaining how to get the jobs using kubectl, like this:
kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}'
但是我不知道如何将jsonpath输出转换"为过滤器或列表选项
But I don't know how to "turn" that jsonpath output into a filter or list options
如果我要按状态和标签搜索豆荚,我将执行以下操作:
If I were searching for pods by their status phase and a label I would do something like this:
listOptions := metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=my-custom-job",
FieldSelector: "status.phase=Running",
}
result, err := clientset.CoreV1().Pods("default").List(listOptions)
但是如果我要实现jsonpath {.items[?(@.status.succeeded==1)].metadata.name}
But if I am going to implement the jsonpath {.items[?(@.status.succeeded==1)].metadata.name}
这将迭代所有作业,并检查状态下的成功密钥是否等于1.对于所有工作.
This is going to iterate through all jobs and check if the succeeded key under status is equal to one. For all jobs.
是否有一种方法可以使这些作业更内存友好",或者可以像ListOptions一样使用jsonpaths?
推荐答案
是,您可以在服务器端仅过滤完成的作业.
Yes, you can filter out, on the server-side, only finished jobs.
listOptions := metav1.ListOptions{
FieldSelector: "status.successful=1",
}
result, err := clientset.BatchV1().Jobs("").List(listOptions)
作业规范中的
status.successful 字段直接映射到 metav1.ListOptions.FieldSelector 中的 status.succeeded 字段. 有关此内容的更多信息. /a>
status.successful field from the job's spec is being directly mapped to status.succeeded field from metav1.ListOptions.FieldSelector. More info about that.
话虽这么说,服务器端要过滤的可用选项列表受到严格限制.您不能使用规范中的任意字段进行过滤(例如, status.active 或 spec.parallelism ). Github问题.
That being said, the list of available options to filter on the server-side is highly restricted. You can not filter using arbitrary fields from the spec (e.g. status.active or spec.parallelism). Github Issue on that.
这篇关于如何在kubernetes中过滤完成的作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!