Skip to content

Commit 48e45b9

Browse files
author
Alan Christie
committed
feat: Attempt to add request timeout to API calls
1 parent d463ffb commit 48e45b9

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88

99
# Conventional Commit message checker (commitizen)
1010
- repo: https://github.com/commitizen-tools/commitizen
11-
rev: v3.30.1
11+
rev: v3.31.0
1212
hooks:
1313
- id: commitizen
1414
stages:
@@ -32,21 +32,21 @@ repos:
3232
- --markdown-linebreak-ext=md
3333
# Black (uncompromising) Python code formatter
3434
- repo: https://github.com/psf/black
35-
rev: 24.10.0
35+
rev: 25.1.0
3636
hooks:
3737
- id: black
3838
args:
3939
- --target-version
4040
- py312
4141
# MyPy
4242
- repo: https://github.com/pre-commit/mirrors-mypy
43-
rev: v1.13.0
43+
rev: v1.15.0
4444
hooks:
4545
- id: mypy
4646
files: ^operator
4747
# Pylint
4848
- repo: https://github.com/pycqa/pylint
49-
rev: v3.3.1
49+
rev: v3.3.6
5050
hooks:
5151
- id: pylint
5252
files: ^operator
@@ -57,7 +57,7 @@ repos:
5757
# All files that end '.yaml' or '.yaml.j2'
5858
# See https://pre-commit.com/#filtering-files-with-types
5959
- repo: https://github.com/adrienverge/yamllint
60-
rev: v1.35.1
60+
rev: v1.37.0
6161
hooks:
6262
- id: yamllint
6363
types:

operator/handlers.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""A kopf handler for the Jupyter CRD.
2-
"""
1+
"""A kopf handler for the Jupyter CRD."""
32

43
import logging
54
import random
@@ -10,6 +9,14 @@
109
import kubernetes
1110
import kopf
1211

12+
# Configuration of underlying API requests.
13+
#
14+
# Request timeout (from Python Kubernetes API)
15+
# If one number provided, it will be total request
16+
# timeout. It can also be a pair (tuple) of
17+
# (connection, read) timeouts.
18+
_REQUEST_TIMEOUT = (30, 20)
19+
1320
# Some (key) default deployment variables...
1421
_DEFAULT_IMAGE: str = "jupyter/minimal-notebook:notebook-6.3.0"
1522
_DEFAULT_SA: str = "default"
@@ -246,7 +253,9 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
246253
#
247254
# Added as a work-wround for sc-
248255
try:
249-
core_api.create_namespaced_config_map(namespace, bp_cm_body)
256+
core_api.create_namespaced_config_map(
257+
namespace, bp_cm_body, _request_timeout=_REQUEST_TIMEOUT
258+
)
250259
except kubernetes.client.exceptions.ApiException as ex:
251260
if ex.status != 409 or ex.reason != "Conflict":
252261
raise ex
@@ -255,8 +264,12 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
255264
"Got ApiException [409/Conflict] creating BP ConfigMap. Ignoring [#10]"
256265
)
257266
return create_response
258-
core_api.create_namespaced_config_map(namespace, startup_cm_body)
259-
core_api.create_namespaced_config_map(namespace, config_cm_body)
267+
core_api.create_namespaced_config_map(
268+
namespace, startup_cm_body, _request_timeout=_REQUEST_TIMEOUT
269+
)
270+
core_api.create_namespaced_config_map(
271+
namespace, config_cm_body, _request_timeout=_REQUEST_TIMEOUT
272+
)
260273

261274
logging.info("Created ConfigMaps")
262275

@@ -372,7 +385,9 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
372385

373386
kopf.adopt(deployment_body)
374387
apps_api = kubernetes.client.AppsV1Api()
375-
apps_api.create_namespaced_deployment(namespace, deployment_body)
388+
apps_api.create_namespaced_deployment(
389+
namespace, deployment_body, _request_timeout=_REQUEST_TIMEOUT
390+
)
376391

377392
logging.info("Created deployment")
378393

@@ -400,7 +415,9 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
400415
}
401416

402417
kopf.adopt(service_body)
403-
core_api.create_namespaced_service(namespace, service_body)
418+
core_api.create_namespaced_service(
419+
namespace, service_body, _request_timeout=_REQUEST_TIMEOUT
420+
)
404421

405422
logging.info("Created service")
406423

@@ -449,7 +466,9 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
449466

450467
kopf.adopt(ingress_body)
451468
ext_api = kubernetes.client.NetworkingV1Api()
452-
ext_api.create_namespaced_ingress(namespace, ingress_body)
469+
ext_api.create_namespaced_ingress(
470+
namespace, ingress_body, _request_timeout=_REQUEST_TIMEOUT
471+
)
453472

454473
logging.info("Created ingress")
455474

0 commit comments

Comments
 (0)