2828
2929
3030class KubeRayCluster :
31- """
32- RayClusterApi provides APIs to list, get, create, build, update, delete rayclusters.
33-
34- Methods:
35- - list_ray_clusters(k8s_namespace: str = "default", async_req: bool = False) -> Any:
36- - get_ray_cluster(name: str, k8s_namespace: str = "default") -> Any:
37- - create_ray_cluster(body: Any, k8s_namespace: str = "default") -> Any:
38- - delete_ray_cluster(name: str, k8s_namespace: str = "default") -> bool:
39- - patch_ray_cluster(name: str, ray_patch: Any, k8s_namespace: str = "default") -> Any:
40- """
41-
4231 EXECUTOR_CLS = KubeRayExecutor
4332
4433 # initial config to setup the kube client
@@ -54,18 +43,7 @@ def list_ray_clusters(
5443 logger .info (
5544 f"Listing Ray clusters in namespace: { k8s_namespace } , label_selector: { label_selector } , async_req: { async_req } "
5645 )
57- """List Ray clusters in a given namespace.
58-
59- Parameters:
60- - k8s_namespace (str, optional): The namespace in which to list the Ray clusters. Defaults to "default".
61- - async_req (bool, optional): Whether to make the request asynchronously. Defaults to False.
6246
63- Returns:
64- Any: The custom resource for Ray clusters in the specified namespace, or None if not found.
65-
66- Raises:
67- ApiException: If there was an error fetching the custom resource.
68- """
6947 try :
7048 resource : Any = self .api .list_namespaced_custom_object (
7149 group = GROUP ,
@@ -88,18 +66,7 @@ def list_ray_clusters(
8866
8967 def get_ray_cluster (self , name : str , k8s_namespace : str = "default" ) -> Any :
9068 logger .info (f"Getting Ray cluster '{ name } ' in namespace '{ k8s_namespace } '" )
91- """Get a specific Ray cluster in a given namespace.
92-
93- Parameters:
94- - name (str): The name of the Ray cluster custom resource. Defaults to "".
95- - k8s_namespace (str, optional): The namespace in which to retrieve the Ray cluster. Defaults to "default".
9669
97- Returns:
98- Any: The custom resource for the specified Ray cluster, or None if not found.
99-
100- Raises:
101- ApiException: If there was an error fetching the custom resource.
102- """
10370 try :
10471 resource : Any = self .api .get_namespaced_custom_object (
10572 group = GROUP ,
@@ -129,20 +96,7 @@ def get_ray_cluster_status(
12996 logger .info (
13097 f"Getting Ray cluster status for '{ name } ' in namespace '{ k8s_namespace } ', timeout: { timeout } s, delay: { delay_between_attempts } s"
13198 )
132- """Get a specific Ray cluster in a given namespace.
133-
134- Parameters:
135- - name (str): The name of the Ray cluster custom resource. Defaults to "".
136- - k8s_namespace (str, optional): The namespace in which to retrieve the Ray cluster. Defaults to "default".
137- - timeout (int, optional): The duration in seconds after which we stop trying to get status if still not set. Defaults to 60 seconds.
138- - delay_between_attempts (int, optional): The duration in seconds to wait between attempts to get status if not set. Defaults to 5 seconds.
139-
140- Returns:
141- Any: The custom resource status for the specified Ray cluster, or None if not found.
14299
143- Raises:
144- ApiException: If there was an error fetching the custom resource.
145- """
146100 while timeout > 0 :
147101 try :
148102 resource : Any = self .api .get_namespaced_custom_object_status (
@@ -186,18 +140,7 @@ def wait_until_ray_cluster_running(
186140 logger .info (
187141 f"Waiting until Ray cluster '{ name } ' is running in namespace '{ namespace } ', timeout: { timeout } s, delay: { delay_between_attempts } s"
188142 )
189- """Get a specific Ray cluster in a given namespace.
190-
191- Parameters:
192- - name (str): The name of the Ray cluster custom resource. Defaults to "".
193- - k8s_namespace (str, optional): The namespace in which to retrieve the Ray cluster. Defaults to "default".
194- - timeout (int, optional): The duration in seconds after which we stop trying to get status. Defaults to 60 seconds.
195- - delay_between_attempts (int, optional): The duration in seconds to wait between attempts to get status if not set. Defaults to 5 seconds.
196-
197- Returns:
198- Bool: True if the raycluster status is Running, False otherwise.
199143
200- """
201144 while timeout > 0 :
202145 status = self .get_ray_cluster_status (
203146 name , k8s_namespace or executor .namespace , timeout , delay_between_attempts
@@ -230,15 +173,7 @@ def create_ray_cluster(
230173 ) -> Any :
231174 namespace = k8s_namespace or executor .namespace
232175 logger .info (f"Creating Ray cluster '{ name } ' in namespace '{ namespace } '" )
233- """Create a new Ray cluster custom resource.
234176
235- Parameters:
236- - body (Any): The data of the custom resource to create.
237- - k8s_namespace (str, optional): The namespace in which to create the custom resource. Defaults to "default".
238-
239- Returns:
240- Any: The created custom resource, or None if it already exists or there was an error.
241- """
242177 if pre_ray_start_commands :
243178 k8s_pre_ray_start_commands = "\n " .join (pre_ray_start_commands )
244179 executor .lifecycle_kwargs ["postStart" ] = {
@@ -283,19 +218,6 @@ def delete_ray_cluster(
283218 timeout : int = 300 ,
284219 poll_interval : int = 5 ,
285220 ) -> Optional [bool ]:
286- """Delete a Ray cluster custom resource and optionally wait for deletion to complete.
287-
288- Parameters:
289- - name (str): The name of the Ray cluster custom resource to delete.
290- - executor (KubeRayExecutor): The executor containing configuration details.
291- - k8s_namespace (str, optional): The namespace in which the Ray cluster exists.
292- - wait (bool, optional): Whether to wait for the cluster and all its pods to be fully deleted. Defaults to False.
293- - timeout (int, optional): Maximum time to wait for deletion in seconds. Defaults to 300 seconds (5 minutes).
294- - poll_interval (int, optional): Time between checks for deletion status in seconds. Defaults to 5 seconds.
295-
296- Returns:
297- Optional[bool]: True if deletion was successful, None if already deleted or there was an error.
298- """
299221 namespace = k8s_namespace or executor .namespace
300222 logger .info (f"Deleting Ray cluster '{ name } ' in namespace '{ namespace } '" )
301223
@@ -400,16 +322,6 @@ def patch_ray_cluster(
400322 ) -> Any :
401323 namespace = k8s_namespace or executor .namespace
402324 logger .info (f"Patching Ray cluster '{ name } ' in namespace '{ namespace } '" )
403- """Patch an existing Ray cluster custom resource.
404-
405- Parameters:
406- - name (str): The name of the Ray cluster custom resource to be patched.
407- - ray_patch (Any): The patch data for the Ray cluster.
408- - k8s_namespace (str, optional): The namespace in which the Ray cluster exists. Defaults to "default".
409-
410- Returns:
411- bool: True if the patch was successful, False otherwise.
412- """
413325 try :
414326 # we patch the existing raycluster with the new config
415327 self .api .patch_namespaced_custom_object (
@@ -436,27 +348,6 @@ def port_forward(
436348 executor : KubeRayExecutor ,
437349 wait : bool = False ,
438350 ):
439- """Port forward a Ray cluster service using kubectl in a daemon thread.
440-
441- When you want to stop the forwarding:
442- forward_thread.stop_forwarding() # Call this method to stop forwarding
443-
444- If wait=True, this function will block until interrupted (e.g., with Ctrl+C).
445-
446- Parameters:
447- - name (str): The name of the Ray cluster custom resource.
448- - port (int): The local port to use for forwarding.
449- - target_port (int): The target port on the Ray cluster to forward to.
450- - k8s_namespace (str, optional): The namespace in which the Ray cluster exists.
451- - wait (bool, optional): If True, block indefinitely until interrupted. Defaults to False.
452-
453- Returns:
454- - ForwardingThread: A thread object with stop_forwarding method.
455-
456- Raises:
457- - RuntimeError: If the Ray head service cannot be found.
458- - TimeoutError: If port forwarding fails to establish within the timeout period.
459- """
460351 import queue
461352 import subprocess
462353 import threading
@@ -618,14 +509,6 @@ def forward_port_daemon():
618509 return forward_thread
619510
620511 def _wait_for_forwarding_termination (self , forward_thread , stop_event ):
621- """Helper method to wait for port forwarding termination.
622-
623- Sets up signal handlers and blocks until interrupted or the stop_event is set.
624-
625- Parameters:
626- - forward_thread: The thread running the port forwarding.
627- - stop_event: The event used to signal the thread to stop.
628- """
629512 import signal
630513 import time
631514
0 commit comments