From 7102de09acfcedb752325f024ad9042388386b20 Mon Sep 17 00:00:00 2001 From: Ro'e Katz Date: Wed, 20 Sep 2023 14:52:07 +0300 Subject: [PATCH] ApiPolicySource: Log errors in bundle init/update before retrying --- .../opal_common/sources/api_policy_source.py | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/packages/opal-common/opal_common/sources/api_policy_source.py b/packages/opal-common/opal_common/sources/api_policy_source.py index 02818d328..6b04054d2 100644 --- a/packages/opal-common/opal_common/sources/api_policy_source.py +++ b/packages/opal-common/opal_common/sources/api_policy_source.py @@ -76,39 +76,51 @@ async def get_initial_policy_state_from_remote(self): """init remote data to local repo.""" async for attempt in AsyncRetrying(wait=wait_fixed(5)): with attempt: - await self.fetch_policy_bundle_from_api_source( - self.remote_source_url, self.token - ) - self.local_git = self.tar_to_git.create_local_git() + try: + await self.fetch_policy_bundle_from_api_source( + self.remote_source_url, self.token + ) + self.local_git = self.tar_to_git.create_local_git() + except Exception: + logger.exception( + "Failed to load initial policy from remote API bundle server" + ) + raise async def api_update_policy(self) -> Tuple[bool, str, str]: async for attempt in AsyncRetrying(wait=wait_fixed(5)): with attempt: - ( - tmp_bundle_path, - prev_version, - current_hash, - ) = await self.fetch_policy_bundle_from_api_source( - self.remote_source_url, self.token - ) - if tmp_bundle_path and prev_version and current_hash: - commit_msg = f"new version {current_hash}" + try: ( - self.local_git, - prev_commit, - new_commit, - ) = self.tar_to_git.extract_bundle_to_local_git( - commit_msg=commit_msg - ) - return ( - True, + tmp_bundle_path, prev_version, current_hash, - prev_commit, - new_commit, + ) = await self.fetch_policy_bundle_from_api_source( + self.remote_source_url, self.token + ) + if tmp_bundle_path and prev_version and current_hash: + commit_msg = f"new version {current_hash}" + ( + self.local_git, + prev_commit, + new_commit, + ) = self.tar_to_git.extract_bundle_to_local_git( + commit_msg=commit_msg + ) + return ( + True, + prev_version, + current_hash, + prev_commit, + new_commit, + ) + else: + return False, None, current_hash, None, None + except Exception as e: + logger.exception( + f"Failed to update policy from remote API bundle server" ) - else: - return False, None, current_hash, None, None + raise def build_auth_headers(self, token=None, path=None): # if it's a simple HTTP server with a bearer token