Skip to content

Commit

Permalink
improving kubernetes client error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Haigh <mhaigh@netapp.com>
  • Loading branch information
MichaelHaigh committed Apr 3, 2024
1 parent 11a0f72 commit b6304e6
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions astraSDK/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
import kubernetes
import sys
import urllib3
import yaml
from datetime import datetime, timedelta, timezone

Expand Down Expand Up @@ -89,11 +90,11 @@ def main(
self.formatPrint(resp, plural)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
if e.status == 404 and e.reason == "Not Found":
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/namespaces/{namespace}/{plural}")
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)

def formatPrint(self, resp, plural, quiet=None, output=None, verbose=None):
if quiet is None:
Expand Down Expand Up @@ -331,11 +332,11 @@ def main(
print(json.dumps(resp) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
if e.status == 404 and e.reason == "Not Found":
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/{plural}")
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class createResource(KubeCommon):
Expand Down Expand Up @@ -392,11 +393,11 @@ def main(
print(json.dumps(resp) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
if e.status == 404 and e.reason == "Not Found":
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/namespaces/{namespace}/{plural}")
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class destroyResource(KubeCommon):
Expand Down Expand Up @@ -453,11 +454,11 @@ def main(
print(json.dumps(resp) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
if e.status == 404 and e.reason == "Not Found":
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/namespaces/{namespace}/{plural}")
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class updateClusterResource(KubeCommon):
Expand Down Expand Up @@ -507,11 +508,11 @@ def main(
print(json.dumps(resp) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
if e.status == 404 and e.reason == "Not Found":
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/{plural}")
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class getNamespaces(KubeCommon):
Expand Down Expand Up @@ -600,9 +601,9 @@ def main(self, systemNS=None, nameFilter=None, unassociated=False, minuteFilter=
print(json.dumps(resp, default=str) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class getSecrets(KubeCommon):
Expand Down Expand Up @@ -656,9 +657,9 @@ def main(self, namespace="astra-connector"):
print(json.dumps(resp, default=str) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class destroySecret(KubeCommon):
Expand Down Expand Up @@ -705,9 +706,9 @@ def main(self, name, namespace="astra-connector"):
print(json.dumps(resp) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class getStorageClasses(KubeCommon):
Expand Down Expand Up @@ -751,9 +752,9 @@ def main(self):
print(json.dumps(resp, default=str) if type(resp) is dict else resp)
return resp

except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class createV1Secret(KubeCommon):
Expand Down Expand Up @@ -799,9 +800,9 @@ def main(self, v1SecretObj, namespace="astra-connector"):
if not self.quiet:
print(json.dumps(resp, default=str) if type(resp) is dict else resp)
return resp
except kubernetes.client.rest.ApiException as e:
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e)
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)


class createRegCred(KubeCommon, SDKCommon):
Expand Down

0 comments on commit b6304e6

Please sign in to comment.