From a9bf8a4c7a0cf1dbec73ef3206ddf9e3cfd851d0 Mon Sep 17 00:00:00 2001 From: MoritzWeber Date: Tue, 1 Oct 2024 17:06:21 +0200 Subject: [PATCH] refactor: Tune log levels - Refresh token failure was promoted to INFO - Session metric not found was reduced to DEBUG - Not found deployment don't raise an exception / log message anymore --- .../capellacollab/core/authentication/oidc.py | 6 +- backend/capellacollab/sessions/injection.py | 2 +- .../capellacollab/sessions/operators/k8s.py | 63 ++++++++++++------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/backend/capellacollab/core/authentication/oidc.py b/backend/capellacollab/core/authentication/oidc.py index ea65eaadf..f2ed30c8b 100644 --- a/backend/capellacollab/core/authentication/oidc.py +++ b/backend/capellacollab/core/authentication/oidc.py @@ -146,8 +146,6 @@ def refresh_token(self, _refresh_token: str) -> dict[str, t.Any]: return self.web_client.parse_request_body_response( r.text, scope=self.oidc_config.get_scopes() ) - except Exception as e: - logger.debug( - "Could not refresh token because of exception %s", str(e) - ) + except Exception: + logger.info("Could not refresh token", exc_info=True) raise exceptions.RefreshTokenSignatureExpired() diff --git a/backend/capellacollab/sessions/injection.py b/backend/capellacollab/sessions/injection.py index 06effd3f7..7338dcd78 100644 --- a/backend/capellacollab/sessions/injection.py +++ b/backend/capellacollab/sessions/injection.py @@ -31,7 +31,7 @@ def get_last_seen(sid: str) -> str: if sid == session["metric"]["session_id"]: return _get_last_seen(float(session["value"][1])) - log.error("No session was found.") + log.debug("Couldn't find Prometheus metrics for session %s.", sid) except Exception: log.exception("Exception during fetching of last seen.") return "UNKNOWN" diff --git a/backend/capellacollab/sessions/operators/k8s.py b/backend/capellacollab/sessions/operators/k8s.py index 5b1e408e2..61183e83b 100644 --- a/backend/capellacollab/sessions/operators/k8s.py +++ b/backend/capellacollab/sessions/operators/k8s.py @@ -336,8 +336,12 @@ def delete_cronjob(self, _id: str): self.v1_batch.delete_namespaced_cron_job( namespace=namespace, name=_id ) - except exceptions.ApiException: - log.exception("Error deleting cronjob with name: %s", _id) + except exceptions.ApiException as e: + # Cronjob doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return + raise def delete_job(self, name: str): log.info("Deleting job '%s' in cluster", name) @@ -345,8 +349,12 @@ def delete_job(self, name: str): self.v1_batch.delete_namespaced_job( namespace=namespace, name=name, propagation_policy="Background" ) - except exceptions.ApiException: - log.error("Error deleting job with name: %s", name) + except exceptions.ApiException as e: + # Job doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return + raise def get_pod_name_from_job_name(self, job_name: str) -> str | None: return self._get_pod_id(label_selector=f"job-name={job_name}") @@ -893,30 +901,42 @@ def _create_promtail_configmap( def _delete_deployment(self, name: str) -> client.V1Status | None: try: return self.v1_apps.delete_namespaced_deployment(name, namespace) - except exceptions.ApiException: - log.exception("Error deleting deployment with name: %s", name) - return None + except exceptions.ApiException as e: + # Deployment doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return None + raise def delete_secret(self, name: str) -> kubernetes.client.V1Status | None: try: return self.v1_core.delete_namespaced_secret(name, namespace) - except client.exceptions.ApiException: - log.exception("Error deleting secret with name: %s", name) - return None + except exceptions.ApiException as e: + # Secret doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return None + raise def _delete_config_map(self, name: str) -> client.V1Status | None: try: return self.v1_core.delete_namespaced_config_map(name, namespace) - except exceptions.ApiException: - log.exception("Error deleting config map with name: %s", name) - return None + except exceptions.ApiException as e: + # Config map doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return None + raise def _delete_service(self, name: str) -> client.V1Status | None: try: return self.v1_core.delete_namespaced_service(name, namespace) - except exceptions.ApiException: - log.exception("Error deleting service with name: %s", name) - return None + except exceptions.ApiException as e: + # Service doesn't exist or was already deleted + # Nothing to do + if e.status == http.HTTPStatus.NOT_FOUND: + return None + raise def _delete_disruptionbudget(self, name: str) -> client.V1Status | None: try: @@ -924,14 +944,11 @@ def _delete_disruptionbudget(self, name: str) -> client.V1Status | None: name, namespace ) except exceptions.ApiException as e: - # Pod disruption budge doesn't exist or was already deleted + # Pod disruption budget doesn't exist or was already deleted # Nothing to do - if not e.status == http.HTTPStatus.NOT_FOUND: - log.exception( - "Error deleting discruptionbudget with name: %s", name - ) - - return None + if e.status == http.HTTPStatus.NOT_FOUND: + return None + raise def _get_pod_name(self, _id: str) -> str: return self.get_pods(label_selector=f"app={_id}")[0].metadata.name