Reputation: 403
I am using the python kubernetes api with list_namespaced_pod to get the pods in my namespace. Now I would like to filter them, using the optional label selector parameter.
The documention describes this parameter as
A selector to restrict the list of returned objects by their labels. Defaults to everything.
It does not bother to give an example. On this website, I found several possibilities on how to use the attribute. I already tried
label_selector='label=my_label'
label_selector='label:my_label'
label_selector='my_label'
non of which is working. How do I use the parameter label_selector correctly?
Upvotes: 24
Views: 52324
Reputation: 365
Jonathan's answer shows how to do the OR
condition.
If we want to do the AND
condition, here is the code:
label_selector='app={0},tier={1}'.format('app_name', 'backend')
Upvotes: 2
Reputation: 3948
Kubernetes CLI uses two types of label selectors.
Equality Based
e.g. kubectl get pods -l key=value
Set Based
e.g. kubectl get pod -l 'key in (value1,value2)'
Using
label_selector='label=my_label'
should work.
If it doesn't, try using
label_selector='label in (my_label1, my_label2)'
If it still does not work the error might be coming from somewhere else.
Upvotes: 26
Reputation: 1383
this works for me:
v1.list_namespaced_pod(namespace='default', label_selector='job-name={}'.format(name))
Upvotes: 16
Reputation: 355
(Python client) This works for me and it returns JSON
result = v1.list_namespaced_pod( "default", label_selector="label_key=label_value",watch=False)
print(len(result.items))
Upvotes: 9