1.获取pvc

from kubernetes import client, config
import urllib3
import csv
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


#获取pod,pvc等信息
def get_pod_pvc(v1):
    list_pvc = []
    ret=v1.list_pod_for_all_namespaces()
    for i in ret.items:
        # print(dir(i))
        if hasattr(i.spec,"volumes") == True:
            for n in range(len(i.spec.volumes) ):
                if hasattr(i.spec.volumes[n],"persistent_volume_claim") == True :
                    if hasattr(i.spec.volumes[n].persistent_volume_claim,"claim_name"):
                        list_pvc.append([i.metadata.namespace,i.metadata.name,i.spec.volumes[n].persistent_volume_claim.claim_name])
    print("输出pvc和pod关系")
    print(len(list_pvc),list_pvc)
    return  list_pvc
#获取pvc信息
def get_pvc(v1):
    list_pvc_pvc=[]
    ret=v1.list_persistent_volume_claim_for_all_namespaces()
    for i in ret.items:
        # print(i)
        # print(i.metadata.annotations,"\n---------------\n",i.spec.storage_class_name)
        # print(type(i.metadata.annotations))
        if i.spec.storage_class_name != None:
            list_pvc_pvc.append([i.metadata.namespace,i.metadata.name,i.spec.storage_class_name])
        if "volume.beta.kubernetes.io/storage-class" in i.metadata.annotations:
            # print("通过annotations打印storageclass")
            # print(i.metadata.annotations["volume.beta.kubernetes.io/storage-class"])
            list_pvc_pvc.append([i.metadata.namespace, i.metadata.name,i.metadata.annotations["volume.beta.kubernetes.io/storage-class"]])
            # print(i.metadata.namespace, i.metadata.name, i.spec.storage_class_name,i.metadata.annotations["volume.beta.kubernetes.io/storage-class"])

    print(len(list_pvc_pvc),list_pvc_pvc)
    return  list_pvc_pvc
def main():
    config.kube_config.load_kube_config(config_file=r"D://lens/huawei-q")
    # 获取API的CoreV1Api版本对象
    v1 = client.CoreV1Api()
    # 列出 namespaces
    # for ns in v1.list_namespace().items:
    #     print(ns.metadata.name)
    with open("D://result.csv", 'w', newline='') as csvfile:
        list_pvc = get_pod_pvc(v1)
        list_pvc_pvc = get_pod_pvc(v1)
        for pvc in list_pvc_pvc:
            for pod in list_pvc:
                if pod[0] == pvc[0] and pod[2] == pvc[1]:
                    if pvc[2] == 'log-nas':
                        print("namespace: ", pod[0], "pod: ", pod[1], 'storageclass: ', pvc[2], "pvc: ", pvc[1])
                        write = csv.writer(csvfile)
                        write.writerow([pod[0], pod[1], pvc[2], pvc[1]])


if __name__ == '__main__':
    main()

 

2.其他

from kubernetes import client, config
import urllib3
import csv
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

#获取pvc信息
def get_pvc(v1):
    list_pvc_pvc=[]
    ret=v1.list_persistent_volume_claim_for_all_namespaces()
    for i in ret.items:
        # print(i)
        # print(i.metadata.annotations,"\n---------------\n",i.spec.storage_class_name)
        # print(type(i.metadata.annotations))
        if i.spec.storage_class_name != None:
            list_pvc_pvc.append([i.metadata.namespace,i.metadata.name,i.spec.storage_class_name])
        if "volume.beta.kubernetes.io/storage-class" in i.metadata.annotations:
            # print("通过annotations打印storageclass")
            # print(i.metadata.annotations["volume.beta.kubernetes.io/storage-class"])
            list_pvc_pvc.append([i.metadata.namespace, i.metadata.name,i.metadata.annotations["volume.beta.kubernetes.io/storage-class"]])
            # print(i.metadata.namespace, i.metadata.name, i.spec.storage_class_name,i.metadata.annotations["volume.beta.kubernetes.io/storage-class"])

    print(len(list_pvc_pvc),list_pvc_pvc)
    return  list_pvc_pvc
#获取所有ns
def get_all_namespace(v1):
    ret=v1.list_namespace()
    for i in ret.items:
        print(i.metadata.name)
#获取所有ns 的所有pod
def all_ns_pod(v1):
    v1.list_pod_for_all_namespaces()

#获取所有ns的cm
def all_ns_cm(v1):
    v1.list_config_map_for_all_namespaces()

#获取ns下的cm
def ns_cm(v1):
    v1.list_namespaced_config_map()

#获取ns下的pod
def ns_pod(v1):
    v1.list_namespaced_pod()
#获取ns下的secret
def ns_secret(v1):
    v1.list_namespaced_secret()

def main():
    config.kube_config.load_kube_config(config_file=r"D://lens/huawei-q")
    # 获取API的CoreV1Api版本对象
    v1 = client.CoreV1Api()
    # print(v1.read_node(name="172.26.63.105"))
    # print(v1.list_node())
    print(v1.list_namespaced_secret(namespace="workflow-q"))
    print(v1.read_namespaced_secret(name="",namespace=""))
    v1.list_namespaced_service()
    v1.list_namespaced_service_account(
        
    )










if __name__ == '__main__':
    main()