1

我正在尝试获取 Kubernetes 集群中不健康 pod 的列表,然后尝试查看事件以获取消息,将它们放入列表并报告。但是,我无法获得所有线程的回复。响应非常不寻常,每次我为某些 pod 或其他 pod 运行脚本时,我都没有得到任何响应。我怀疑线程有问题,因为在循环上做同样的事情就可以了。请建议。

代码:

#!/usr/bin/env python

import os
import threading
import time 
from texttable import Texttable
from kubernetes import client, config, watch

#Set config file
os.environ['KUBECONFIG'] = '/etc/kubernetes/admin.conf'

def get_unhealthy_pods():
    config.load_kube_config()
    try:
        v1 = client.CoreV1Api()
        field_selector = 'status.phase='+'Pending'
        pod_list_response = v1.list_pod_for_all_namespaces(watch=False,field_selector=field_selector)
    except ApiException as e:
        print("Exception when calling CoreV1Api->list_pod_for_all_namespaces: %s\n" % e)
    pod_dict = {}
    pod_keys = []
    pod_values = []
    for i in pod_list_response.items:
        #print("%s\t%s\t%s" % (i.status.phase, i.metadata.namespace, i.metadata.name))
        # pod_dict[key].append(i.metadata.name)
        # pod_dic[value].append(i.metadata.namespace)
        pod_keys.append(i.metadata.name)
        pod_values.append(i.metadata.namespace)
    pod_dict = dict(zip(pod_keys, pod_values))
    return pod_dict

def get_event_message(pod,namespace):
    config.load_kube_config()
    try:
        v1 = client.CoreV1Api()
        field_selector='involvedObject.name='+pod
        stream = watch.Watch().stream(v1.list_namespaced_event, namespace, field_selector=field_selector, timeout_seconds=10)
    except ApiException as e:
        print("Exception when calling CoreV1Api->list_namespaced_event: %s\n" % e)
    event_list = []
    iter = 3 
    for i in range(iter): 
        for event in stream:
            event_list.append(event['object'].message)
            print(pod, namespace)
        time.sleep(30)
    events = '\n \n'.join(event_list)
    t = Texttable()
    t.add_row(['Pod', 'Namespace', 'Events'])
    t.add_row([pod, str(namespace), events])
    print(t.draw())
    print("")
    # print(pod, namespace, event_list)
    # print("")

def create_event_histogram():
    unhealthy_pod_dict = get_unhealthy_pods()
    #print(unhealthy_pod_dict)
    thread_count = len(unhealthy_pod_dict)
    print("Total threads: " + str(thread_count))
    thread_list = []
    for i in range(thread_count):
        #print(list(unhealthy_pod_dict.keys())[i], list(unhealthy_pod_dict.values())[i])
        thread  = threading.Thread(target=get_event_message, args={list(unhealthy_pod_dict.keys())[i], list(unhealthy_pod_dict.values())[i]})
        thread_list.append(thread)

    for thread in thread_list:
        print("Starting Thread: " + str(thread))
        thread.start()

    for thread in thread_list:
        thread.join()
        print("Completed Thread: " + str(thread))


#Main
if __name__ == '__main__':
    create_event_histogram()
    print("Done")

输出:

Total threads: 6
issol1-22761-7f98bcdf8d-nd58t 007
umsol1-22761-0 007
sag-b2b-5df7787df9-884hx 1167763323
sa-25482-b6f5b46d-x85hz 1518580704
dbcreatejoba630b9958ba954ca0007-m2c6k 190298074
is-25992-68d6fb8f6d-t4vws 697189153
Starting Thread: <Thread(Thread-1, initial)>
entered event message func
Ended.
Starting Thread: <Thread(Thread-2, initial)>
entered event message func
Ended.
Starting Thread: <Thread(Thread-3, initial)>
entered event message func
Ended.
Starting Thread: <Thread(Thread-4, initial)>
entered event message func
Event: ADDED sa-25482-b6f5b46d-x85hz.15f5a4413c940a9c MountVolume.SetUp failed for volume "my-secret" : secret "my-secret" not found
Event: ADDED sa-25482-b6f5b46d-x85hz.15f5a5386edd1df6 (combined from similar events): MountVolume.SetUp failed for volume "is-secret-volume" : secret "my-secret" not found
Ended.
Starting Thread: <Thread(Thread-5, initial)>
entered event message func
Event: ADDED dbcreatejoba630b9958ba954ca0007-m2c6k.15fb80507afe715f MountVolume.SetUp failed for volume "dbmigrationspec" : secret "create-dbsecreta756b5fb4b285483e" not found
Event: ADDED dbcreatejoba630b9958ba954ca0007-m2c6k.15fb806d117d0b03 Unable to attach or mount volumes: unmounted volumes=[dbmigrationspec], unattached volumes=[dbmigrationspec default-token-2s5dn]: timed out waiting for the condition
Ended.
Starting Thread: <Thread(Thread-6, initial)>
entered event message func
Ended.
Completed Thread: <Thread(Thread-1, stopped 140657099073280)>
Completed Thread: <Thread(Thread-2, stopped 140657099073280)>
Completed Thread: <Thread(Thread-3, stopped 140657099073280)>
Completed Thread: <Thread(Thread-4, stopped 140657099073280)>
Completed Thread: <Thread(Thread-5, stopped 140657099073280)>
Completed Thread: <Thread(Thread-6, stopped 140657099073280)>
Done 
4

0 回答 0