From 727dbc06ab9b31cdb4af1ae2a16470fc975cc776 Mon Sep 17 00:00:00 2001 From: Samuel Esposito <40698384+samuel-esp@users.noreply.github.com> Date: Wed, 22 May 2024 11:32:55 +0200 Subject: [PATCH] 12fdfe21b3 (#41) fixed a bug that prevented ScaledObjects from being paused if the "paused-replicas" annotation was already present inside the object --- kube_downscaler/resources/keda.py | 10 +++++++++- kube_downscaler/scaler.py | 14 +++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/kube_downscaler/resources/keda.py b/kube_downscaler/resources/keda.py index 2b92132..9a3caee 100644 --- a/kube_downscaler/resources/keda.py +++ b/kube_downscaler/resources/keda.py @@ -10,8 +10,16 @@ class ScaledObject(NamespacedAPIObject): kind = "ScaledObject" keda_pause_annotation = "autoscaling.keda.sh/paused-replicas" + last_keda_pause_annotation_if_present = "downscaler/original-pause-replicas" @property def replicas(self): - replicas = 0 if ScaledObject.keda_pause_annotation in self.annotations else 1 + if ScaledObject.keda_pause_annotation in self.annotations: + if self.annotations[ScaledObject.keda_pause_annotation] == "0": + replicas = 0 + elif self.annotations[ScaledObject.keda_pause_annotation] != "0": + replicas = int(self.annotations[ScaledObject.keda_pause_annotation]) + else: + replicas = 1 + return replicas diff --git a/kube_downscaler/scaler.py b/kube_downscaler/scaler.py index ff2f552..422f0c6 100644 --- a/kube_downscaler/scaler.py +++ b/kube_downscaler/scaler.py @@ -206,9 +206,14 @@ def scale_up( f"Scaling up {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {original_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) elif resource.kind == "ScaledObject": - resource.annotations[ScaledObject.keda_pause_annotation] = None + if resource.annotations[ScaledObject.last_keda_pause_annotation_if_present] is not None: + paused_replicas = resource.annotations[ScaledObject.last_keda_pause_annotation_if_present] + resource.annotations[ScaledObject.keda_pause_annotation] = paused_replicas + resource.annotations[ScaledObject.last_keda_pause_annotation_if_present] = None + else: + resource.annotations[ScaledObject.keda_pause_annotation] = None logger.info( - f"Unpausing {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime}" + f"Unpausing {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) else: resource.replicas = original_replicas @@ -253,9 +258,12 @@ 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 resource.annotations[ScaledObject.keda_pause_annotation] = "0" logger.info( - f"Pausing {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime}" + f"Pausing {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) event_message = "Pausing KEDA ScaledObject" else: