如何使用 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/