Skip to content

Add is_dashboard_ready function + update unit test #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from kubernetes import client, config
import yaml
import os
import requests


class Cluster:
Expand Down Expand Up @@ -250,27 +251,36 @@ def status(

return status, ready

def is_dashboard_ready(self) -> bool:
response = requests.get(self.cluster_dashboard_uri(), timeout=5)
if response.status_code == 200:
return True
else:
return False

def wait_ready(self, timeout: Optional[int] = None):
"""
Waits for requested cluster to be ready, up to an optional timeout (s).
Checks every five seconds.
"""
print("Waiting for requested resources to be set up...")
ready = False
dashboard_ready = False
status = None
time = 0
while not ready:
while not ready or not dashboard_ready:
status, ready = self.status(print_to_console=False)
dashboard_ready = self.is_dashboard_ready()
if status == CodeFlareClusterStatus.UNKNOWN:
print(
"WARNING: Current cluster status is unknown, have you run cluster.up yet?"
)
if not ready:
if not ready or not dashboard_ready:
if timeout and time >= timeout:
raise TimeoutError(f"wait() timed out after waiting {timeout}s")
sleep(5)
time += 5
print("Requested cluster up and running!")
print("Requested cluster and dashboard are up and running!")

def details(self, print_to_console: bool = True) -> RayCluster:
cluster = _copy_to_ray(self)
Expand Down
17 changes: 16 additions & 1 deletion tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,21 @@ def test_wait_ready(mocker, capsys):
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch("codeflare_sdk.cluster.cluster._app_wrapper_status", return_value=None)
mocker.patch("codeflare_sdk.cluster.cluster._ray_cluster_status", return_value=None)
mocker.patch.object(
client.CustomObjectsApi,
"list_namespaced_custom_object",
return_value={
"items": [
{
"metadata": {"name": "ray-dashboard-test"},
"spec": {"host": "mocked-host"},
}
]
},
)
mock_response = mocker.Mock()
mock_response.status_code = 200
mocker.patch("requests.get", return_value=mock_response)
cf = Cluster(ClusterConfiguration(name="test", namespace="ns"))
try:
cf.wait_ready(timeout=5)
Expand All @@ -1773,7 +1788,7 @@ def test_wait_ready(mocker, capsys):
captured = capsys.readouterr()
assert (
captured.out
== "Waiting for requested resources to be set up...\nRequested cluster up and running!\n"
== "Waiting for requested resources to be set up...\nRequested cluster and dashboard are up and running!\n"
)


Expand Down