From 1170e512a9b0d8162cd0950ac59d7f6d972569db Mon Sep 17 00:00:00 2001 From: julialawrence Date: Tue, 24 Oct 2023 08:27:54 +0100 Subject: [PATCH] Wait for service ready instead of pod. --- app/cluster_manager.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/cluster_manager.py b/app/cluster_manager.py index d2e809c..5cb3d37 100644 --- a/app/cluster_manager.py +++ b/app/cluster_manager.py @@ -1,4 +1,4 @@ -import uuid +import requests from kubernetes import client, config, watch from kubernetes.config import ConfigException import time @@ -115,6 +115,25 @@ def wait_for_pod_ready(namespace, pod_name): return False # Default case, though your logic might differ based on how you want to handle timeouts +def wait_for_service_ready(service_url, timeout=300): + """ + Wait for the service to become ready by sending requests to the service URL. + :param service_url: The URL of the service to check. + :param timeout: The maximum time to wait, in seconds. + :return: True if the service becomes ready, False if the timeout is reached. + """ + start_time = time.time() + while time.time() - start_time < timeout: + try: + response = requests.get(service_url, timeout=5) + if response.status_code == 200: + # Service is ready + return True + except requests.RequestException as e: + print(f"Request failed: {e}") + # Wait for a while before retrying + time.sleep(5) + return False # Timeout reached def launch_vscode_for_user(user_id): # Step 1: Create a service account for the user @@ -131,7 +150,8 @@ def launch_vscode_for_user(user_id): pod_name = f"vscode-server-{sanitized_user_id}" namespace = "dataaccessmanager" - if wait_for_pod_ready(namespace, pod_name): + # if wait_for_pod_ready(namespace, pod_name): + if wait_for_service_ready(service_url="http://vscode-service-dbe0354c6b5f4bdc8a356af8d4ec68ed.dataaccessmanager.svc.cluster.local/"): print("VS Code server is ready for use.") else: print("There was a problem starting the VS Code server.")