Skip to content

Get cluster #189

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 6 commits into from
Jul 6, 2023
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
64 changes: 64 additions & 0 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,49 @@ def torchx_config(
to_return["requirements"] = requirements
return to_return

def from_k8_cluster_object(rc):
machine_types = (
rc["metadata"]["labels"]["orderedinstance"].split("_")
if "orderedinstance" in rc["metadata"]["labels"]
else []
)
local_interactive = (
"volumeMounts"
in rc["spec"]["workerGroupSpecs"][0]["template"]["spec"]["containers"][0]
)
cluster_config = ClusterConfiguration(
name=rc["metadata"]["name"],
namespace=rc["metadata"]["namespace"],
machine_types=machine_types,
min_worker=rc["spec"]["workerGroupSpecs"][0]["minReplicas"],
max_worker=rc["spec"]["workerGroupSpecs"][0]["maxReplicas"],
min_cpus=rc["spec"]["workerGroupSpecs"][0]["template"]["spec"][
"containers"
][0]["resources"]["requests"]["cpu"],
max_cpus=rc["spec"]["workerGroupSpecs"][0]["template"]["spec"][
"containers"
][0]["resources"]["limits"]["cpu"],
min_memory=int(
rc["spec"]["workerGroupSpecs"][0]["template"]["spec"]["containers"][0][
"resources"
]["requests"]["memory"][:-1]
),
max_memory=int(
rc["spec"]["workerGroupSpecs"][0]["template"]["spec"]["containers"][0][
"resources"
]["limits"]["memory"][:-1]
),
gpu=rc["spec"]["workerGroupSpecs"][0]["template"]["spec"]["containers"][0][
"resources"
]["limits"]["nvidia.com/gpu"],
instascale=True if machine_types else False,
image=rc["spec"]["workerGroupSpecs"][0]["template"]["spec"]["containers"][
0
]["image"],
local_interactive=local_interactive,
)
return Cluster(cluster_config)


def list_all_clusters(namespace: str, print_to_console: bool = True):
"""
Expand Down Expand Up @@ -337,6 +380,27 @@ def get_current_namespace(): # pragma: no cover
return "default"


def get_cluster(cluster_name: str, namespace: str = "default"):
try:
config.load_kube_config()
api_instance = client.CustomObjectsApi()
rcs = api_instance.list_namespaced_custom_object(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@carsonmh carsonmh Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KPostOffice I get urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /apis/ray.io/v1alpha1/namespaces/default/rayclusters/quicktest when calling the get_namespaced_custom_object so I think there is something wrong with this function

group="ray.io",
version="v1alpha1",
namespace=namespace,
plural="rayclusters",
)
except Exception as e:
return _kube_api_error_handling(e)

for rc in rcs["items"]:
if rc["metadata"]["name"] == cluster_name:
return Cluster.from_k8_cluster_object(rc)
raise FileNotFoundError(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are able to use the function suggested above then the error handling can be removed here.

f"Cluster {cluster_name} is not found in {namespace} namespace"
)


# private methods


Expand Down
32 changes: 29 additions & 3 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
list_all_clusters,
list_all_queued,
_copy_to_ray,
get_cluster,
_app_wrapper_status,
_ray_cluster_status,
)
Expand Down Expand Up @@ -615,6 +616,7 @@ def get_ray_obj(group, version, namespace, plural, cls=None):
"appwrapper.mcad.ibm.com": "quicktest",
"controller-tools.k8s.io": "1.0",
"resourceName": "quicktest",
"orderedinstance": "m4.xlarge_g4dn.xlarge",
},
"managedFields": [
{
Expand Down Expand Up @@ -792,10 +794,10 @@ def get_ray_obj(group, version, namespace, plural, cls=None):
"workerGroupSpecs": [
{
"groupName": "small-group-quicktest",
"maxReplicas": 1,
"minReplicas": 1,
"maxReplicas": 2,
"minReplicas": 2,
"rayStartParams": {"block": "true", "num-gpus": "0"},
"replicas": 1,
"replicas": 2,
"template": {
"metadata": {
"annotations": {"key": "value"},
Expand Down Expand Up @@ -1530,6 +1532,30 @@ def get_aw_obj(group, version, namespace, plural):
return api_obj1


def test_get_cluster(mocker):
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch(
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
side_effect=get_ray_obj,
)
cluster = get_cluster("quicktest")
cluster_config = cluster.config
assert cluster_config.name == "quicktest" and cluster_config.namespace == "ns"
assert (
"m4.xlarge" in cluster_config.machine_types
and "g4dn.xlarge" in cluster_config.machine_types
)
assert cluster_config.min_cpus == 1 and cluster_config.max_cpus == 1
assert cluster_config.min_memory == 2 and cluster_config.max_memory == 2
assert cluster_config.gpu == 0
assert cluster_config.instascale
assert (
cluster_config.image
== "ghcr.io/foundation-model-stack/base:ray2.1.0-py38-gpu-pytorch1.12.0cu116-20221213-193103"
)
assert cluster_config.min_worker == 2 and cluster_config.max_worker == 2


def test_list_clusters(mocker, capsys):
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch(
Expand Down