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

Commit

Permalink
Bugfix: removes hard-coded zero in 'scale-up' logic (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Gwartz authored and hjacobs committed Apr 4, 2019
1 parent c7dea9d commit 35d2dc9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion kube_downscaler/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def autoscale_resource(resource: pykube.objects.NamespacedAPIObject,
is_uptime = helper.matches_time_spec(now, uptime) and not helper.matches_time_spec(now, downtime)

original_replicas = resource.annotations.get(ORIGINAL_REPLICAS_ANNOTATION)
downtime_replicas = resource.annotations.get(DOWNTIME_REPLICAS_ANNOTATION, downtime_replicas)
logger.debug('%s %s/%s has %s replicas (original: %s, uptime: %s)',
resource.kind, resource.namespace, resource.name, replicas, original_replicas, uptime)
update_needed = False
if is_uptime and replicas == 0 and original_replicas and int(original_replicas) > 0:

if is_uptime and replicas == downtime_replicas and original_replicas and int(original_replicas) > 0:
logger.info('Scaling up %s %s/%s from %s to %s replicas (uptime: %s, downtime: %s)',
resource.kind, resource.namespace, resource.name, replicas, original_replicas,
uptime, downtime)
Expand Down
48 changes: 45 additions & 3 deletions tests/test_scaler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from unittest.mock import MagicMock

from kube_downscaler.scaler import scale, ORIGINAL_REPLICAS_ANNOTATION
from kube_downscaler.scaler import scale, ORIGINAL_REPLICAS_ANNOTATION, DOWNTIME_REPLICAS_ANNOTATION


def test_scaler_always_up(monkeypatch):
Expand Down Expand Up @@ -122,7 +122,7 @@ def get(url, version, **kwargs):
{
'metadata': {
'name': 'deploy-1', 'namespace': 'default', 'creationTimestamp': '2019-03-01T16:38:00Z',
'annotations': {'downscaler/downtime-replicas': SCALE_TO},
'annotations': {DOWNTIME_REPLICAS_ANNOTATION: SCALE_TO},
}, 'spec': {'replicas': 5}
},
]}
Expand All @@ -143,4 +143,46 @@ def get(url, version, **kwargs):

assert api.patch.call_count == 1
assert api.patch.call_args[1]['url'] == 'deployments/deploy-1'
assert json.loads(api.patch.call_args[1]['data'])["spec"]["replicas"] == SCALE_TO
assert json.loads(api.patch.call_args[1]['data'])['spec']['replicas'] == SCALE_TO


def test_scaler_down_to_upscale(monkeypatch):
api = MagicMock()
monkeypatch.setattr('kube_downscaler.scaler.helper.get_kube_api', MagicMock(return_value=api))
SCALE_TO = 1
ORIGINAL = 3

def get(url, version, **kwargs):
if url == 'pods':
data = {'items': []}
elif url == 'deployments':
data = {'items': [
{
'metadata': {
'name': 'deploy-1', 'namespace': 'default', 'creationTimestamp': '2019-03-01T16:38:00Z',
'annotations': {
DOWNTIME_REPLICAS_ANNOTATION: SCALE_TO,
ORIGINAL_REPLICAS_ANNOTATION: ORIGINAL,
},
}, 'spec': {'replicas': SCALE_TO}
},
]}
elif url == 'namespaces/default':
data = {'metadata': {}}
else:
raise Exception(f'unexpected call: {url}, {version}, {kwargs}')

response = MagicMock()
response.json.return_value = data
return response

api.get = get

kinds = frozenset(['deployment'])
scale(namespace=None, default_uptime='always', default_downtime='never', kinds=kinds,
exclude_namespaces=[], exclude_deployments=[], exclude_statefulsets=[], dry_run=False, grace_period=300, downtime_replicas=0)

assert api.patch.call_count == 1
assert api.patch.call_args[1]['url'] == 'deployments/deploy-1'
assert json.loads(api.patch.call_args[1]['data'])['spec']['replicas'] == ORIGINAL
assert not json.loads(api.patch.call_args[1]['data'])['metadata']['annotations'][ORIGINAL_REPLICAS_ANNOTATION]

0 comments on commit 35d2dc9

Please sign in to comment.