Skip to content

Commit 82d2c5b

Browse files
authored
raise error if raycluster crd not available (#493)
Signed-off-by: Kevin <[email protected]>
1 parent 80de58e commit 82d2c5b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import requests
4747

4848
from kubernetes import config
49+
from kubernetes.client.rest import ApiException
4950

5051

5152
class Cluster:
@@ -216,6 +217,10 @@ def up(self):
216217
Applies the AppWrapper yaml, pushing the resource request onto
217218
the MCAD queue.
218219
"""
220+
221+
# check if RayCluster CustomResourceDefinition exists if not throw RuntimeError
222+
self._throw_for_no_raycluster()
223+
219224
namespace = self.config.namespace
220225

221226
try:
@@ -246,12 +251,32 @@ def up(self):
246251
except Exception as e: # pragma: no cover
247252
return _kube_api_error_handling(e)
248253

254+
def _throw_for_no_raycluster(self):
255+
api_instance = client.CustomObjectsApi(api_config_handler())
256+
try:
257+
api_instance.list_namespaced_custom_object(
258+
group="ray.io",
259+
version="v1",
260+
namespace=self.config.namespace,
261+
plural="rayclusters",
262+
)
263+
except ApiException as e:
264+
if e.status == 404:
265+
raise RuntimeError(
266+
"RayCluster CustomResourceDefinition unavailable contact your administrator."
267+
)
268+
else:
269+
raise RuntimeError(
270+
"Failed to get RayCluster CustomResourceDefinition: " + str(e)
271+
)
272+
249273
def down(self):
250274
"""
251275
Deletes the AppWrapper yaml, scaling-down and deleting all resources
252276
associated with the cluster.
253277
"""
254278
namespace = self.config.namespace
279+
self._throw_for_no_raycluster()
255280
try:
256281
config_check()
257282
api_instance = client.CustomObjectsApi(api_config_handler())

Diff for: tests/unit_test.py

+33
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ def arg_check_del_effect(group, version, namespace, plural, name, *args):
508508
def test_cluster_up_down(mocker):
509509
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
510510
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
511+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
511512
mocker.patch(
512513
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
513514
return_value={"spec": {"domain": ""}},
@@ -530,6 +531,7 @@ def test_cluster_up_down(mocker):
530531

531532

532533
def test_cluster_up_down_no_mcad(mocker):
534+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
533535
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
534536
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
535537
mocker.patch(
@@ -3000,6 +3002,37 @@ def test_export_env():
30003002
)
30013003

30023004

3005+
def test_cluster_throw_for_no_raycluster(mocker: MockerFixture):
3006+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
3007+
mocker.patch(
3008+
"codeflare_sdk.cluster.cluster.get_current_namespace",
3009+
return_value="opendatahub",
3010+
)
3011+
mocker.patch(
3012+
"codeflare_sdk.utils.generate_yaml.get_default_kueue_name",
3013+
return_value="default",
3014+
)
3015+
3016+
def throw_if_getting_raycluster(group, version, namespace, plural):
3017+
if plural == "rayclusters":
3018+
raise client.ApiException(status=404)
3019+
return
3020+
3021+
mocker.patch(
3022+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
3023+
side_effect=throw_if_getting_raycluster,
3024+
)
3025+
cluster = Cluster(
3026+
ClusterConfiguration(
3027+
"test_cluster",
3028+
image="quay.io/project-codeflare/ray:latest-py39-cu118",
3029+
write_to_file=False,
3030+
)
3031+
)
3032+
with pytest.raises(RuntimeError):
3033+
cluster.up()
3034+
3035+
30033036
"""
30043037
Ray Jobs tests
30053038
"""

0 commit comments

Comments
 (0)