Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list certificates in the namespace using kubernetes python cli

How to list all certificates & make a describe in the particular namespaces using kubernetes python cli?

# list certificates
kubectl get certificates -n my-namespace

# describe a certificate
kubectl describe certificate my-certificate -n my-namespace
like image 683
marcin_ Avatar asked Dec 06 '25 02:12

marcin_


1 Answers

Kubernetes by default doesn't have a kind certificate, you must first install cert-manager's CustomResourceDefinition.

Considering the above means that in Kuberentes Python client we must use custom object API, especially in your case: functions list_namespaced_custom_object() and get_namespaced_custom_object().

Below code has two functions, one is returning all certificates (equivalent to the kubectl get certificates command), the second one is returning information about one specific certificate (equivalent to the kubectl describe certificate {certificate-name} command). Based on this example code:

from kubernetes import client, config

config.load_kube_config()
api = client.CustomObjectsApi()

# kubectl get certificates -n my-namespace
def list_certificates():
    resources = api.list_namespaced_custom_object(
        group = "cert-manager.io",
        version = "v1",
        namespace = "my-namespace",
        plural = "certificates"
    )
    return resources

# kubectl describe certificate my-certificate -n my-namespace
def get_certificate():
    resource = api.get_namespaced_custom_object(
        group = "cert-manager.io",
        version = "v1",
        name = "my-certificate",
        namespace = "my-namespace",
        plural = "certificates"
    )
    return resource

Keep in mind that both functions are returning Python dictionaries.

like image 99
Mikolaj S. Avatar answered Dec 08 '25 14:12

Mikolaj S.