Skip to content

Commit 13c4cdd

Browse files
committed
Fixed unit tests
1 parent dc4a051 commit 13c4cdd

File tree

7 files changed

+101
-36
lines changed

7 files changed

+101
-36
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
from ..utils import pretty_print
2828
from ..utils.generate_yaml import (
2929
generate_appwrapper,
30-
generate_custom_ingresses,
31-
generate_default_ingresses,
3230
)
3331
from ..utils.kube_api_helpers import _kube_api_error_handling
3432
from .config import ClusterConfiguration

src/codeflare_sdk/templates/base-template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ spec:
297297
annotations:
298298
annotations-example:annotations-example
299299
spec:
300+
ingressClassName: nginx
300301
rules:
301302
- http:
302303
paths:
@@ -320,7 +321,7 @@ spec:
320321
labels:
321322
odh-ray-cluster-service: deployment-name-head-svc
322323
spec:
323-
ingressClassName: openshift-default
324+
ingressClassName: nginx
324325
rules:
325326
- http:
326327
paths:

src/codeflare_sdk/utils/generate_yaml.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
from kubernetes import client, config
2525
from .kube_api_helpers import _kube_api_error_handling
2626
from ..cluster.auth import api_config_handler, config_check
27-
from jinja2 import Template
28-
import pathlib
2927

3028

3129
def read_template(template):
@@ -56,8 +54,8 @@ def is_openshift_cluster():
5654
)
5755

5856
return True
59-
except client.ApiException as e:
60-
if e.status == 404:
57+
except client.ApiException as e: # pragma: no cover
58+
if e.status == 404 or e.status == 403:
6159
return False
6260
else:
6361
print(f"Error detecting cluster type defaulting to Kubernetes: {e}")
@@ -66,21 +64,21 @@ def is_openshift_cluster():
6664

