Skip to content

Commit 653fd15

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

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import requests
4949

5050
from kubernetes import config
51+
from kubernetes.client.rest import ApiException
5152

5253

5354
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

230235
try:
@@ -255,12 +260,32 @@ def up(self):
255260
except Exception as e: # pragma: no cover
256261
return _kube_api_error_handling(e)
257262

263+
def _throw_for_no_raycluster(self):
264+
api_instance = client.CustomObjectsApi(api_config_handler())
265+
try:
266+
api_instance.list_namespaced_custom_object(
267+
group="ray.io",
268+
version="v1",
269+
namespace=self.config.namespace,
270+
plural="rayclusters",
271+
)
272+
except ApiException as e:
273+
if e.status == 404:
274+
raise RuntimeError(
275+
"RayCluster CustomResourceDefinition unavailable contact your administrator."
276+
)
277+
else:
278+
raise RuntimeError(
279+
"Failed to get RayCluster CustomResourceDefinition: " + str(e)
280+
)
281+
258282
def down(self):
259283
"""
260284
Deletes the AppWrapper yaml, scaling-down and deleting all resources
261285
associated with the cluster.
262286
"""
263287
namespace = self.config.namespace
288+
self._throw_for_no_raycluster()
264289
try:
265290
config_check()
266291
api_instance = client.CustomObjectsApi(api_config_handler())

Diff for: tests/unit_test.py

+24
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ def arg_check_del_effect(group, version, namespace, plural, name, *args):
474474
def test_cluster_up_down(mocker):
475475
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
476476
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
477+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
477478
mocker.patch(
478479
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
479480
return_value={"spec": {"domain": ""}},
@@ -496,6 +497,7 @@ def test_cluster_up_down(mocker):
496497

497498

498499
def test_cluster_up_down_no_mcad(mocker):
500+
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
499501
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
500502
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
501503
mocker.patch(
@@ -3172,6 +3174,28 @@ def test_gen_app_wrapper_with_oauth(mocker: MockerFixture):
31723174
)
31733175

31743176

3177+
def test_cluster_throw_for_no_raycluster(mocker: MockerFixture):
3178+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
3179+
mocker.patch(
3180+
"codeflare_sdk.cluster.cluster.get_current_namespace",
3181+
return_value="opendatahub",
3182+
)
3183+
mocker.patch(
3184+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
3185+
side_effect=client.ApiException(status=404),
3186+
)
3187+
cluster = Cluster(
3188+
ClusterConfiguration(
3189+
"test_cluster",
3190+
image="quay.io/project-codeflare/ray:latest-py39-cu118",
3191+
ingress_domain="apps.cluster.awsroute.org",
3192+
write_to_file=False,
3193+
)
3194+
)
3195+
with pytest.raises(RuntimeError):
3196+
cluster.up()
3197+
3198+
31753199
"""
31763200
Ray Jobs tests
31773201
"""

0 commit comments

Comments
 (0)