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 5, 2023
1 parent bff2e16 commit a5a934a
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 @@ -58,7 +59,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 72 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L71-L72

Added lines #L71 - L72 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 74 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L74

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

Expand Down Expand Up @@ -105,13 +118,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 a5a934a

Please sign in to comment.