Kseniia Churiumova
Kseniia Churiumova

Reputation: 161

How to list all kubernetes objects with specific label using client-go

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.

Upvotes: 9

Views: 14146

Answers (3)

I think you're looking for something like this:

objectsToList := []schema.GroupVersionKind{
  schema.GroupVersionKind{
    Group:   "apps",
    Kind:    "DeploymentList",
    Version: "v1",
  },
}

for _, o := range objectsToList {
  // Using an unstructured object.
  u := &unstructured.UnstructuredList{}
  u.SetGroupVersionKind(schema.GroupVersionKind{
    Group:   o.Group,
    Kind:    o.Kind,
    Version: o.Version,
  })
  err := c.List(context.Background(), u)
  if err != nil {
    log.Fatal("cannot List Objects")
  }
}

Upvotes: 0

Oladapo Ajala
Oladapo Ajala

Reputation: 91

You can list all objects with:

kubectl get all

To list objects matching a specific label use:

kubectl get all --selector key=value

where "key" is the name of the label and "value" is the value you're trying to match.

To list all the objects in a namespace use:

kubectl get all --namespace "NAMESPACE"

Upvotes: 0

ahmet alp balkan
ahmet alp balkan

Reputation: 45214

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.

If you aren't required to use client-go, there's a kubectl plugin called get-all which exists to do this task.

Upvotes: 10

Related Questions