Клиент Kubernetes python: проблема с ответом из-за многопоточности - PullRequest
1 голос
/ 13 апреля 2020

Я пытаюсь получить список нездоровых контейнеров в кластере Kubernetes, а затем пытаюсь посмотреть события на наличие сообщений, поместить их в список и составить отчет. Тем не менее, я не могу получить ответ для всех потоков. Ответ довольно необычный, каждый раз, когда я запускаю скрипт для одних или других пакетов, я не получаю никакого ответа. Я подозреваю, что с потоками что-то не так, так как все то же самое с al oop работает нормально. Пожалуйста, предложите.

Код:

#!/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 
...