douren1928 2019-05-21 04:38
浏览 1228
已采纳

kubernetes client-go:将labelselector转换为标签字符串

In the kubernetes client-go API (or another library that uses it), is there a utility function to convert a k8s.io/apimachinery/pkg/apis/meta/v1/LabelSelector to a string to fill the field LabelSelector in k8s.io/apimachinery/pkg/apis/meta/v1/ListOptions?

I digged through the code of client-go but I can't find a function like that.

The LabelSelector.Marshall() nor LabelSelector.String() give me that (unsurprisingly, because that's not their purpose, but I tried it anyway).

Background

I have spec descriptions like k8s.io/api/extensions/v1beta1/Deployment, and want to use it's set of selector labels (i.e. the Selector field) to query it's pods using

options := metav1.ListOptions{
    LabelSelector: <stringified labels>,
}

podList, err := clientset.CoreV1().Pods(<namespace>).List(options)
  • 写回答

1条回答 默认 最新

  • dp198879 2019-05-21 06:18
    关注

    You can use LabelSelectorAsMap(LabelSelector) function to convert the labelselector into map[string]string map.

    Then, use SelectorFromSet function of package k8s.io/apimachinery/pkg/labels to convert map to selector/strings.

    Pseudo code:

    import (
        "k8s.io/apimachinery/pkg/labels"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    )
    
    func ListPod(labelSelector metav1.LabelSelector) {
    
        labelMap := metav1.LabelSelectorAsMap(labelSelector)
    
        options := metav1.ListOptions{
            LabelSelector: labels.SelectorFromSet(labelMap).String(),
        }
    
        podList, err := clientset.CoreV1().Pods("<namespace>").List(options)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器