From d74d9ac09d97eaaded4df3b3837bfa187df1d2c6 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Tue, 12 Dec 2023 14:48:27 -0800 Subject: [PATCH] Recreate configmaps if missing (#1444) If configmap is missing (eg. was accidentally deleted from k8s) recreate the configmap when updating the crawl workflow or running a crawl. Previously, this would result in an error, but now the configmap should be correctly recreated. --- backend/btrixcloud/crawlconfigs.py | 29 ++++++++++++++++++++++++----- backend/btrixcloud/crawlmanager.py | 6 +++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/backend/btrixcloud/crawlconfigs.py b/backend/btrixcloud/crawlconfigs.py index e6befb4b..b1db641d 100644 --- a/backend/btrixcloud/crawlconfigs.py +++ b/backend/btrixcloud/crawlconfigs.py @@ -254,6 +254,25 @@ class CrawlConfigOps: return False + async def readd_configmap( + self, + crawlconfig: CrawlConfig, + org: Organization, + profile_filename: Optional[str] = None, + ) -> None: + """readd configmap that may have been deleted / is invalid""" + + if profile_filename is None: + _, profile_filename = await self._lookup_profile(crawlconfig.profileid, org) + + await self.crawl_manager.add_crawl_config( + crawlconfig=crawlconfig, + storage=org.storage, + run_now=False, + out_filename=self.default_filename_template, + profile_filename=profile_filename or "", + ) + async def update_crawl_config( self, cid: UUID, org: Organization, user: User, update: UpdateCrawlConfig ) -> dict[str, bool]: @@ -352,6 +371,9 @@ class CrawlConfigOps: await self.crawl_manager.update_crawl_config( crawlconfig, update, profile_filename ) + except FileNotFoundError: + await self.readd_configmap(crawlconfig, org, profile_filename) + except Exception as exc: print(exc, flush=True) # pylint: disable=raise-missing-from @@ -776,12 +798,9 @@ class CrawlConfigOps: # ensure crawlconfig exists try: await self.crawl_manager.get_configmap(crawlconfig.id) + # pylint: disable=bare-except except: - # pylint: disable=broad-exception-raised,raise-missing-from - raise HTTPException( - status_code=404, - detail=f"crawl-config-{cid} missing, can not start crawl", - ) + await self.readd_configmap(crawlconfig, org) if await self.org_ops.storage_quota_reached(org.id): raise HTTPException(status_code=403, detail="storage_quota_reached") diff --git a/backend/btrixcloud/crawlmanager.py b/backend/btrixcloud/crawlmanager.py index 531ed323..ee4d5a5c 100644 --- a/backend/btrixcloud/crawlmanager.py +++ b/backend/btrixcloud/crawlmanager.py @@ -386,7 +386,11 @@ class CrawlManager(K8sAPI): profile_filename: Optional[str] = None, update_config: bool = False, ) -> None: - config_map = await self.get_configmap(str(crawlconfig.id)) + try: + config_map = await self.get_configmap(str(crawlconfig.id)) + # pylint: disable=raise-missing-from + except: + raise FileNotFoundError(str(crawlconfig.id)) if update.scale is not None: config_map.data["INITIAL_SCALE"] = str(update.scale)