Skip to content

Commit

Permalink
make final adjustments so everything passes
Browse files Browse the repository at this point in the history
  • Loading branch information
JTaeuber committed Nov 29, 2024
1 parent 4e5459e commit 108bc01
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 54 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ such as an HPA, CI/CD pipeline, or manual intervention. If enabled, Kube Downsca
retry the update immediately, without waiting for the next iteration (default: 0). This
argument is strongly recommended when using the `--once` argument to process large clusters
### Constrained Mode (Limited Access Mode)
The Constrained Mode (also known as Limited Access Mode) is designed for users who do not have full cluster access.
Expand Down
15 changes: 12 additions & 3 deletions kube_downscaler/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ def get_resources(kind, api, namespaces: FrozenSet[str], excluded_namespaces):

def get_resource(kind, api, namespace, resource_name: str):
try:
resource = kind.objects(api).filter(namespace=namespace).get_or_none(name=resource_name)
resource = (
kind.objects(api)
.filter(namespace=namespace)
.get_or_none(name=resource_name)
)
if resource is None:
logger.debug(f"{kind.endpoint} {namespace}/{resource_name} not found")
except requests.HTTPError as e:
Expand All @@ -331,7 +335,9 @@ def get_resource(kind, api, namespace, resource_name: str):
return resource


def scale_jobs_without_admission_controller(plural, admission_controller, constrainted_downscaler):
def scale_jobs_without_admission_controller(
plural, admission_controller, constrainted_downscaler
):
return (plural == "jobs" and admission_controller == "") or constrainted_downscaler


Expand Down Expand Up @@ -1074,7 +1080,10 @@ def autoscale_resource(
else:
resource.update()
except Exception as e:
if isinstance(e, HTTPError) and "the object has been modified" in str(e).lower():
if (
isinstance(e, HTTPError)
and "the object has been modified" in str(e).lower()
):
logger.warning(
f"Unable to process {resource.kind} {resource.namespace}/{resource.name} because it was recently modified"
)
Expand Down
23 changes: 9 additions & 14 deletions tests/test_autoscale_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from datetime import datetime
from datetime import timezone
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock

import pykube
import pytest
Expand Down Expand Up @@ -33,6 +33,7 @@ def resource():
res.annotations = {}
return res


def test_swallow_exception(monkeypatch, resource, caplog):
api = MagicMock()
monkeypatch.setattr(
Expand Down Expand Up @@ -1634,16 +1635,14 @@ def test_downscale_resource_concurrently_modified(monkeypatch):
"namespace": "default",
"creationTimestamp": "2018-10-23T21:55:00Z",
},
"spec": {
"template": {
"spec": {}
}
}
}
"spec": {"template": {"spec": {}}},
},
)

# Replace update method to track calls
ds.update = MagicMock(side_effect=[http_error, None]) # Simulate conflict and success
ds.update = MagicMock(
side_effect=[http_error, None]
) # Simulate conflict and success

# Mock get_resource with MagicMock
mock_get_resource = MagicMock(return_value=ds)
Expand Down Expand Up @@ -1700,12 +1699,8 @@ def test_downscale_resource_concurrently_modified_without_retries_allowed(monkey
"namespace": "default",
"creationTimestamp": "2018-10-23T21:55:00Z",
},
"spec": {
"template": {
"spec": {}
}
}
}
"spec": {"template": {"spec": {}}},
},
)

# Mock get_resource with MagicMock
Expand Down
36 changes: 0 additions & 36 deletions tests/test_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,6 @@
from kube_downscaler.scaler import scale_down_jobs
from kube_downscaler.scaler import scale_up_jobs

def test_scale_custom_timeout(monkeypatch):
api_server_timeout = 15 # Defined by the user
api = MagicMock()
api.timeout = 15 # Expected timeout

mock_get_kube_api = MagicMock(return_value=api)
monkeypatch.setattr(
"kube_downscaler.scaler.helper.get_kube_api", mock_get_kube_api
)

scale(
namespaces=frozenset({"default"}),
upscale_period="never",
downscale_period="never",
default_uptime="never",
default_downtime="always",
upscale_target_only=False,
include_resources=frozenset(["pods"]),
exclude_namespaces=frozenset(),
exclude_deployments=frozenset(),
dry_run=False,
grace_period=300,
admission_controller="",
constrained_downscaler=False,
api_server_timeout=api_server_timeout,
max_retries_on_conflict=0,
downtime_replicas=0,
deployment_time_annotation=None,
enable_events=False,
matching_labels=frozenset(),
)

# ensure get_kube_api is called with the correct timeout value
mock_get_kube_api.assert_called_once_with(api_server_timeout)
# ensure timeout value is correctly set on the returned object
assert api.timeout == api_server_timeout

def test_scale_custom_timeout(monkeypatch):
api_server_timeout = 15 # Defined by the user
Expand Down

0 comments on commit 108bc01

Please sign in to comment.