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

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

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

问题

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

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

spec:
  [...]
  selector:
    matchLabels:
      app: myapp
      process: web
      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

spec:
  [...]
  selector:
    matchLabels:
      app: myapp
      process: web
      release: myrelease

Does not seem this can be done using the ListOptions

答案1

得分: 4

不支持的内容:

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

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

It is not supported:

You have to filter on the client side:

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

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:

确定