Skip to content

Commit a64bf9d

Browse files
committed
raise error if raycluster crd not available
Signed-off-by: Kevin <[email protected]>
1 parent 1497434 commit a64bf9d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Diff for: src/codeflare_sdk/cluster/cluster.py

+24
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import requests
5252

5353
from kubernetes import config
54+
from kubernetes.client.rest import ApiException
5455

5556

5657
class Cluster:
@@ -225,6 +226,10 @@ def up(self):
225226
Applies the AppWrapper yaml, pushing the resource request onto
226227
the MCAD queue.
227228
"""
229+
230+
# check if RayCluster CustomResourceDefinition exists if not throw RuntimeError
231+
self._throw_for_no_raycluster()
232+
228233
namespace = self.config.namespace
229234
if self.config.openshift_oauth:
230235
create_openshift_oauth_objects(
@@ -259,6 +264,25 @@ def up(self):
259264
except Exception as e: # pragma: no cover
260265
return _kube_api_error_handling(e)
261266

267+
def _throw_for_no_raycluster(self):
268+
api_instance = client.CustomObjectsApi(api_config_handler())
269+
try:
270+
api_instance.list_namespaced_custom_object(
271+
group="ray.io",
272+
version="v1",
273+
namespace=self.config.namespace,
274+
plural="rayclusters",
275+
)
276+
except ApiException as e:
277+
if e.status == 404:
278+
raise RuntimeError(
279+
"RayCluster CustomResourceDefinition unavailable contact your administrator."
280+
)
281+
else:
282+
raise RuntimeError(
283+
"Failed to get RayCluster CustomResourceDefinition: " + str(e)
284+
)
285+
262286
def down(self):
263287
"""
264288
Deletes the AppWrapper yaml, scaling-down and deleting all resources

Diff for: tests/unit_test.py

+25
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ def arg_check_del_effect(group, version, namespace, plural, name, *args):
481481
def test_cluster_up_down(mocker):
482482
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
483483
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
484+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
484485
mocker.patch(
485486
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
486487
return_value={"spec": {"domain": ""}},
@@ -503,6 +504,7 @@ def test_cluster_up_down(mocker):
503504

504505

505506
def test_cluster_up_down_no_mcad(mocker):
507+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
506508
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
507509
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
508510
mocker.patch(
@@ -3273,6 +3275,29 @@ def test_gen_app_wrapper_with_oauth(mocker: MockerFixture):
32733275
)
32743276

32753277

3278+
def test_cluster_throw_for_no_raycluster(mocker: MockerFixture):
3279+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
3280+
mocker.patch(
3281+
"codeflare_sdk.cluster.cluster.get_current_namespace",
3282+
return_value="opendatahub",
3283+
)
3284+
mocker.patch(
3285+
"kubernetes.client.ApiextensionsV1Api.read_custom_resource_definition",
3286+
side_effect=client.ApiException(status=404),
3287+
)
3288+
cluster = Cluster(
3289+
ClusterConfiguration(
3290+
"test_cluster",
3291+
openshift_oauth=True,
3292+
image="quay.io/project-codeflare/ray:latest-py39-cu118",
3293+
ingress_domain="apps.cluster.awsroute.org",
3294+
write_to_file=False,
3295+
)
3296+
)
3297+
with pytest.raises(RuntimeError):
3298+
cluster.up()
3299+
3300+
32763301
"""
32773302
Ray Jobs tests
32783303
"""

0 commit comments

Comments
 (0)