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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With