From a0825ffd1eca5b86ffaf745f276490fc4c51974d Mon Sep 17 00:00:00 2001 From: Varun Yadav Date: Thu, 14 May 2026 21:09:40 +0530 Subject: [PATCH] fix: use .get() for optional health check config keys to prevent KeyError (#1310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: use .get() for optional health check config keys to prevent KeyError bearer_token, auth, exit_on_failure and verify_url were accessed with bare [] indexing. If a user omits any of these optional keys, the health check thread crashes with a KeyError that disappears silently — the telemetry queue never gets populated and health checks stop working with no log output. Replaced all four with .get() calls with safe defaults. Fixes #1309 Signed-off-by: v0idheaven * fix: address review feedback on HealthChecker config key handling - Add docstring to run_health_check for API clarity - Replace conditional url assignment with direct .get() + continue so a missing url skips the entry cleanly instead of falling through to make_request with a potentially stale url from a previous iteration Signed-off-by: v0idheaven --------- Signed-off-by: v0idheaven Co-authored-by: Paige Patton <64206430+paigerube14@users.noreply.github.com> --- krkn/utils/HealthChecker.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/krkn/utils/HealthChecker.py b/krkn/utils/HealthChecker.py index 839ce2ea..c6e87990 100644 --- a/krkn/utils/HealthChecker.py +++ b/krkn/utils/HealthChecker.py @@ -50,7 +50,12 @@ class HealthChecker: return response_data - def run_health_check(self, health_check_config, health_check_telemetry_queue: queue.Queue): + def run_health_check(self, health_check_config, health_check_telemetry_queue: queue.Queue): + """ + Runs health checks against configured URLs in a loop until iterations complete. + Populates health_check_telemetry_queue with results on completion. + Skips execution if health_check_config is not defined or contains no valid URLs. + """ if health_check_config and health_check_config["config"] and any(config.get("url") for config in health_check_config["config"]): health_check_telemetry = [] health_check_tracker = {} @@ -60,13 +65,16 @@ class HealthChecker: while self.current_iterations < self.iterations: for config in health_check_config.get("config"): auth, headers = None, None - verify_url = config["verify_url"] if "verify_url" in config else True - if config["url"]: url = config["url"] + verify_url = config.get("verify_url", True) + url = config.get("url") + if not url: + continue - if config["bearer_token"]: + if config.get("bearer_token"): bearer_token = "Bearer " + config["bearer_token"] headers = {"Authorization": bearer_token} - if config["auth"]: auth = tuple(config["auth"].split(',')) + if config.get("auth"): + auth = tuple(config["auth"].split(',')) try: response = self.make_request(url, auth, headers, verify_url) except Exception: @@ -82,7 +90,7 @@ class HealthChecker: if response["status_code"] != 200: if response_tracker[config["url"]] is not False: response_tracker[config["url"]] = False - if config["exit_on_failure"] is True and self.ret_value == 0: + if config.get("exit_on_failure") is True and self.ret_value == 0: self.ret_value = 2 else: if response["status_code"] != health_check_tracker[config["url"]]["status_code"]: