@@ -46,24 +46,32 @@ def gen_names(name):
46
46
return name , name
47
47
48
48
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
+
49
67
def generate_default_ingresses (
50
68
cluster_name , namespace , ingress_domain , local_interactive
51
69
): # pragma: no cover
52
70
dir = pathlib .Path (__file__ ).parent .parent .resolve ()
53
71
with open (f"{ dir } /templates/ingress-template.yaml.tmpl" , "r" ) as template_file :
54
72
ingress_template = Template (template_file .read ())
55
73
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 ():
67
75
try :
68
76
config_check ()
69
77
api_client = client .CustomObjectsApi (api_config_handler ())
@@ -72,18 +80,26 @@ def generate_default_ingresses(
72
80
)
73
81
except Exception as e : # pragma: no cover
74
82
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
+ }
87
103
88
104
ingressOptions = {
89
105
"ingresses" : [
@@ -403,9 +419,7 @@ def enable_local_interactive(resources, cluster_name, namespace, ingress_domain)
403
419
404
420
command = command .replace ("deployment-name" , cluster_name )
405
421
406
- if ingress_domain is not None :
407
- domain = ingress_domain
408
- else :
422
+ if is_openshift_cluster ():
409
423
# We can try get the domain through checking ingresses.config.openshift.io
410
424
try :
411
425
config_check ()
@@ -415,12 +429,14 @@ def enable_local_interactive(resources, cluster_name, namespace, ingress_domain)
415
429
)
416
430
except Exception as e : # pragma: no cover
417
431
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
+
424
440
command = command .replace ("server-name" , domain )
425
441
426
442
item ["generictemplate" ]["spec" ]["headGroupSpec" ]["template" ]["spec" ][
0 commit comments