简体   繁体   English

我可以在kubernetes python客户端中使用正则表达式作为工作名称吗?

[英]Can I use regular expression for job name in kubernetes python client?

I'm a very new user of k8s python client. 我是k8s python客户端的非常新的用户。

I'm trying to find the way to get jobs with regex in python client. 我正在尝试找到在python客户端中使用正则表达式获得工作的方法。

For example in CLI, 例如在CLI中,

kubectl describe jobs -n mynamespace partial-name-of-job

gives me the number of jobs whose name has partial-name-of-job in "mynamespace". 给我提供partial-name-of-job “ mynamespace”中的partial-name-of-job数。

I'm trying to find the exact same code in python client. 我试图在python客户端中找到完全相同的代码。

I did several searches and some are suggested to use label selector, but the python client API function BatchV1Api().read_namespaced_job() requires the exact name of jobs. 我进行了几次搜索,建议使用标签选择器,但是python客户端API函数BatchV1Api().read_namespaced_job()需要作业的确切名称。

Please let me know if there's a way! 请让我知道是否有办法!

Unfortunately, read_namespaced_job doesn't allow to filter jobs with name pattern. 不幸的是, read_namespaced_job不允许使用名称模式过滤作业。

There is list_namespaced_job that have field_selector argument. 有具有field_selector参数的list_namespaced_job But field_selector supports a limited list of operators: 但是field_selector支持有限的运算符列表:

You can use the =, ==, and != operators with field selectors (= and == mean the same thing). 您可以将=,==和!=运算符与字段选择器一起使用(=和==表示同一意思)。

So, if you want to apply regex filter to job list, I'd suggest to get full list and then filter it using Python regex 因此,如果您想将正则表达式过滤器应用于作业列表,建议您获取完整列表,然后使用Python regex对其进行过滤

kubectl describe jobs (it describes all jobs in default namespace) instead of returning the number of jobs. kubectl describe jobs (它描述默认名称空间中的所有作业),而不是返回作业数。

So as mentioned by Yasen please use list_namespaced_job with namespace parameter it gives api request like kubectl get --raw=/apis/batch/v1/namespaces/{namespace}/jobs 因此,正如Yasen所述,请使用带有namespace参数的list_namespaced_job ,它会给api请求,例如kubectl get --raw=/apis/batch/v1/namespaces/{namespace}/jobs

You can also modify your script and get some specific value. 您还可以修改脚本并获得一些特定的值。 Please run kubectl get or describe --v=8 to get the strict api request. 请运行kubectl get or describe --v=8以获取严格的api请求。 Please refer to Kubectl output verbosity and debugging 请参考Kubectl输出的详细程度和调试

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM