douzhulan1815 2019-02-13 16:47
浏览 855

如何使用Client-Go列出具有特定标签的所有kubernetes对象

I want to execute equivalent of

kubectl get all -l app=myapp -n mynamespace

or

kubectl label all -l version=1.2.0,app=myapp track=stable --overwrite

using client-go

I looked at dynamic package, but it seems like it needs GroupVersionResource, which is different for, say, Service objects and Deployment objects. Also when I pass schema.GroupVersionResource{Group: "apps", Version: "v1"} it doesn't find anything, when I pass schema.GroupVersionResource{Version: "v1"} it finds only namespace object and also doesn't looks for labels, though I provided label options:

resource := schema.GroupVersionResource{Version: "v1"}
listOptions := metav1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", AppName), FieldSelector: ""}
res, listErr := dynamicClient.Resource(resource).Namespace("myapps").List(listOptions)

I also looked at runtime package, but didn't find anything useful. I took a look at how kubectl implement this, bit haven't figured it out yet, too many levels of abstractions.

  • 写回答

1条回答 默认 最新

  • douzhang6646 2019-02-21 21:15
    关注

    You can't list "all objects" with one call.

    Unfortunately the way Kubernetes API is architected is via API groups, which have multiple APIs under them.

    So you need to:

    1. Query all API groups (apiGroup)
    2. Visit each API group to see what APIs (kind) it exposes.
    3. Actually query that kind to get all the objects (here you may actually filter the list query with the label).

    Fortunately, kubectl api-versions and kubectl api-resources commands do these.

    So to learn how kubectl finds all "kinds" of API resources, run:

    kubectl api-resources -v=6
    

    and you'll see kubectl making calls like:

    • GET https://IP/api
    • GET https://IP/apis
    • then it visits every api group:
      • GET https://IP/apis/metrics.k8s.io/v1beta1
      • GET https://IP/apis/storage.k8s.io/v1
      • ...

    So if you're trying to clone this behavior with client-go, you should use the same API calls, or better just write a script just shells out to kubectl api-resources -o=json and script around it.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?