Skip to content

raise error if raycluster crd not available #493

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
merged 3 commits into from
Apr 26, 2024
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
25 changes: 25 additions & 0 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import requests

from kubernetes import config
from kubernetes.client.rest import ApiException


class Cluster:
Expand Down Expand Up @@ -216,6 +217,10 @@ def up(self):
Applies the AppWrapper yaml, pushing the resource request onto
the MCAD queue.
"""

# check if RayCluster CustomResourceDefinition exists if not throw RuntimeError
self._throw_for_no_raycluster()

namespace = self.config.namespace

try:
Expand Down Expand Up @@ -246,12 +251,32 @@ def up(self):
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)

def _throw_for_no_raycluster(self):
api_instance = client.CustomObjectsApi(api_config_handler())
try:
api_instance.list_namespaced_custom_object(
group="ray.io",
version="v1",
namespace=self.config.namespace,
plural="rayclusters",
)
except ApiException as e:
if e.status == 404:
raise RuntimeError(
"RayCluster CustomResourceDefinition unavailable contact your administrator."
)
else:
raise RuntimeError(
"Failed to get RayCluster CustomResourceDefinition: " + str(e)
)

def down(self):
"""
Deletes the AppWrapper yaml, scaling-down and deleting all resources
associated with the cluster.
"""
namespace = self.config.namespace
self._throw_for_no_raycluster()
try:
config_check()
api_instance = client.CustomObjectsApi(api_config_handler())
Expand Down
33 changes: 33 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ def arg_check_del_effect(group, version, namespace, plural, name, *args):
def test_cluster_up_down(mocker):
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
mocker.patch(
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
return_value={"spec": {"domain": ""}},
Expand All @@ -530,6 +531,7 @@ def test_cluster_up_down(mocker):


def test_cluster_up_down_no_mcad(mocker):
mocker.patch("codeflare_sdk.cluster.cluster.Cluster._throw_for_no_raycluster")
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
mocker.patch(
Expand Down Expand Up @@ -3000,6 +3002,37 @@ def test_export_env():
)


def test_cluster_throw_for_no_raycluster(mocker: MockerFixture):
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
mocker.patch(
"codeflare_sdk.cluster.cluster.get_current_namespace",
return_value="opendatahub",
)
mocker.patch(
"codeflare_sdk.utils.generate_yaml.get_default_kueue_name",
return_value="default",
)

def throw_if_getting_raycluster(group, version, namespace, plural):
if plural == "rayclusters":
raise client.ApiException(status=404)
return

mocker.patch(
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
side_effect=throw_if_getting_raycluster,
)
cluster = Cluster(
ClusterConfiguration(
"test_cluster",
image="quay.io/project-codeflare/ray:latest-py39-cu118",
write_to_file=False,
)
)
with pytest.raises(RuntimeError):
cluster.up()


"""
Ray Jobs tests
"""
Expand Down
Loading