Skip to content

Commit 5a1fc0b

Browse files
authored
Merge pull request #1 from openshift-psap/cluster_details
implemented getting cluster status
2 parents 6e45753 + d3260f9 commit 5a1fc0b

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22
from ..utils.pretty_print import RayCluster
33
from ..utils import pretty_print
44
import openshift as oc
5-
from typing import List
5+
from typing import List, Optional
6+
67

78
class Cluster:
89
def __init__(self, config: ClusterConfiguration):
9-
pass
10+
self.config = config
1011

12+
# creates a new cluser with the provided or default spec
1113
def up(self):
1214
pass
1315

1416
def down(self, name):
1517
pass
1618

17-
def status(self, name):
18-
pass
19-
19+
def status(self, print_to_console=True):
20+
cluster = _ray_cluster_status(self.config.name)
21+
if cluster:
22+
if print_to_console:
23+
pretty_print.print_clusters([cluster])
24+
return cluster.status
25+
else:
26+
return None
27+
2028

2129
def list_all_clusters(print_to_console=True):
2230
clusters = _get_ray_clusters()
2331
if print_to_console:
2432
pretty_print.print_clusters(clusters)
2533
return clusters
26-
34+
2735

2836
# private methods
2937

@@ -33,18 +41,39 @@ def _get_appwrappers(namespace='default'):
3341
return app_wrappers
3442

3543

44+
def _ray_cluster_status(name, namespace='default') -> Optional[RayCluster]:
45+
46+
with oc.project(namespace), oc.timeout(10*60):
47+
cluster = oc.selector(f'rayclusters/{name}').object()
48+
49+
if cluster:
50+
return _map_to_ray_cluster(cluster)
51+
else:
52+
return None
53+
54+
55+
56+
3657
def _get_ray_clusters(namespace='default') -> List[RayCluster]:
3758
list_of_clusters = []
59+
3860
with oc.project(namespace), oc.timeout(10*60):
3961
ray_clusters = oc.selector('rayclusters').objects()
40-
for cluster in ray_clusters:
41-
cluster_model = cluster.model
42-
list_of_clusters.append(RayCluster(
43-
name=cluster.name(), status=cluster_model.status.state,
44-
min_workers=cluster_model.spec.workerGroupSpecs[0].replicas,
45-
max_workers=cluster_model.spec.workerGroupSpecs[0].replicas,
46-
worker_mem_max=cluster_model.spec.workerGroupSpecs[0].template.spec.containers[0].resources.limits.memory,
47-
worker_mem_min=cluster_model.spec.workerGroupSpecs[0].template.spec.containers[0].resources.requests.memory,
48-
worker_cpu=cluster_model.spec.workerGroupSpecs[0].template.spec.containers[0].resources.limits.cpu,
49-
worker_gpu=0))
62+
63+
for cluster in ray_clusters:
64+
list_of_clusters.append(_map_to_ray_cluster(cluster))
5065
return list_of_clusters
66+
67+
68+
def _map_to_ray_cluster(cluster):
69+
cluster_model = cluster.model
70+
return RayCluster(
71+
name=cluster.name(), status=cluster_model.status.state,
72+
min_workers=cluster_model.spec.workerGroupSpecs[0].replicas,
73+
max_workers=cluster_model.spec.workerGroupSpecs[0].replicas,
74+
worker_mem_max=cluster_model.spec.workerGroupSpecs[
75+
0].template.spec.containers[0].resources.limits.memory,
76+
worker_mem_min=cluster_model.spec.workerGroupSpecs[
77+
0].template.spec.containers[0].resources.requests.memory,
78+
worker_cpu=cluster_model.spec.workerGroupSpecs[0].template.spec.containers[0].resources.limits.cpu,
79+
worker_gpu=0)

src/codeflare_sdk/cluster/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
@dataclass
44
class ClusterConfiguration:
5-
min_cpus: int
6-
max_cpus: int
5+
name: str
6+
min_cpus: int = 1
7+
max_cpus: int = 1
8+
min_worker: int = 0
9+
max_worker: int = 1

src/codeflare_sdk/utils/pretty_print.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ class RayCluster:
2020
worker_cpu: int
2121
worker_gpu: int
2222

23+
def _print_no_cluster_found():
24+
pass
25+
2326
def print_clusters(clusters:List[RayCluster], verbose=True):
2427
console = Console()
25-
2628
title_printed = False
29+
#FIXME handle case where no clusters are found
30+
if len(clusters) == 0:
31+
_print_no_cluster_found()
32+
return #exit early
2733

2834
for cluster in clusters:
2935
status = "Active :white_heavy_check_mark:" if cluster.status.lower() == 'ready' else "InActive :x:"

tests/test_clusters.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from codeflare_sdk.cluster.cluster import _get_ray_clusters
2-
from codeflare_sdk.utils.pretty_print import print_clusters
1+
from codeflare_sdk.cluster.cluster import list_all_clusters
2+
from codeflare_sdk.cluster.cluster import Cluster, ClusterConfiguration
3+
4+
#for now these tests assume that the cluster was already created
35
def test_list_clusters():
4-
clusters = _get_ray_clusters()
5-
print_clusters(clusters)
6+
clusters = list_all_clusters()
7+
8+
def test_cluster_status():
9+
cluster = Cluster(ClusterConfiguration(name='raycluster-autoscaler'))
10+
cluster.status()
11+
12+
613

0 commit comments

Comments
 (0)