Skip to content

Commit

Permalink
gce: Improve logging for routes cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Aug 10, 2023
1 parent 2480949 commit 4d0dc1f
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import json
from os.path import basename
from datetime import timezone
Expand Down Expand Up @@ -59,7 +60,19 @@ def _delete_resource(self, api_call, resource_name, *_, **kwargs) -> None:
if GCE.get_error_reason(err) == 'resourceInUseByAnotherResource':
self.log_dbg(f"{resource_type.title()} '{resource_name}' can not be deleted because in use")
elif GCE.get_error_reason(err) == 'badRequest':
self.log_err(f"{resource_type.title()} '{resource_name}' can not be deleted because of unknown reason")
# These are system generated routes when you create a network. These
# will be deleted by the deletion of the network and do not block the
# deletion of that network.
# There are no properties on the Route struct that indicate a route is a
# default one. Typically, the name will contain the word "default" or the
# description will contain the word "Default" but a property like Kind
# returns "compute#route" for all routes.
# All this creating false alarms in log which we want to prevent.
# Only way to prevent is mute error
if resource_type.title() == "Route" and "The local route cannot be deleted" in str(err):
self.log_info("Skip deletion of local route")

Check warning on line 73 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L72-L73

Added lines #L72 - L73 were not covered by tests
else:
self.log_err(f"{resource_type.title()} '{resource_name}' can not be deleted. Error : {err}")

Check warning on line 75 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L75

Added line #L75 was not covered by tests
else:
raise err

Expand Down Expand Up @@ -106,13 +119,9 @@ def delete_instance(self, instance_id, zone) -> None:

@staticmethod
def get_error_reason(error: "googleapiclient.errors.HttpError") -> str:
reason = "unknown"
try:
error_content = json.loads(error.content)
return error_content['error']['errors'][0]['reason']
except (KeyError, ValueError, IndexError):
pass
return reason
with contextlib.suppress(KeyError, ValueError, IndexError):
return json.loads(error.content)['error']['errors'][0]['reason']
return "unknown"

def cleanup_all(self) -> None:
self.log_info("Call cleanup_all")
Expand Down

0 comments on commit 4d0dc1f

Please sign in to comment.