texdade
texdade

Reputation: 112

Kubernetes python API get instances of CRD

I'm currently using the Python APIs for Kubernetes and I have to:

In the terminal, I would simply list all FADepls with kubectl get fadepl and then edit the right one using kubectl edit fadepl <fadepl_name>. I checked the K8s APIs for Python but I can't find what I need. Is it something I can do with the APIs?

Thank you in advance!

Upvotes: 5

Views: 5060

Answers (3)

Juan-Kabbali
Juan-Kabbali

Reputation: 2474

You can use list_cluster_custom_object method from kubernetes.client.CustomObjectsApi.

Let's say that I need to get all calico globalnetworksets instances which are a CRD from calico project.

So first, we need to retrieve some crd data using kubectl as follows:

# get api group, version and plural

> kubectl api-resources -o wide | grep globalnetworkset
    globalnetworksets  crd.projectcalico.org/v1  false  GlobalNetworkSet
     ---------------    -------------------  --
        `plural`             api group       v

With that in mind, we implement list_cluster_custom_object

from kubernetes.client import CustomObjectsApi

group = "crd.projectcalico.org"
v = "v1"
plural = "globalnetworksets"

global_network_sets = CustomObjectsApi.list_cluster_custom_object(group, v, plural)

Upvotes: 7

kkopczak
kkopczak

Reputation: 862

You're right. Using get_namespaced_custom_object you can retrieve the instance. This method returns a namespace scoped custom object. By default it uses a synchronous HTTP request.

Since the output of that method returns an object, you can simply replace it using replace_cluster_custom_object.

Here you can find implementation examples.

See also whole list of API Reference for Python.

Upvotes: 4

texdade
texdade

Reputation: 112

I found the get_namespaced_custom_object call that should do the trick

Upvotes: 0

Related Questions