Skip to content

Commit d95f10f

Browse files
authored
Merge pull request #117 from samuel-esp/timeout-api-fix
Fix: Api Server Timeout Argument Doesn't Override Default Timeout Value
2 parents 72ad3fb + a471ced commit d95f10f

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

kube_downscaler/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def _matches_absolute_time_spec(time: datetime.datetime, match: Match):
6565
return time_from <= time <= time_to
6666

6767

68-
def get_kube_api():
68+
def get_kube_api(timeout: int):
6969
config = pykube.KubeConfig.from_env()
70-
api = pykube.HTTPClient(config)
70+
api = pykube.HTTPClient(config, timeout=timeout)
7171
return api
7272

7373

kube_downscaler/scaler.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,7 @@ def apply_kubedownscalerjobsconstraint_crd(excluded_names, matching_labels, api)
11421142
time.sleep(0.02)
11431143

11441144

1145-
def gatekeeper_constraint_template_crd_exist() -> bool:
1146-
api = helper.get_kube_api()
1145+
def gatekeeper_constraint_template_crd_exist(api) -> bool:
11471146
constraint_template_crd = CustomResourceDefinition.objects(api).get_or_none(
11481147
name="constrainttemplates.templates.gatekeeper.sh")
11491148

@@ -1227,7 +1226,7 @@ def autoscale_jobs(
12271226
):
12281227
if admission_controller != "" and admission_controller in ADMISSION_CONTROLLERS:
12291228

1230-
if admission_controller == "gatekeeper" and gatekeeper_constraint_template_crd_exist():
1229+
if admission_controller == "gatekeeper" and gatekeeper_constraint_template_crd_exist(api):
12311230
apply_kubedownscalerjobsconstraint_crd(exclude_names, matching_labels, api)
12321231
if admission_controller == "gatekeeper" and not gatekeeper_healthy(api):
12331232
logging.error("unable to scale jobs, there was a problem applying kubedownscalerjobsconstraint crd or it was deleted"
@@ -1357,11 +1356,10 @@ def scale(
13571356
enable_events: bool = False,
13581357
matching_labels: FrozenSet[Pattern] = frozenset(),
13591358
):
1360-
api = helper.get_kube_api()
1359+
api = helper.get_kube_api(api_server_timeout)
13611360

13621361
now = datetime.datetime.now(datetime.timezone.utc)
13631362
forced_uptime = pods_force_uptime(api, namespaces)
1364-
pykube.http.DEFAULT_HTTP_TIMEOUT=api_server_timeout
13651363

13661364
for clazz in RESOURCE_CLASSES:
13671365
plural = clazz.endpoint

tests/test_scaler.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,41 @@
1212
from kube_downscaler.scaler import scale_down_jobs
1313
from kube_downscaler.scaler import scale_up_jobs
1414

15+
def test_scale_custom_timeout(monkeypatch):
16+
api_server_timeout = 15 # Defined by the user
17+
api = MagicMock()
18+
api.timeout = 15 # Expected timeout
19+
20+
mock_get_kube_api = MagicMock(return_value=api)
21+
monkeypatch.setattr(
22+
"kube_downscaler.scaler.helper.get_kube_api", mock_get_kube_api
23+
)
24+
25+
scale(
26+
namespaces=frozenset({"default"}),
27+
upscale_period="never",
28+
downscale_period="never",
29+
default_uptime="never",
30+
default_downtime="always",
31+
upscale_target_only=False,
32+
include_resources=frozenset(["pods"]),
33+
exclude_namespaces=frozenset(),
34+
exclude_deployments=frozenset(),
35+
dry_run=False,
36+
grace_period=300,
37+
admission_controller="",
38+
constrained_downscaler=False,
39+
api_server_timeout=api_server_timeout,
40+
downtime_replicas=0,
41+
deployment_time_annotation=None,
42+
enable_events=False,
43+
matching_labels=frozenset(),
44+
)
45+
46+
# ensure get_kube_api is called with the correct timeout value
47+
mock_get_kube_api.assert_called_once_with(api_server_timeout)
48+
# ensure timeout value is correctly set on the returned object
49+
assert api.timeout == api_server_timeout
1550

1651
def test_scaler_always_up(monkeypatch):
1752
api = MagicMock()

0 commit comments

Comments
 (0)