Skip to content

Commit 9ecdc62

Browse files
committed
improve/fix waiting for cluster creation/deletion
Signed-off-by: Matthias Büchse <[email protected]>
1 parent 01dbfd5 commit 9ecdc62

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

Tests/kaas/plugin/cs_helper.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ def get_clusterstackreleases(api_instance: CustomObjectsApi, namespace):
3333
)['items']
3434

3535

36-
def get_machines(api_instance: CustomObjectsApi, namespace):
37-
"""mimic `kubectl get machines`"""
38-
return api_instance.list_namespaced_custom_object(
39-
'cluster.x-k8s.io', 'v1beta1', namespace, 'machines',
40-
)['items']
41-
42-
4336
def get_secret_data(api_instance: CoreV1Api, namespace, secret):
4437
"""mimic `kubectl get secrets NAME -o=jsonpath='{.data.value}' | base64 -d > kubeconfig.yaml`"""
4538
res = api_instance.read_namespaced_secret(secret, namespace)

Tests/kaas/plugin/plugin_clusterstacks.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def __init__(self, namespace: str, name: str):
2323
self.name = name
2424
self.secret_name = f'{name}-kubeconfig'
2525

26+
def _get_condition_ready(self, co_api: _csh.CustomObjectsApi):
27+
# be defensive here, for none of the fields need exist in early stages of the object's life
28+
status = _csh.get_cluster_status(co_api, self.namespace, self.name).get('status', {})
29+
return bool([
30+
cond
31+
for cond in status.get('conditions', ())
32+
if cond.get('type', '').lower() == 'ready'
33+
if cond.get('status', '').lower() == 'true'
34+
])
35+
2636
def _get_phase(self, co_api: _csh.CustomObjectsApi):
2737
try:
2838
return _csh.get_cluster_status(co_api, self.namespace, self.name)['status'].get('phase', 'n/a')
@@ -64,34 +74,24 @@ def delete(self, co_api: _csh.CustomObjectsApi):
6474
raise
6575
# wait a bit, because the phase attribute seems to be delayed a bit
6676
# also, wait a bit longer, because if deletion was just requested (as is typical),
67-
# it will take at least 5 s
68-
logger.debug(f'cluster {self.name} deletion requested; waiting 5 s for it to vanish')
69-
time.sleep(5)
77+
# it will take at least 5 s (and way longer if the cluster is already running)
78+
logger.debug(f'cluster {self.name} deletion requested; waiting 8 s for it to vanish')
79+
time.sleep(8)
7080
while True:
7181
phase = self._get_phase(co_api)
7282
if phase is None:
7383
break
7484
if phase.lower() != 'deleting':
7585
raise RuntimeError(f'cluster {self.name} in phase {phase}; expected: Deleting')
76-
logger.debug(f'cluster {self.name} still deleting; waiting 4 s for it to vanish')
77-
time.sleep(4)
86+
logger.debug(f'cluster {self.name} still deleting; waiting 30 s for it to vanish')
87+
time.sleep(30)
7888

7989
def get_kubeconfig(self, core_api: _csh.CoreV1Api):
8090
return _csh.get_secret_data(core_api, self.namespace, self.secret_name)
8191

82-
def wait_for_machines(self, co_api: _csh.CustomObjectsApi):
83-
logger.debug(f'checking if cluster {self.name} is ready')
84-
while True:
85-
# filter machines by cluster name
86-
items = [
87-
(item['metadata']['name'], item['status'].get('phase', 'n/a'))
88-
for item in _csh.get_machines(co_api, self.namespace)
89-
if item['spec']['clusterName'] == self.name
90-
]
91-
in_progress = [item[0] for item in items if item[1].lower() != 'running']
92-
if items and not in_progress:
93-
break
94-
logger.debug(f'waiting 30 s for machines to become ready: {in_progress or "none yet"}')
92+
def wait_for_cluster_ready(self, co_api: _csh.CustomObjectsApi):
93+
while not self._get_condition_ready(co_api):
94+
logger.debug(f'waiting 30 s for cluster {self.name} to become ready')
9595
time.sleep(30)
9696
logger.debug(f'cluster {self.name} appears to be ready')
9797

@@ -190,7 +190,7 @@ def create_cluster(self):
190190
self._write_cluster_yaml(cluster_yaml)
191191
cops = _ClusterOps(self.namespace, self.config['name'])
192192
cops.create(co_api, cluster_dict)
193-
cops.wait_for_machines(co_api)
193+
cops.wait_for_cluster_ready(co_api)
194194
self._write_kubeconfig(cops.get_kubeconfig(core_api))
195195

196196
def delete_cluster(self):

0 commit comments

Comments
 (0)