【问题标题】:Kubernetes javascript client - could not filter by label selectorKubernetes javascript客户端 - 无法按标签选择器过滤
【发布时间】:2021-09-01 00:05:33
【问题描述】:

我从 Kubernetes 官方 javascript 客户端运行以下函数,例如:

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

或任何其他功能。

有一个参数叫做标签选择器。

现在我想使用以下标签选择器查找 pod(如在 HTTP 请求中):

smth=test

但我无法将smth=test 作为字符串发送。

如何按标签选择器过滤?

这是我来自 YAML 的元数据:

metadata:
  name: label-demo
  labels:
    smth: test 
    app: nginx

我可以通过kubectl运行:

kubectl -n="namespace" get deployments -l=smth=test

所以它只会返回匹配的标签。

【问题讨论】:

    标签: javascript kubernetes kube-apiserver


    【解决方案1】:

    基于CoreV1APIthis sample,应该是:

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

    它将列出 default 命名空间中标签为 smth=test 的所有 pod。

    【讨论】:

      【解决方案2】:

      我完全同意 Arnaud Develay 提供的答案,但我想补充一下我在调查这个问题时发现的内容。

      要使您的代码以具有此标签的Pods 进行响应,还需要将其包含在spec.selector.matchLabels(分别为.spec.template. metadata.labels)中。

      通过使用以下Deployment 定义:

      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
      

      以及来自官方github页面的以下代码sn-p:

      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);
      });
      

      代码生成以下输出(以及Pods 的空列表):

      V1PodList {
        apiVersion: 'v1',
        items: [],
        kind: 'PodList',
        metadata: V1ListMeta {
          _continue: undefined,
          remainingItemCount: undefined,
          resourceVersion: '990955',
          selfLink: '/api/v1/namespaces/default/pods'
        }
      }
      

      在使用.spec 中的search=here 标签查询时,回复为:

      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'
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-20
        • 2021-05-28
        • 1970-01-01
        • 2019-06-16
        • 1970-01-01
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 2018-07-24
        相关资源
        最近更新 更多