6765
def update_dashboard_ingress(
6866
ingress_item, cluster_name, namespace, ingress_options, ingress_domain
69-
):
67+
): # pragma: no cover
7068
metadata = ingress_item.get("generictemplate", {}).get("metadata")
7169
spec = ingress_item.get("generictemplate", {}).get("spec")
7270
if ingress_options != {}:
7371
for index, ingress_option in enumerate(ingress_options["ingresses"]):
7472
if "ingressName" not in ingress_option.keys():
75-
return ValueError(
73+
raise ValueError(
7674
f"Error: 'ingressName' is missing or empty for ingress item at index {index}"
7775
)
7876
if "port" not in ingress_option.keys():
79-
return ValueError(
77+
raise ValueError(
8078
f"Error: 'port' is missing or empty for ingress item at index {index}"
8179
)
8280
elif not isinstance(ingress_option["port"], int):
83-
return ValueError(
81+
raise ValueError(
8482
f"Error: 'port' is not of type int for ingress item at index {index}"
8583
)
8684
if ingress_option["port"] == 8265:
@@ -105,7 +103,7 @@ def update_dashboard_ingress(
105103
else:
106104
spec["rules"][0]["host"] = ingress_option["host"]
107105
if "ingressClassName" not in ingress_option.keys():
108-
ingress_options["ingressClassName"] = None
106+
del spec["ingressClassName"]
109107
else:
110108
spec["ingressClassName"] = ingress_option["ingressClassName"]
111109

@@ -125,6 +123,7 @@ def update_dashboard_ingress(
125123
ingress = api_client.get_cluster_custom_object(
126124
"config.openshift.io", "v1", "ingresses", "cluster"
127125
)
126+
del spec["ingressClassName"]
128127
except Exception as e: # pragma: no cover
129128
return _kube_api_error_handling(e)
130129
domain = ingress["spec"]["domain"]
@@ -140,21 +139,21 @@ def update_dashboard_ingress(
140139

141140
def update_rayclient_ingress(
142141
ingress_item, cluster_name, namespace, ingress_options, ingress_domain
143-
):
142+
): # pragma: no cover
144143
metadata = ingress_item.get("generictemplate", {}).get("metadata")
145144
spec = ingress_item.get("generictemplate", {}).get("spec")
146145
if ingress_options != {}:
147146
for index, ingress_option in enumerate(ingress_options["ingresses"]):
148147
if "ingressName" not in ingress_option.keys():
149-
return ValueError(
148+
raise ValueError(
150149
f"Error: 'ingressName' is missing or empty for ingress item at index {index}"
151150
)
152151
if "port" not in ingress_option.keys():
153-
return ValueError(
152+
raise ValueError(
154153
f"Error: 'port' is missing or empty for ingress item at index {index}"
155154
)
156155
elif not isinstance(ingress_option["port"], int):
157-
return ValueError(
156+
raise ValueError(
158157
f"Error: 'port' is not of type int for ingress item at index {index}"
159158
)
160159
if ingress_option["port"] == 10001:
@@ -179,7 +178,7 @@ def update_rayclient_ingress(
179178
else:
180179
spec["rules"][0]["host"] = ingress_option["host"]
181180
if "ingressClassName" not in ingress_option.keys():
182-
ingress_option["ingressClassName"] = None
181+
del spec["ingressClassName"]
183182
else:
184183
spec["ingressClassName"] = ingress_option["ingressClassName"]
185184

@@ -457,23 +456,41 @@ def enable_local_interactive(
457456

458457
command = command.replace("deployment-name", cluster_name)
459458

460-
if is_openshift_cluster():
461-
# We can try get the domain through checking ingresses.config.openshift.io
462-
try:
463-
config_check()
464-
api_client = client.CustomObjectsApi(api_config_handler())
465-
ingress = api_client.get_cluster_custom_object(
466-
"config.openshift.io", "v1", "ingresses", "cluster"
467-
)
468-
except Exception as e: # pragma: no cover
469-
return _kube_api_error_handling(e)
470-
domain = ingress["spec"]["domain"]
471-
elif ingress_domain is None:
472-
raise ValueError(
473-
"ingress_domain is invalid. For Kubernetes Clusters please specify an ingress domain"
474-
)
459+
if ingress_options != {}:
460+
for index, ingress_option in enumerate(ingress_options["ingresses"]):
461+
if ingress_option["port"] == 10001:
462+
if "host" not in ingress_option.keys():
463+
raise ValueError(
464+
f"Client host is not specified please include a host for the ingress item at index {index}"
465+
)
466+
else:
467+
host = ingress_option["host"]
468+
domain_split = host.split(".", 1)
469+
if len(domain_split) > 1:
470+
domain = domain_split[1]
471+
else:
472+
raise ValueError(
473+
f"The client ingress host is configured incorrectly please specify a host with a correct domain for the ingress item at index {index}"
474+
)
475+
475476
else:
476-
domain = ingress_domain
477+
if is_openshift_cluster():
478+
# We can try get the domain through checking ingresses.config.openshift.io
479+
try:
480+
config_check()
481+
api_client = client.CustomObjectsApi(api_config_handler())
482+
ingress = api_client.get_cluster_custom_object(
483+
"config.openshift.io", "v1", "ingresses", "cluster"
484+
)
485+
except Exception as e: # pragma: no cover
486+
return _kube_api_error_handling(e)
487+
domain = ingress["spec"]["domain"]
488+
elif ingress_domain is None:
489+
raise ValueError(
490+
"ingress_domain is invalid. For Kubernetes Clusters please specify an ingress domain"
491+
)
492+
else:
493+
domain = ingress_domain
477494

478495
command = command.replace("server-name", domain)
479496
update_rayclient_ingress(

tests/test-case-prio.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,24 @@ spec:
179179
name: init-myservice
180180
priorityClassName: default
181181
replicas: 1
182+
- generictemplate:
183+
apiVersion: networking.k8s.io/v1
184+
kind: Ingress
185+
metadata:
186+
name: ray-dashboard-prio-test-cluster
187+
namespace: ns
188+
spec:
189+
ingressClassName: nginx
190+
rules:
191+
- host: ray-dashboard-prio-test-cluster-ns.apps.cluster.awsroute.org
192+
http:
193+
paths:
194+
- backend:
195+
service:
196+
name: prio-test-cluster-head-svc
197+
port:
198+
number: 8265
199+
path: /
200+
pathType: Prefix
201+
replicas: 1
182202
Items: []

tests/test-case.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,24 @@ spec:
176176
image: busybox:1.28
177177
name: init-myservice
178178
replicas: 1
179+
- generictemplate:
180+
apiVersion: networking.k8s.io/v1
181+
kind: Ingress
182+
metadata:
183+
name: ray-dashboard-unit-test-cluster
184+
namespace: ns
185+
spec:
186+
ingressClassName: nginx
187+
rules:
188+
- host: ray-dashboard-unit-test-cluster-ns.apps.cluster.awsroute.org
189+
http:
190+
paths:
191+
- backend:
192+
service:
193+
name: unit-test-cluster-head-svc
194+
port:
195+
number: 8265
196+
path: /
197+
pathType: Prefix
198+
replicas: 1
179199
Items: []

tests/unit_test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@
7575
createDDPJob_with_cluster,
7676
)
7777

78-
from codeflare_sdk.utils.generate_yaml import (
79-
gen_names,
80-
)
78+
from codeflare_sdk.utils.generate_yaml import gen_names, is_openshift_cluster
8179

8280
import openshift
8381
from openshift.selector import Selector
@@ -439,6 +437,16 @@ def test_local_client_url(mocker):
439437
)
440438

441439

440+
def test_is_openshift_cluster(mocker):
441+
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
442+
assert is_openshift_cluster() == False
443+
mocker.patch(
444+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
445+
return_value={"spec": {"domain": ""}},
446+
)
447+
assert is_openshift_cluster() == True
448+
449+
442450
def ray_addr(self, *args):
443451
return self._address
444452

tests/unit_test_support.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def createClusterConfig():
4646
instascale=True,
4747
machine_types=["cpu.small", "gpu.large"],
4848
image_pull_secrets=["unit-test-pull-secret"],
49+
ingress_domain="apps.cluster.awsroute.org",
4950
)
5051
return config
5152

0 commit comments

Comments
 (0)