From 070a5edebaeafe93914e247bdfba13c39e644bac Mon Sep 17 00:00:00 2001 From: Brandon Squizzato Date: Mon, 13 Dec 2021 11:18:07 -0500 Subject: [PATCH] Fix KeyErrors on reservation status --- bonfire/bonfire.py | 1 + bonfire/namespaces.py | 4 ++-- bonfire/openshift.py | 12 ++++-------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/bonfire/bonfire.py b/bonfire/bonfire.py index e7b8888e..d48007f4 100755 --- a/bonfire/bonfire.py +++ b/bonfire/bonfire.py @@ -73,6 +73,7 @@ def wrapper(*args, **kwargs): except FatalError as err: _error(f"{command}: hit fatal error: {err}") except Exception as err: + log.exception("hit unexpected error") _error(f"{command}: hit unexpected error: {err}") return wrapper diff --git a/bonfire/namespaces.py b/bonfire/namespaces.py index 2f0ea20d..33a889d7 100644 --- a/bonfire/namespaces.py +++ b/bonfire/namespaces.py @@ -114,7 +114,7 @@ def refresh(self, data): if self.reserved: res = get_reservation(namespace=self.name) - if res: + if res and res.get("status"): self.requester = res["spec"]["requester"] self.expires = _parse_time(res["status"]["expiration"]) else: @@ -232,7 +232,7 @@ def release_namespace(namespace): def extend_namespace(namespace, duration): res = get_reservation(namespace=namespace) if res: - if res["status"]["state"] == "expired": + if res.get("status", {}).get("state") == "expired": log.error( "The reservation for namespace %s has expired. Please reserve a new namespace", res["status"]["namespace"], diff --git a/bonfire/openshift.py b/bonfire/openshift.py index 92fac850..57a28443 100644 --- a/bonfire/openshift.py +++ b/bonfire/openshift.py @@ -761,12 +761,7 @@ def wait_on_reservation(res_name, timeout): def _find_reservation(): res = get_json("reservation", name=res_name) - try: - if res["status"]["namespace"]: - return res["status"]["namespace"] - return False - except (KeyError, IndexError): - return False + return res.get("status", {}).get("namespace", False) ns_name, elapsed = wait_for( _find_reservation, @@ -785,7 +780,8 @@ def check_for_existing_reservation(requester): all_res = get_json("reservation") for res in all_res["items"]: - if res["spec"]["requester"] == requester and res["status"]["state"] == "active": + res_state = res.get("status", {}).get("state") == "active" + if res["spec"]["requester"] == requester and res_state == "active": return True return False @@ -801,7 +797,7 @@ def get_reservation(name=None, namespace=None, requester=None): elif namespace: all_res = get_json("reservation") for res in all_res["items"]: - if res["status"]["namespace"] == namespace: + if res.get("status", {}).get("namespace") == namespace: return res elif requester: all_res = get_json("reservation", label=f"requester={requester}")