简体   繁体   English

使用 python k8s 客户端获取持久卷声明的详细信息

[英]Get details of persistent volume claim using python k8s client

I'm trying to get details of persistentvolumeclaim such as "Used By" which you can get when you run kubectl describe pvc [your-pvc-name] but I'm trying to get that using python k8s client.我正在尝试获取persistentvolumeclaim的详细信息,例如当您运行kubectl describe pvc [your-pvc-name]时可以获得的“Used By” ,但我正在尝试使用 python k8s 客户端来获取它。 I'm able to get YAML of the pvc through readNamespacedPersistentVolumeClaim() function but it doesn't contain the "Used By" .我能够通过readNamespacedPersistentVolumeClaim() function 获得 pvc 的 YAML 但它不包含"Used By" How to use python k8s client to get details of a persistentvolumeclaim such as "Used By" .如何使用 python k8s 客户端获取 persistentvolumeclaim 的详细信息,例如“Used By”

from kubernetes import client


def get_pod_related_to_pvc(pvc_obj, pv_obj):
 v1 = client.CoreV1Api()
 pod = None
 pod_list = v1.list_namespaced_pod(pvc_obj.metadata.namespace)
 for pod in pod_list.items:
    for volume in pod.spec.volumes:
        if volume.persistent_volume_claim:
            if (volume.persistent_volume_claim.claim_name == pv_obj.spec.claimRef.name):
                return pod

This code seems to work perfectly to list all pods with their respective pvc in a namespace from that we can filter out the pod we want.这段代码似乎可以完美地列出命名空间中的所有 pod 及其各自的 pvc,我们可以从中过滤掉我们想要的 pod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 k8s python 客户端拍摄卷快照吗? - Can I take a volume snapshot with the k8s python client? 使用python客户端使用客户端证书进行K8S API访问 - K8S API access with client certificate using python client 使用本机k8s python客户端与k8s运算符添加的API进行交互 - interact with API added by k8s operator using native k8s python client 如何使用 k8s python 客户端删除 k8s 部署? - How to delete a k8s deployment using k8s python client? 使用与 k8s python 客户端等效的 kubectl rollout restart - Using kubectl rollout restart equivalent with k8s python client 使用 Python 客户端调用任意 k8s api - Calling arbitrary k8s apis using the Python client 使用与 K8s Python 客户端等效的 kubectl run 命令 - Using kubectl run command equivalent with K8s Python client 已创建 k8s 卷快照,但它在 python k8s 客户端中返回 409 错误消息 - k8s volumesnapshot is created but it returns 409 error message in python k8s client k8s/python:如何使用 Kubernetes Python 客户端读取机密? - k8s/python: How do I read a secret using the Kubernetes Python client? 如何在 python k8s 客户端中以 root 身份运行“connect_get_namespaced_pod_exec” - How to run 'connect_get_namespaced_pod_exec' as root in python k8s client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM