Skip to content

Commit 1309934

Browse files
committed
Added support for ingress over routes on cluster creation
1 parent fb34ba4 commit 1309934

File tree

1 file changed

+69
-22
lines changed

1 file changed

+69
-22
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def up(self):
128128
plural="appwrappers",
129129
body=aw,
130130
)
131+
# Create the ingress
132+
create_ingress(self)
131133
except Exception as e: # pragma: no cover
132134
return _kube_api_error_handling(e)
133135

@@ -147,6 +149,9 @@ def down(self):
147149
plural="appwrappers",
148150
name=self.app_wrapper_name,
149151
)
152+
# Delete the ingress
153+
api_instance = client.NetworkingV1Api(api_config_handler())
154+
api_instance.delete_namespaced_ingress(name=f"ray-dashboard-{self.config.name}", namespace=namespace)
150155
except Exception as e: # pragma: no cover
151156
return _kube_api_error_handling(e)
152157

@@ -257,19 +262,17 @@ def cluster_dashboard_uri(self) -> str:
257262
"""
258263
try:
259264
config_check()
260-
api_instance = client.CustomObjectsApi(api_config_handler())
261-
routes = api_instance.list_namespaced_custom_object(
262-
group="route.openshift.io",
263-
version="v1",
264-
namespace=self.config.namespace,
265-
plural="routes",
266-
)
267-
except Exception as e: # pragma: no cover
265+
api_instance = client.NetworkingV1Api(api_config_handler())
266+
ingresses = api_instance.list_namespaced_ingress(self.config.namespace)
267+
except Exception as e:
268268
return _kube_api_error_handling(e)
269-
270-
for route in routes["items"]:
271-
if route["metadata"]["name"] == f"ray-dashboard-{self.config.name}":
272-
return f"http://{route['spec']['host']}"
269+
270+
for ingress in ingresses.items:
271+
if (
272+
ingress.metadata.name == f"ray-dashboard-{self.config.name}"
273+
and ingress.spec.rules[0].host.startswith(f"ray-dashboard-{self.config.name}")
274+
):
275+
return f"http://{ingress.spec.rules[0].host}"
273276
return "Dashboard route not available yet, have you run cluster.up()?"
274277

275278
def list_jobs(self) -> List:
@@ -359,6 +362,52 @@ def local_client_url(self):
359362
else:
360363
return "None"
361364

365+
def create_ingress(self):
366+
"""
367+
Create a Kubernetes Ingress resource to expose the Ray dashboard service externally.
368+
"""
369+
ingress_domain = _get_ingress_domain()
370+
try:
371+
config_check()
372+
api_instance = client.NetworkingV1Api(api_config_handler())
373+
374+
ingress_manifest = {
375+
"apiVersion": "networking.k8s.io/v1",
376+
"kind": "Ingress",
377+
"metadata": {
378+
"name": f"ray-dashboard-{self.config.name}",
379+
"namespace": self.config.namespace,
380+
},
381+
"spec": {
382+
"rules": [
383+
{
384+
"host": f"ray-dashboard-{self.config.name}.{self.config.namespace}.{ingress_domain}",
385+
"http": {
386+
"paths": [
387+
{
388+
"path": "/",
389+
"pathType": "Prefix",
390+
"backend": {
391+
"service": {
392+
"name": f"{self.config.name}-head-svc",
393+
"port": {
394+
"number": 8265,
395+
},
396+
}
397+
},
398+
}
399+
]
400+
},
401+
}
402+
],
403+
},
404+
}
405+
406+
api_instance.create_namespaced_ingress(
407+
namespace=self.config.namespace, body=ingress_manifest
408+
)
409+
except Exception as e:
410+
return _kube_api_error_handling(e)
362411

363412
def list_all_clusters(namespace: str, print_to_console: bool = True):
364413
"""
@@ -534,17 +583,15 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
534583
status = RayClusterStatus.UNKNOWN
535584

536585
config_check()
537-
api_instance = client.CustomObjectsApi(api_config_handler())
538-
routes = api_instance.list_namespaced_custom_object(
539-
group="route.openshift.io",
540-
version="v1",
541-
namespace=rc["metadata"]["namespace"],
542-
plural="routes",
543-
)
586+
api_instance = client.NetworkingV1Api(api_config_handler())
587+
ingresses = api_instance.list_namespaced_ingress(rc["metadata"]["namespace"])
588+
544589
ray_route = None
545-
for route in routes["items"]:
546-
if route["metadata"]["name"] == f"ray-dashboard-{rc['metadata']['name']}":
547-
ray_route = route["spec"]["host"]
590+
for ingress in ingresses.items:
591+
if ingress.metadata.name == f"ray-dashboard-{rc['metadata']['name']}":
592+
ray_route = ingress.spec.rules[0].host
593+
594+
548595

549596
return RayCluster(
550597
name=rc["metadata"]["name"],

0 commit comments

Comments
 (0)