Skip to content

Fixed creating ingresses without admin access #407

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 5 commits into from
Dec 20, 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
126 changes: 93 additions & 33 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
generate_appwrapper,
)
from ..utils.kube_api_helpers import _kube_api_error_handling
from ..utils.generate_yaml import is_openshift_cluster
from ..utils.openshift_oauth import (
create_openshift_oauth_objects,
delete_openshift_oauth_objects,
Expand Down Expand Up @@ -415,25 +416,48 @@ def cluster_dashboard_uri(self) -> str:
"""
Returns a string containing the cluster's dashboard URI.
"""
try:
config_check()
api_instance = client.NetworkingV1Api(api_config_handler())
ingresses = api_instance.list_namespaced_ingress(self.config.namespace)
except Exception as e: # pragma no cover
return _kube_api_error_handling(e)
config_check()
if is_openshift_cluster():
try:
api_instance = client.CustomObjectsApi(api_config_handler())
routes = api_instance.list_namespaced_custom_object(
group="route.openshift.io",
version="v1",
namespace=self.config.namespace,
plural="routes",
)
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)

for route in routes["items"]:
if route["metadata"][
"name"
] == f"ray-dashboard-{self.config.name}" or route["metadata"][
"name"
].startswith(
f"{self.config.name}-ingress"
):
protocol = "https" if route["spec"].get("tls") else "http"
return f"{protocol}://{route['spec']['host']}"
else:
try:
api_instance = client.NetworkingV1Api(api_config_handler())
ingresses = api_instance.list_namespaced_ingress(self.config.namespace)
except Exception as e: # pragma no cover
return _kube_api_error_handling(e)

for ingress in ingresses.items:
annotations = ingress.metadata.annotations
protocol = "http"
if (
ingress.metadata.name == f"ray-dashboard-{self.config.name}"
or ingress.metadata.name.startswith(f"{self.config.name}-ingress")
):
if annotations == None:
protocol = "http"
elif "route.openshift.io/termination" in annotations:
protocol = "https"
return f"{protocol}://{ingress.spec.rules[0].host}"
for ingress in ingresses.items:
annotations = ingress.metadata.annotations
protocol = "http"
if (
ingress.metadata.name == f"ray-dashboard-{self.config.name}"
or ingress.metadata.name.startswith(f"{self.config.name}-ingress")
):
if annotations == None:
protocol = "http"
elif "route.openshift.io/termination" in annotations:
protocol = "https"
return f"{protocol}://{ingress.spec.rules[0].host}"
return "Dashboard ingress not available yet, have you run cluster.up()?"

def list_jobs(self) -> List:
Expand Down Expand Up @@ -532,6 +556,14 @@ def _component_resources_up(
plural="rayclusters",
body=resource,
)
elif resource["kind"] == "Ingress":
api_instance.create_namespaced_custom_object(
group="networking.k8s.io",
version="v1",
namespace=namespace,
plural="ingresses",
body=resource,
)
elif resource["kind"] == "Route":
api_instance.create_namespaced_custom_object(
group="route.openshift.io",
Expand Down Expand Up @@ -561,6 +593,15 @@ def _component_resources_down(
plural="rayclusters",
name=self.app_wrapper_name,
)
elif resource["kind"] == "Ingress":
name = resource["metadata"]["name"]
api_instance.delete_namespaced_custom_object(
group="networking.k8s.io",
version="v1",
namespace=namespace,
plural="ingresses",
name=name,
)
elif resource["kind"] == "Route":
name = resource["metadata"]["name"]
api_instance.delete_namespaced_custom_object(
Expand Down Expand Up @@ -663,29 +704,48 @@ def _check_aw_exists(name: str, namespace: str) -> bool:
)
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e, print_error=False)

for aw in aws["items"]:
if aw["metadata"]["name"] == name:
return True
return False


# Cant test this until get_current_namespace is fixed
# Cant test this until get_current_namespace is fixed and placed in this function over using `self`
def _get_ingress_domain(self): # pragma: no cover
try:
config_check()
api_client = client.NetworkingV1Api(api_config_handler())
if self.config.namespace != None:
namespace = self.config.namespace
else:
namespace = get_current_namespace()
ingresses = api_client.list_namespaced_ingress(namespace)
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)
config_check()

if self.config.namespace != None:
namespace = self.config.namespace
else:
namespace = get_current_namespace()
domain = None
for ingress in ingresses.items:
if ingress.spec.rules[0].http.paths[0].backend.service.port.number == 10001:
domain = ingress.spec.rules[0].host

if is_openshift_cluster():
try:
api_instance = client.CustomObjectsApi(api_config_handler())

routes = api_instance.list_namespaced_custom_object(
group="route.openshift.io",
version="v1",
namespace=namespace,
plural="routes",
)
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)

for route in routes["items"]:
if route["spec"]["port"]["targetPort"] == "client":
domain = route["spec"]["host"]
else:
try:
api_client = client.NetworkingV1Api(api_config_handler())
ingresses = api_client.list_namespaced_ingress(namespace)
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)

for ingress in ingresses.items:
if ingress.spec.rules[0].http.paths[0].backend.service.port.number == 10001:
domain = ingress.spec.rules[0].host
return domain


Expand Down
38 changes: 36 additions & 2 deletions src/codeflare_sdk/templates/base-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ spec:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ray-dashboard-raytest
name: ray-dashboard-deployment-ingress
namespace: default
annotations:
annotations-example:annotations-example
Expand All @@ -306,12 +306,28 @@ spec:
pathType: Prefix
path: /
host: ray-dashboard-raytest.<ingress-domain>
- replicas: 1
generictemplate:
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: ray-dashboard-deployment-route
namespace: default
labels:
# allows me to return name of service that Ray operator creates
odh-ray-cluster-service: deployment-name-head-svc
spec:
to:
kind: Service
name: deployment-name-head-svc
port:
targetPort: dashboard
- replicas: 1
generictemplate:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: rayclient-deployment-name
name: rayclient-deployment-ingress
namespace: default
annotations:
annotations-example:annotations-example
Expand All @@ -330,6 +346,24 @@ spec:
path: ''
pathType: ImplementationSpecific
host: rayclient-raytest.<ingress-domain>
- replicas: 1
generictemplate:
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: rayclient-deployment-route
namespace: default
labels:
# allows me to return name of service that Ray operator creates
odh-ray-cluster-service: deployment-name-head-svc
spec:
port:
targetPort: client
tls:
termination: passthrough
to:
kind: Service
name: deployment-name-head-svc
- replicas: 1
generictemplate:
apiVersion: v1
Expand Down
Loading