python
python copied to clipboard
`list_namespaced_pod()` should accept `label_selector` values of `dict` type
What happened (please include outputs or screenshots):
I did a lot of searching on-line, and I couldn't find a trivial way to obtain a list of pods belonging to a certain service.
What I understand is that services are associated with their corresponding pods by the selector labels they have in common.
So what I tried to do is obtain the selector from read_namespaced_service()
and then passing that selector on as the label_selector
argument to list_namespaced_pod()
.
That resulted in the following error:
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
The response also contained HTTP response headers
and a HTTP response body
with error messages that made it clear that it choked on special characters in the dict, which apparently got rendered as a JSON object, instead of the expected comma-separated a=b,x=y
selector labels notation.
What you expected to happen:
I would expect this to work, and see the list of pods corresponding to service myapp
.
How to reproduce it (as minimally and precisely as possible):
So first, I look up the service (assuming service/application name myapp
), for which I want to retrieve the corresponding pods:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
myapp_svc = v1.read_namespaced_service(namespace="myappnamespace", name="myapp")
print(myapp_svc.spec.selector)
At this point, myapp_svc.spec.selector
contains a dict
with the label selector keys and their corresponding values.
So subsequently, I want to look up the corresponding pods:
ret = v1.list_namespaced_pod(namespace="myappnamespace",
label_selector=myapp_svc.spec.selector)
for i in ret.items:
print(i.metadata.name)
Anything else we need to know?:
I found a workaround in this answer on StackOverflow, which dates back to 2018, but still works. But this is an awkward and unintuitive workaround. Why does the API expect us to perform such a conversion manually? Or better yet, would it perhaps be possible to add a list_service_pods()
function to the API that simply takes a reference to the service retrieved from read_namespaced_service()
and returns the corresponding pods?
Also, maybe there is a far more trivial and more proper way to solve this problem, but honestly, I haven't been able to find such a solution on-line. If so, could you maybe clarify this in the documentation, or perhaps add an example to the examples
folder in this project?
This issue is similar, but not entirely identical to issue #1559. That issue was about not being able to pass on a selector obtained from V1LabelSelector
to list_namespaced_pod()
, which resulted in the same problem. That issue was closed without a proper resolution.
Thank you kindly for looking into this.
Environment:
- Python client version (
pip list | grep kubernetes
):25.3.0
Hey @volkert-fastned the label selector param should be something like this: app=value
so you have to do something like this to make it work:
ret = v1.list_namespaced_pod(namespace="default",
label_selector="app=" + myapp_svc.spec.selector["app"])
/assign @Ananya2001-an Thank you!
The Kubernetes project currently lacks enough contributors to adequately respond to all issues.
This bot triages un-triaged issues according to the following rules:
- After 90d of inactivity,
lifecycle/stale
is applied - After 30d of inactivity since
lifecycle/stale
was applied,lifecycle/rotten
is applied - After 30d of inactivity since
lifecycle/rotten
was applied, the issue is closed
You can:
- Mark this issue as fresh with
/remove-lifecycle stale
- Close this issue with
/close
- Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.
This bot triages un-triaged issues according to the following rules:
- After 90d of inactivity,
lifecycle/stale
is applied - After 30d of inactivity since
lifecycle/stale
was applied,lifecycle/rotten
is applied - After 30d of inactivity since
lifecycle/rotten
was applied, the issue is closed
You can:
- Mark this issue as fresh with
/remove-lifecycle rotten
- Close this issue with
/close
- Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle rotten
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.
This bot triages issues according to the following rules:
- After 90d of inactivity,
lifecycle/stale
is applied - After 30d of inactivity since
lifecycle/stale
was applied,lifecycle/rotten
is applied - After 30d of inactivity since
lifecycle/rotten
was applied, the issue is closed
You can:
- Reopen this issue with
/reopen
- Mark this issue as fresh with
/remove-lifecycle rotten
- Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/close not-planned
@k8s-triage-robot: Closing this issue, marking it as "Not Planned".
In response to this:
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.
This bot triages issues according to the following rules:
- After 90d of inactivity,
lifecycle/stale
is applied- After 30d of inactivity since
lifecycle/stale
was applied,lifecycle/rotten
is applied- After 30d of inactivity since
lifecycle/rotten
was applied, the issue is closedYou can:
- Reopen this issue with
/reopen
- Mark this issue as fresh with
/remove-lifecycle rotten
- Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/close not-planned
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.