Skip to content

Commit c05158f

Browse files
committed
Implement error handling for edge case
1 parent 036ca19 commit c05158f

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

netapp_dataops_k8s/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ If an error is encountered, the function will raise an exception of one of the f
853853
```py
854854
InvalidConfigError # kubeconfig file is missing or is invalid.
855855
APIConnectionError # The Kubernetes API returned an error.
856+
ServiceUnavailableError # A Kubernetes service is not available.
856857
```
857858

858859
<a name="lib-create-jupyterlab"></a>
@@ -892,6 +893,7 @@ If an error is encountered, the function will raise an exception of one of the f
892893
```py
893894
InvalidConfigError # kubeconfig file is missing or is invalid.
894895
APIConnectionError # The Kubernetes API returned an error.
896+
ServiceUnavailableError # A Kubernetes service is not available.
895897
```
896898

897899
<a name="lib-delete-jupyterlab"></a>

netapp_dataops_k8s/netapp_dataops/k8s.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class InvalidConfigError(Exception):
4646
pass
4747

4848

49+
class ServiceUnavailableError(Exception):
50+
'''Error that will be raised when a service is not available'''
51+
pass
52+
53+
4954
#
5055
# Private functions
5156
#
@@ -127,20 +132,26 @@ def _retrieve_jupyter_lab_url(workspaceName: str, namespace: str = "default", pr
127132
_print_invalid_config_error()
128133
raise InvalidConfigError()
129134

130-
# Retrieve access port
131135
try:
132136
api = client.CoreV1Api()
133137
serviceStatus = api.read_namespaced_service(namespace=namespace,
134138
name=_get_jupyter_lab_service(workspaceName=workspaceName))
135-
port = serviceStatus.spec.ports[0].node_port
136-
loadBalancerIP = serviceStatus.status.load_balancer.ingress[0].ip
137139

138140
# Check if service type is LoadBalancer
139141
if serviceStatus.spec.type == "LoadBalancer":
140142
# Construct and return url
143+
try :
144+
loadBalancerIP = serviceStatus.status.load_balancer.ingress[0].ip
145+
except :
146+
if printOutput :
147+
print("Error: Kubernetes Service for workspace is not available.")
148+
raise ServiceUnavailableError()
141149
return "http://" + loadBalancerIP
142150
else:
143-
# Retrieve node IP (random node)
151+
# Retrieve access port
152+
port = serviceStatus.spec.ports[0].node_port
153+
154+
# Retrieve node IP (random node)
144155
try:
145156
api = client.CoreV1Api()
146157
nodes = api.list_node()
@@ -608,7 +619,7 @@ def create_jupyter_lab(workspace_name: str, workspace_size: str, storage_class:
608619
# Step 4 - Retrieve access URL
609620
try:
610621
url = _retrieve_jupyter_lab_url(workspaceName=workspace_name, namespace=namespace, printOutput=print_output)
611-
except APIConnectionError as err:
622+
except (APIConnectionError, ServiceUnavailableError) as err:
612623
if print_output:
613624
print("Aborting workspace creation...")
614625
raise
@@ -951,8 +962,14 @@ def list_jupyter_labs(namespace: str = "default", print_output: bool = False) ->
951962
workspaceDict["StorageClass"] = ""
952963

953964
# Retrieve access URL
954-
workspaceDict["Access URL"] = _retrieve_jupyter_lab_url(workspaceName=workspaceName, namespace=namespace,
955-
printOutput=print_output)
965+
try :
966+
workspaceDict["Access URL"] = _retrieve_jupyter_lab_url(workspaceName=workspaceName, namespace=namespace, printOutput=False)
967+
except ServiceUnavailableError :
968+
workspaceDict["Access URL"] = "unavailable"
969+
except APIConnectionError as err:
970+
if print_output:
971+
print("Error: Kubernetes API Error: ", err)
972+
raise APIConnectionError(err)
956973

957974
# Retrieve clone details
958975
try:

0 commit comments

Comments
 (0)