Answer a question

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?

Answers

Kubernetes CLI uses two types of label selectors.

  1. Equality Based Eg: kubectl get pods -l key=value

  2. Set Based Eg: kubectl get pod -l 'key in (value1,value2)'

label_selector='label=my_label'

should work, else try using

label_selector='label in (my_label1, my_label2)'.

If this does not work the error might come from somewhere else.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