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

Commit

Permalink
#21 fix setting original-replicas annotation (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs authored Mar 15, 2019
1 parent 3d6f562 commit 73c2673
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 69 deletions.
122 changes: 56 additions & 66 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/test_autoscale_resource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import pykube
import pytest
import logging

Expand Down Expand Up @@ -147,3 +149,17 @@ def test_downtime_replicas_valid(resource):
autoscale_resource(resource, 'never', 'always', False, False, now, 0, 1)
assert resource.replicas == 1
resource.update.assert_called_once()


def test_set_annotation():
api = MagicMock()
api.config.namespace = 'myns'
resource = pykube.StatefulSet(api, {'metadata': {'name': 'foo', 'creationTimestamp': '2019-03-15T21:55:00Z'}, 'spec': {}})
resource.replicas = 1
now = datetime.strptime('2019-03-15T21:56:00Z', '%Y-%m-%dT%H:%M:%SZ')
autoscale_resource(resource, 'never', 'always', False, False, now, 0, 0)
api.patch.assert_called_once()
patch_data = json.loads(api.patch.call_args[1]['data'])
# ensure the original replicas annotation is send to the server
assert patch_data == {"metadata": {"name": "foo", "creationTimestamp": "2019-03-15T21:55:00Z",
'annotations': {ORIGINAL_REPLICAS_ANNOTATION: '1'}}, "spec": {"replicas": 0}}
8 changes: 5 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
from kube_downscaler.scaler import scale, ORIGINAL_REPLICAS_ANNOTATION


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

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

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

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

Expand Down

0 comments on commit 73c2673

Please sign in to comment.