[skip ci]Fix wait pods ready function (#12690)

Signed-off-by: zhuwenxing <wenxing.zhu@zilliz.com>
pull/12503/head
zhuwenxing 2021-12-03 17:11:45 +08:00 committed by GitHub
parent c77ed0417f
commit ffe7cf0d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 9 deletions

View File

@ -4,7 +4,7 @@ from kubernetes.client.rest import ApiException
from utils.util_log import test_log as log
def wait_pods_ready(namespace, label_selector, timeout=360):
def wait_pods_ready(namespace, label_selector, expected_num=None, timeout=360):
"""
wait pods with label selector all ready
@ -14,12 +14,15 @@ def wait_pods_ready(namespace, label_selector, timeout=360):
:param label_selector: labels to restrict which pods are waiting to be ready
:type label_selector: str
:param expected_num: expected the minimum number of pods to be ready if not None
:type expected_num: int
:param timeout: limits the duration of the call
:type timeout: int
:example:
>>> wait_pods_ready("default", "app.kubernetes.io/instance=scale-query")
"""
>>> wait_pods_ready("default", "app.kubernetes.io/instance=scale-query", expected_num=9)
"""
config.load_kube_config()
api_instance = client.CoreV1Api()
try:
@ -28,12 +31,15 @@ def wait_pods_ready(namespace, label_selector, timeout=360):
while not all_pos_ready_flag and time_cnt < timeout:
api_response = api_instance.list_namespaced_pod(namespace=namespace, label_selector=label_selector)
all_pos_ready_flag = True
for item in api_response.items:
for c in item.status.container_statuses:
log.info(f"{c.name} status is {c.ready}")
if c.ready is False:
all_pos_ready_flag = False
break
if expected_num is not None and len(api_response.items) < expected_num:
all_pos_ready_flag = False
else:
for item in api_response.items:
for c in item.status.container_statuses:
log.info(f"{c.name} status 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)