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

8/30/2019

I'm a very new user of k8s python client.

I'm trying to find the way to get jobs with regex in python client.

For example in 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".

I'm trying to find the exact same code in python client.

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.

Please let me know if there's a way!

-- N Lee
kubectl
kubernetes
python
regex

2 Answers

8/30/2019

Unfortunately, read_namespaced_job doesn't allow to filter jobs with name pattern.

There is list_namespaced_job that have field_selector argument. But field_selector supports a limited list of operators:

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

-- Yasen
Source: StackOverflow

9/3/2019

kubectl describe jobs (it describes all jobs in default namespace) instead of returning the number of 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

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. Please refer to Kubectl output verbosity and debugging

Hope this help

-- Hanx
Source: StackOverflow