diff --git a/README.md b/README.md index 82548fe..602eae1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/kube_downscaler/scaler.py b/kube_downscaler/scaler.py index 883606b..26478ba 100644 --- a/kube_downscaler/scaler.py +++ b/kube_downscaler/scaler.py @@ -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: @@ -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 @@ -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" ) diff --git a/tests/test_autoscale_resource.py b/tests/test_autoscale_resource.py index bd9e63f..eb25822 100644 --- a/tests/test_autoscale_resource.py +++ b/tests/test_autoscale_resource.py @@ -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 @@ -33,6 +33,7 @@ def resource(): res.annotations = {} return res + def test_swallow_exception(monkeypatch, resource, caplog): api = MagicMock() monkeypatch.setattr( @@ -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) @@ -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 diff --git a/tests/test_scaler.py b/tests/test_scaler.py index e169633..f528aba 100644 --- a/tests/test_scaler.py +++ b/tests/test_scaler.py @@ -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