Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit f7b59f7

Browse files
committed
#22 parse downtime-replicas as int
1 parent e787b0c commit f7b59f7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

kube_downscaler/scaler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def autoscale_resource(resource: pykube.objects.NamespacedAPIObject,
6969
logger.info('%s %s/%s within grace period (%ds), not scaling down (yet)',
7070
resource.kind, resource.namespace, resource.name, grace_period)
7171
else:
72-
target_replicas = resource.annotations.get(DOWNTIME_REPLICAS_ANNOTATION, 0)
72+
target_replicas = int(resource.annotations.get(DOWNTIME_REPLICAS_ANNOTATION, 0))
7373
logger.info('Scaling down %s %s/%s from %s to %s replicas (uptime: %s, downtime: %s)',
7474
resource.kind, resource.namespace, resource.name, replicas, target_replicas,
7575
uptime, downtime)

tests/test_autoscale_resource.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import datetime
55
from unittest.mock import MagicMock
66

7-
from kube_downscaler.scaler import autoscale_resource, EXCLUDE_ANNOTATION, ORIGINAL_REPLICAS_ANNOTATION
7+
from kube_downscaler.scaler import autoscale_resource, EXCLUDE_ANNOTATION, ORIGINAL_REPLICAS_ANNOTATION, DOWNTIME_REPLICAS_ANNOTATION
88

99

1010
@pytest.fixture
@@ -107,3 +107,24 @@ def test_scale_up(resource):
107107
autoscale_resource(resource, 'Mon-Fri 07:30-20:30 Europe/Berlin', 'never', False, False, now, 0)
108108
assert resource.replicas == 3
109109
resource.update.assert_called_once()
110+
111+
112+
def test_downtime_replicas_invalid(resource):
113+
resource.annotations = {DOWNTIME_REPLICAS_ANNOTATION: 'x'}
114+
resource.replicas = 2
115+
now = datetime.strptime('2018-10-23T21:56:00Z', '%Y-%m-%dT%H:%M:%SZ')
116+
resource.metadata = {'creationTimestamp': '2018-10-23T21:55:00Z'}
117+
autoscale_resource(resource, 'never', 'always', False, False, now, 0)
118+
assert resource.replicas == 2
119+
resource.update.assert_not_called()
120+
121+
122+
def test_downtime_replicas_valid(resource):
123+
resource.annotations = {DOWNTIME_REPLICAS_ANNOTATION: '1'}
124+
resource.replicas = 2
125+
now = datetime.strptime('2018-10-23T21:56:00Z', '%Y-%m-%dT%H:%M:%SZ')
126+
resource.metadata = {'creationTimestamp': '2018-10-23T21:55:00Z'}
127+
autoscale_resource(resource, 'never', 'always', False, False, now, 0)
128+
assert resource.replicas == 1
129+
resource.update.assert_called_once()
130+
assert resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] == '2'

0 commit comments

Comments
 (0)