From 9f4913d0bea1346a3ee70773347fcc1915c889cb Mon Sep 17 00:00:00 2001 From: Philipp Hellmich Date: Thu, 22 Aug 2024 10:20:16 +0200 Subject: [PATCH] Fix KEDA ScaledObject KeyError Safely check if the key exists before accessing it --- kube_downscaler/scaler.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kube_downscaler/scaler.py b/kube_downscaler/scaler.py index d509d74..c8130e6 100644 --- a/kube_downscaler/scaler.py +++ b/kube_downscaler/scaler.py @@ -660,9 +660,10 @@ def scale_down( f"Scaling down {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {target_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) elif resource.kind == "ScaledObject": - if resource.annotations[ScaledObject.keda_pause_annotation] is not None: - paused_replicas = resource.annotations[ScaledObject.keda_pause_annotation] - resource.annotations[ScaledObject.last_keda_pause_annotation_if_present] = paused_replicas + if ScaledObject.keda_pause_annotation in resource.annotations: + if resource.annotations[ScaledObject.keda_pause_annotation] is not None: + paused_replicas = resource.annotations[ScaledObject.keda_pause_annotation] + resource.annotations[ScaledObject.last_keda_pause_annotation_if_present] = paused_replicas resource.annotations[ScaledObject.keda_pause_annotation] = "0" logger.info( f"Pausing {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})"