Skip to content

Commit

Permalink
Fix KeyErrors on reservation status
Browse files Browse the repository at this point in the history
  • Loading branch information
bsquizz committed Dec 13, 2021
1 parent d4fc8b9 commit 070a5ed
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
1 change: 1 addition & 0 deletions bonfire/bonfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bonfire/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"],
Expand Down
12 changes: 4 additions & 8 deletions bonfire/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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}")
Expand Down

0 comments on commit 070a5ed

Please sign in to comment.