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

Commit

Permalink
Ignore invalid annotation value for downscaler/exclude-until (#97)
Browse files Browse the repository at this point in the history
* #94 ignore invalid timestamp in exclude-until annotation

* #94 test the warning message
  • Loading branch information
hjacobs authored Apr 6, 2020
1 parent 0823c04 commit ec8b3b7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion kube_downscaler/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ def ignore_resource(

exclude_until = resource.annotations.get(EXCLUDE_UNTIL_ANNOTATION)
if exclude_until:
until_ts = parse_time(exclude_until)
try:
until_ts = parse_time(exclude_until)
except ValueError as e:
logger.warning(
f"Invalid annotation value for '{EXCLUDE_UNTIL_ANNOTATION}' on {resource.namespace}/{resource.name}: {e}"
)
# we will ignore the invalid timestamp and treat the resource as not excluded
return False
if now < until_ts:
return True

Expand Down
29 changes: 29 additions & 0 deletions tests/test_autoscale_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from kube_downscaler.scaler import DOWNSCALE_PERIOD_ANNOTATION
from kube_downscaler.scaler import DOWNTIME_REPLICAS_ANNOTATION
from kube_downscaler.scaler import EXCLUDE_ANNOTATION
from kube_downscaler.scaler import EXCLUDE_UNTIL_ANNOTATION
from kube_downscaler.scaler import ORIGINAL_REPLICAS_ANNOTATION
from kube_downscaler.scaler import UPSCALE_PERIOD_ANNOTATION

Expand Down Expand Up @@ -60,6 +61,34 @@ def test_exclude(resource):
assert ORIGINAL_REPLICAS_ANNOTATION not in resource.annotations


def test_exclude_until_invalid_time(resource, caplog):
caplog.set_level(logging.WARNING)
resource.annotations = {EXCLUDE_UNTIL_ANNOTATION: "some-invalid-timestamp"}
resource.replicas = 1
now = datetime.strptime("2018-10-23T21:56:00Z", "%Y-%m-%dT%H:%M:%SZ").replace(
tzinfo=timezone.utc
)
resource.metadata = {"creationTimestamp": "2018-10-23T21:55:00Z"}
autoscale_resource(
resource,
"never",
"never",
"never",
"always",
forced_uptime=False,
dry_run=True,
now=now,
)
assert resource.replicas == 0
assert resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] == "1"
# dry run will update the object properties, but won't call the Kubernetes API (update)
resource.update.assert_not_called()

# check that the warning was logged
msg = "Invalid annotation value for 'downscaler/exclude-until' on mock/res-1: time data 'some-invalid-timestamp' does not match any format (%Y-%m-%dT%H:%M:%SZ, %Y-%m-%dT%H:%M, %Y-%m-%d %H:%M, %Y-%m-%d)"
assert caplog.record_tuples == [("kube_downscaler.scaler", logging.WARNING, msg)]


def test_dry_run(resource):
resource.annotations = {}
resource.replicas = 1
Expand Down

0 comments on commit ec8b3b7

Please sign in to comment.