Skip to content

Commit

Permalink
improving k8s.py error messaging
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 17, 2024
1 parent 9a7664b commit 5348330
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
9 changes: 9 additions & 0 deletions astraSDK/common.py
Expand Up @@ -323,6 +323,15 @@ def notInstalled(self, path):
)
raise SystemExit()

def printKubeError(self, e):
if hasattr(e, "body"):
e.text = e.body
self.printError(e)
elif hasattr(e, "reason"):
self.printError(e.reason)
else:
self.printError(e)

class WriteVerbose:
def __init__(self):
self.content = []
Expand Down
26 changes: 13 additions & 13 deletions astraSDK/k8s.py
Expand Up @@ -94,7 +94,7 @@ def main(
sys.stdout = sys.__stdout__
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.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)

def formatPrint(self, resp, plural, quiet=None, output=None, verbose=None):
if quiet is None:
Expand Down Expand Up @@ -148,7 +148,7 @@ def getTableInfo(self, plural, headers=False):
"status",
]
return [
"status.observedSpec.natsSyncClient.cloudBridgeURL",
"spec.natsSyncClient.cloudBridgeURL",
"status.natsSyncClient.registered",
"status.natsSyncClient.astraClusterID",
"status.natsSyncClient.astraConnectorID",
Expand Down Expand Up @@ -336,11 +336,11 @@ def main(
sys.stdout = sys.__stdout__
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/{plural}")
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)


class createResource(KubeCommon):
"""Creates a cluster scoped Custom Resource"""
"""Creates a namespace scoped Custom Resource"""

def __init__(
self, quiet=True, dry_run=False, verbose=False, config_context=None, skip_tls_verify=False
Expand Down Expand Up @@ -397,11 +397,11 @@ def main(
sys.stdout = sys.__stdout__
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.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)


class destroyResource(KubeCommon):
"""Destroys a cluster scoped Custom Resource"""
"""Destroys a namespace scoped Custom Resource"""

def __init__(
self, quiet=True, dry_run=False, verbose=False, config_context=None, skip_tls_verify=False
Expand Down Expand Up @@ -458,7 +458,7 @@ def main(
sys.stdout = sys.__stdout__
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.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)


class updateClusterResource(KubeCommon):
Expand Down Expand Up @@ -512,7 +512,7 @@ def main(
sys.stdout = sys.__stdout__
if hasattr(e, "status") and e.status == 404 and e.reason == "Not Found":
self.notInstalled(f"/apis/{group}/{version}/{plural}")
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)


class getNamespaces(KubeCommon):
Expand Down Expand Up @@ -603,7 +603,7 @@ def main(self, systemNS=None, nameFilter=None, unassociated=False, minuteFilter=

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


class getSecrets(KubeCommon):
Expand Down Expand Up @@ -659,7 +659,7 @@ def main(self, namespace="astra-connector"):

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


class destroySecret(KubeCommon):
Expand Down Expand Up @@ -708,7 +708,7 @@ def main(self, name, namespace="astra-connector"):

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


class getStorageClasses(KubeCommon):
Expand Down Expand Up @@ -754,7 +754,7 @@ def main(self):

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


class createV1Secret(KubeCommon):
Expand Down Expand Up @@ -802,7 +802,7 @@ def main(self, v1SecretObj, namespace="astra-connector"):
return resp
except (kubernetes.client.rest.ApiException, urllib3.exceptions.MaxRetryError) as e:
sys.stdout = sys.__stdout__
self.printError(e.reason) if hasattr(e, "reason") else self.printError(e)
self.printKubeError(e)


class createRegCred(KubeCommon, SDKCommon):
Expand Down

0 comments on commit 5348330

Please sign in to comment.