skip to Main Content

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


  1. Based on CoreV1API and this sample, it should be:

    .listNamespacedPod("default", undefined, "false", undefined, undefined, "smth=test")
    

    It will list all pods with labels smth=test in the default namespace.

    Login or Signup to reply.
  2. 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'
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search