-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwait_deployment_ready.py
87 lines (59 loc) · 2.36 KB
/
wait_deployment_ready.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import subprocess
import logging
import time
import pathlib
from dotenv import load_dotenv
import os
import argparse
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser()
parser.add_argument(
"-t", "--timeout", help="Maximum to wait for.", required=False, type=float
)
ENV_FILE = pathlib.Path(__file__).parent.parent / "config.env"
load_dotenv(dotenv_path=ENV_FILE)
CLUSTER_NAME = os.getenv("CLUSTER_NAME")
assert CLUSTER_NAME is not None
CONTEXT_NAME = f"kind-{CLUSTER_NAME}"
subprocess.run(["kubectl", "config", "use-context", CONTEXT_NAME], stdout=True)
def all_pods_ready(namespace: str):
output = subprocess.check_output(["kubectl", "get", "pods", "-n", namespace])
logger.info("\n" + output.decode())
for line in output.decode().strip().split("\n")[1:]:
name, ready, status, restarts = line.split()[:4]
# skip this pod which is always down
if name.startswith("proxy-agent") and namespace == "kubeflow":
continue
if status != "Completed" and (ready[0] == "0" or status != "Running"):
logger.info(f"Resources not ready (namespace={namespace}).")
return False
logger.info(f"All resources are ready (namespace={namespace}).")
return True
def get_all_namespaces():
out = subprocess.check_output(["kubectl", "get", "namespaces"]).decode()
all_namespaces = [n.split()[0] for n in out.strip().split("\n")[1:]]
return all_namespaces
def wait_deployment_ready(timeout: float = None):
start_time = time.time()
namespaces = get_all_namespaces()
namespaces = [{"name": name, "ready": False} for name in namespaces]
all_ready = False
while not all_ready:
for namespace in namespaces:
if not namespace["ready"]:
namespace["ready"] = all_pods_ready(namespace=namespace["name"])
all_ready = all([namespace["ready"] for namespace in namespaces])
if all_ready:
logger.info(f"Cluster ready!")
break
else:
if timeout and time.time() - start_time > timeout * 60:
raise TimeoutError
else:
logger.info(f"Waiting for resources...")
time.sleep(10)
if __name__ == "__main__":
args = parser.parse_args()
logger.info(vars(args))
wait_deployment_ready(args.timeout)