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

Commit 73c2673

Browse files
authored
#21 fix setting original-replicas annotation (#42)
1 parent 3d6f562 commit 73c2673

File tree

3 files changed

+77
-69
lines changed

3 files changed

+77
-69
lines changed

Pipfile.lock

Lines changed: 56 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_autoscale_resource.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import pykube
13
import pytest
24
import logging
35

@@ -147,3 +149,17 @@ def test_downtime_replicas_valid(resource):
147149
autoscale_resource(resource, 'never', 'always', False, False, now, 0, 1)
148150
assert resource.replicas == 1
149151
resource.update.assert_called_once()
152+
153+
154+
def test_set_annotation():
155+
api = MagicMock()
156+
api.config.namespace = 'myns'
157+
resource = pykube.StatefulSet(api, {'metadata': {'name': 'foo', 'creationTimestamp': '2019-03-15T21:55:00Z'}, 'spec': {}})
158+
resource.replicas = 1
159+
now = datetime.strptime('2019-03-15T21:56:00Z', '%Y-%m-%dT%H:%M:%SZ')
160+
autoscale_resource(resource, 'never', 'always', False, False, now, 0, 0)
161+
api.patch.assert_called_once()
162+
patch_data = json.loads(api.patch.call_args[1]['data'])
163+
# ensure the original replicas annotation is send to the server
164+
assert patch_data == {"metadata": {"name": "foo", "creationTimestamp": "2019-03-15T21:55:00Z",
165+
'annotations': {ORIGINAL_REPLICAS_ANNOTATION: '1'}}, "spec": {"replicas": 0}}

tests/test_scaler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from unittest.mock import MagicMock
33

4-
from kube_downscaler.scaler import scale
4+
from kube_downscaler.scaler import scale, ORIGINAL_REPLICAS_ANNOTATION
55

66

77
def test_scaler_always_up(monkeypatch):
@@ -65,7 +65,8 @@ def get(url, version, **kwargs):
6565
assert api.patch.call_count == 1
6666

6767
# make sure that deploy-2 was updated (namespace of sysdep-1 was excluded)
68-
patch_data = {"metadata": {"name": "deploy-2", "namespace": "default", "creationTimestamp": "2019-03-01T16:38:00Z"}, "spec": {"replicas": 0}}
68+
patch_data = {"metadata": {"name": "deploy-2", "namespace": "default", "creationTimestamp": "2019-03-01T16:38:00Z",
69+
'annotations': {ORIGINAL_REPLICAS_ANNOTATION: '2'}}, "spec": {"replicas": 0}}
6970
assert api.patch.call_args[1]['url'] == 'deployments/deploy-2'
7071
assert json.loads(api.patch.call_args[1]['data']) == patch_data
7172

@@ -102,7 +103,8 @@ def get(url, version, **kwargs):
102103
assert api.patch.call_count == 1
103104

104105
# make sure that deploy-2 was updated (deploy-1 was excluded via annotation on ns-1)
105-
patch_data = {"metadata": {"name": "deploy-2", "namespace": "ns-2", "creationTimestamp": "2019-03-01T16:38:00Z"}, "spec": {"replicas": 0}}
106+
patch_data = {"metadata": {"name": "deploy-2", "namespace": "ns-2", "creationTimestamp": "2019-03-01T16:38:00Z",
107+
'annotations': {ORIGINAL_REPLICAS_ANNOTATION: '2'}}, "spec": {"replicas": 0}}
106108
assert api.patch.call_args[1]['url'] == 'deployments/deploy-2'
107109
assert json.loads(api.patch.call_args[1]['data']) == patch_data
108110

0 commit comments

Comments
 (0)