python - 使用 kubernetes python cli 列出命名空间中的证书

标签 python ssl kubernetes certificate

如何使用 kubernetes python cli 列出所有证书并在特定命名空间中进行描述?

# list certificates
kubectl get certificates -n my-namespace

# describe a certificate
kubectl describe certificate my-certificate -n my-namespace

最佳答案

Kubernetes 默认没有类型 certificate ,必须先安装cert-manager's CustomResourceDefinition .
考虑到上述情况意味着在 Kuberentes Python 客户端中我们必须使用 custom object API ,尤其是在您的情况下:函数 list_namespaced_custom_object()get_namespaced_custom_object() .
下面的代码有两个功能,一个是返回所有证书(相当于 kubectl get certificates 命令),二是返回一个特定证书的信息(相当于 kubectl describe certificate {certificate-name} 命令)。基于 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
请记住,这两个函数都返回 Python dictionaries .

关于python - 使用 kubernetes python cli 列出命名空间中的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69482529/

相关文章:

Python:尝试在更改变量时重新计算类属性

python - 从 Kubernetes pod 将大文件上传到 Google Storage GCE

amazon-web-services - 错误 : getting availability zones when trying to create EKS cluster

python - 按任意间隔对时间索引的 DataFrame 进行分组

python - Azure Function App - Python 函数适用于应用程序服务计划类型消耗,但不适用于专用应用程序服务计划

python - 我可以将这个小 Python 脚本与全局字典并行吗?

facebook - GraphMethodException - 不支持的获取请求位于

ssl - 为 wso2 esb API 和 EndPoint 启用统计信息时出错

ssl - 管理 Multi-Tenancy 网站的 SSL 证书

Kubernetes 在 pod 中使用 secret