Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 854
pkaramol Avatar asked Nov 09 '21 18:11

pkaramol


1 Answers

It is not supported:

  • https://github.com/kubernetes/client-go/issues/713#issuecomment-557540936
  • and https://github.com/kubernetes/kubernetes/issues/53459

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")
        }
    }
like image 98
blackgreen Avatar answered Oct 21 '22 15:10

blackgreen