使用 apimachinery 通过 .spec.selector.matchLabels 键列出部署。

huangapple go评论136阅读模式
英文:

List deployments using apimachinery by the .spec.selector.matchLabels key

问题

我想根据在.spec.selector.matchLabels字段中找到的键值对列出我的部署。

使用普通的labels来做这个很容易,但我找不到一种方法来匹配/获取满足以下条件的部署:在以下部分中存在某个特定的key=value

  1. spec:
  2. [...]
  3. selector:
  4. matchLabels:
  5. app: myapp
  6. process: web
  7. release: myrelease

似乎不能使用ListOptions来实现这一点。

英文:

I want to list my deployments based on a key value pair found in the .spec.selector.matchLabels field.

Doing so using the plain labels is easy, but I could not find a way to match / fetch the deployment that satisfies the condition that a certain key=value is present in the following section

  1. spec:
  2. [...]
  3. selector:
  4. matchLabels:
  5. app: myapp
  6. process: web
  7. release: myrelease

Does not seem this can be done using the ListOptions

答案1

得分: 4

不支持的内容:

你需要在客户端进行过滤:

  1. depl, err := clientset.AppsV1().Deployments("some_namespace").List(context.Background(), metav1.ListOptions{})
  2. if err != nil {
  3. panic(err.Error())
  4. }
  5. for _, item := range depl.Items {
  6. if item.Spec.Selector.MatchLabels["app"] == "myapp" {
  7. fmt.Println("找到了")
  8. }
  9. }
英文:

It is not supported:

You have to filter on the client side:

  1. depl, err := clientset.AppsV1().Deployments("some_namespace").List(context.Background(), metav1.ListOptions{})
  2. if err != nil {
  3. panic(err.Error())
  4. }
  5. for _, item := range depl.Items {
  6. if item.Spec.Selector.MatchLabels["app"] == "myapp" {
  7. fmt.Println("found it")
  8. }
  9. }

huangapple
  • 本文由 发表于 2021年11月10日 02:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/69903346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定