Skip to content

Commit 29a4fe8

Browse files
committed
Added is_openshift_cluster() function for ingress_domain check
1 parent b62db74 commit 29a4fe8

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

src/codeflare_sdk/utils/generate_yaml.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,32 @@ def gen_names(name):
4646
return name, name
4747

4848

49+
# Check if the ingress api cluster resource exists
50+
def is_openshift_cluster():
51+
try:
52+
config_check()
53+
api_instance = client.CustomObjectsApi(api_config_handler())
54+
api_instance.get_cluster_custom_object(
55+
"config.openshift.io", "v1", "ingresses", "cluster"
56+
)
57+
58+
return True
59+
except client.ApiException as e:
60+
if e.status == 404:
61+
return False
62+
else:
63+
print(f"Error detecting cluster type defaulting to Kubernetes: {e}")
64+
return False
65+
66+
4967
def generate_default_ingresses(
5068
cluster_name, namespace, ingress_domain, local_interactive
5169
): # pragma: no cover
5270
dir = pathlib.Path(__file__).parent.parent.resolve()
5371
with open(f"{dir}/templates/ingress-template.yaml.tmpl", "r") as template_file:
5472
ingress_template = Template(template_file.read())
5573

56-
# If the ingress domain is not specifically specified we can assume the user is on OpenShift
57-
if ingress_domain is not None:
58-
domain = ingress_domain
59-
ingressClassName = "nginx"
60-
annotations = {
61-
"nginx.ingress.kubernetes.io/rewrite-target": "/",
62-
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
63-
"nginx.ingress.kubernetes.io/ssl-passthrough": "true",
64-
}
65-
else:
66-
# We can try get the domain through checking ingresses.config.openshift.io
74+
if is_openshift_cluster():
6775
try:
6876
config_check()
6977
api_client = client.CustomObjectsApi(api_config_handler())
@@ -72,18 +80,26 @@ def generate_default_ingresses(
7280
)
7381
except Exception as e: # pragma: no cover
7482
return _kube_api_error_handling(e)
75-
if len(ingress) != 0:
76-
ingressClassName = "openshift-default"
77-
annotations = {
78-
"nginx.ingress.kubernetes.io/rewrite-target": "/",
79-
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
80-
"route.openshift.io/termination": "passthrough",
81-
}
82-
domain = ingress["spec"]["domain"]
83-
else:
84-
return ValueError(
85-
"ingressDomain is invalid For Kubernetes Clusters please specify an ingressDomain"
86-
)
83+
84+
ingressClassName = "openshift-default"
85+
annotations = {
86+
"nginx.ingress.kubernetes.io/rewrite-target": "/",
87+
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
88+
"route.openshift.io/termination": "passthrough",
89+
}
90+
domain = ingress["spec"]["domain"]
91+
elif ingress_domain is None:
92+
raise ValueError(
93+
"ingress_domain is invalid. For Kubernetes Clusters please specify an ingress domain"
94+
)
95+
else:
96+
domain = ingress_domain
97+
ingressClassName = "nginx"
98+
annotations = {
99+
"nginx.ingress.kubernetes.io/rewrite-target": "/",
100+
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
101+
"nginx.ingress.kubernetes.io/ssl-passthrough": "true",
102+
}
87103

88104
ingressOptions = {
89105
"ingresses": [
@@ -403,9 +419,7 @@ def enable_local_interactive(resources, cluster_name, namespace, ingress_domain)
403419

404420
command = command.replace("deployment-name", cluster_name)
405421

406-
if ingress_domain is not None:
407-
domain = ingress_domain
408-
else:
422+
if is_openshift_cluster():
409423
# We can try get the domain through checking ingresses.config.openshift.io
410424
try:
411425
config_check()
@@ -415,12 +429,14 @@ def enable_local_interactive(resources, cluster_name, namespace, ingress_domain)
415429
)
416430
except Exception as e: # pragma: no cover
417431
return _kube_api_error_handling(e)
418-
if len(ingress) != 0:
419-
domain = ingress["spec"]["domain"]
420-
else:
421-
return ValueError(
422-
"ingressDomain is invalid. For Kubernetes Clusters please specify an ingressDomain"
423-
)
432+
domain = ingress["spec"]["domain"]
433+
elif ingress_domain is None:
434+
raise ValueError(
435+
"ingress_domain is invalid. For Kubernetes Clusters please specify an ingress domain"
436+
)
437+
else:
438+
domain = ingress_domain
439+
424440
command = command.replace("server-name", domain)
425441

426442
item["generictemplate"]["spec"]["headGroupSpec"]["template"]["spec"][

0 commit comments

Comments
 (0)