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 the spec.selector.matchLabels (and .spec.template. metadata.labels respectively).
By using the following Deployment definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: label-demo
labels:
smth: test # <-- IMPORTANT
app: nginx
spec:
replicas: 1
selector:
matchLabels:
search: here
template:
metadata:
labels:
search: here
spec:
containers:
- name: nginx
image: nginx
and following code snippet from the official github page:
const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
k8sApi.listNamespacedPod("default", undefined, "false", undefined, undefined, "smth=test").then((res) => {
console.log(res.body);
});
The code generated following output (and empty list of Pods):
V1PodList {
apiVersion: 'v1',
items: [],
kind: 'PodList',
metadata: V1ListMeta {
_continue: undefined,
remainingItemCount: undefined,
resourceVersion: '990955',
selfLink: '/api/v1/namespaces/default/pods'
}
}
While querying with the search=here label in the .spec responded with:
V1PodList {
apiVersion: 'v1',
items: [
V1Pod {
apiVersion: undefined,
kind: undefined,
metadata: [V1ObjectMeta],
spec: [V1PodSpec],
status: [V1PodStatus]
}
],
kind: 'PodList',
metadata: V1ListMeta {
_continue: undefined,
remainingItemCount: undefined,
resourceVersion: '991498',
selfLink: '/api/v1/namespaces/default/pods'
}
}