I run the following function from Kubernetes official javascript client, for example :
.listNamespacedPod("default", null, "false", "smth=test", null, null, null, null, null, null)
or any other function.
There is a param called labels selector.
Now I would like to find the pods (as in the HTTP request) with the following label selector:
smth=test
But I cannot send smth=test
as a string.
How can I filter by label selector?
This is my metadata from the YAML:
metadata:
name: label-demo
labels:
smth: test
app: nginx
I can run via kubectl
:
kubectl -n="namespace" get deployments -l=smth=test
so it will return only the matched labels.
2
Answers
Based on CoreV1API and this sample, it should be:
It will list all pods with labels
smth=test
in thedefault
namespace.I wholeheartedly agree with the answer provided by Arnaud Develay but I wanted to add what I found out while investigating this question.
To make your code respond with the
Pods
that are having this label, it also needs to be included in thespec.selector.matchLabels
(and.spec.template. metadata.labels
respectively).By using the following
Deployment
definition:and following code snippet from the official github page:
The code generated following output (and empty list of
Pods
):While querying with the
search=here
label in the.spec
responded with: