Skip to content

Commit

Permalink
12fdfe21b3 (#41)
Browse files Browse the repository at this point in the history
fixed a bug that prevented ScaledObjects from being paused if the "paused-replicas" annotation was already present inside the object
  • Loading branch information
samuel-esp authored May 22, 2024
1 parent f14f951 commit 727dbc0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion kube_downscaler/resources/keda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 11 additions & 3 deletions kube_downscaler/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 727dbc0

Please sign in to comment.