From cbfa44606285abb3249f1732f8f422e7d9305e76 Mon Sep 17 00:00:00 2001 From: zhuwenxing Date: Mon, 29 Nov 2021 21:13:55 +0800 Subject: [PATCH] [skip ci]Add func to wait pods ready (#12408) Signed-off-by: zhuwenxing --- .../python_client/common/cus_resource_opts.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/python_client/common/cus_resource_opts.py b/tests/python_client/common/cus_resource_opts.py index 08c8c73650..6e79323413 100644 --- a/tests/python_client/common/cus_resource_opts.py +++ b/tests/python_client/common/cus_resource_opts.py @@ -2,6 +2,7 @@ from __future__ import print_function from kubernetes import client, config from kubernetes.client.rest import ApiException from utils.util_log import test_log as log +import time _GROUP = 'milvus.io' _VERSION = 'v1alpha1' @@ -96,3 +97,36 @@ class CustomResourceOperations(object): for item in cus_objects["items"]: metadata_name = item["metadata"]["name"] self.delete(metadata_name) + + def wait_pods_ready(self, namespace, label_selector): + """wait pods with label selector all ready""" + config.load_kube_config() + api_instance = client.CoreV1Api() + try: + all_pos_ready_flag = False + timeout = 0 + while (not all_pos_ready_flag and timeout < 360): + api_response = api_instance.list_namespaced_pod(namespace=namespace,label_selector=label_selector) + all_pos_ready_flag = True + for item in api_response.items: + print(item.status.phase) + for c in item.status.container_statuses: + print(c.name) + print(c.ready) + log.info(f"{c.name} statu is {c.ready}") + if c.ready is False: + all_pos_ready_flag = False + break + if not all_pos_ready_flag: + log.info("all pods are not ready, please wait") + time.sleep(30) + timeout += 30 + if all_pos_ready_flag: + log.info(f"all pods in namespace {namespace} with label {label_selector} are ready") + else: + log.info("timeout for waiting all pods in namespace {namespace} with label {label_selector} ready") + log.debug(f"list pods response: {api_response}") + except ApiException as e: + log.error("Exception when calling CoreV1Api->list_namespaced_pod: %s\n" % e) + raise Exception(str(e)) + return all_pos_ready_flag