From 10273f2ada6306b756c1afa01d1bb20c3457ef63 Mon Sep 17 00:00:00 2001 From: Henning Jacobs Date: Sun, 3 Mar 2019 17:53:02 +0100 Subject: [PATCH] #7 add HTTP timeout (default: 10s) --- pykube/http.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pykube/http.py b/pykube/http.py index 7acbb7f..bd14aaf 100644 --- a/pykube/http.py +++ b/pykube/http.py @@ -5,7 +5,6 @@ import datetime import json import posixpath -import re import shlex import subprocess @@ -23,10 +22,11 @@ from .exceptions import HTTPError from .utils import jsonpath_installed, jsonpath_parse +from .config import KubeConfig from . import __version__ -_ipv4_re = re.compile(r"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$") +DEFAULT_HTTP_TIMEOUT = 10 # seconds class KubernetesHTTPAdapter(requests.adapters.HTTPAdapter): @@ -35,7 +35,7 @@ class KubernetesHTTPAdapter(requests.adapters.HTTPAdapter): # it can be overwritten in unit tests to mock the actual HTTP calls _do_send = requests.adapters.HTTPAdapter.send - def __init__(self, kube_config, **kwargs): + def __init__(self, kube_config: KubeConfig, **kwargs): self.kube_config = kube_config super().__init__(**kwargs) @@ -150,7 +150,7 @@ class HTTPClient(object): Client for interfacing with the Kubernetes API. """ - def __init__(self, config): + def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT): """ Creates a new instance of the HTTPClient. @@ -158,6 +158,7 @@ def __init__(self, config): - `config`: The configuration instance """ self.config = config + self.timeout = timeout self.url = self.config.cluster["server"] session = requests.Session() @@ -193,7 +194,7 @@ def resource_list(self, api_version): setattr(self, cached_attr, r.json()) return getattr(self, cached_attr) - def get_kwargs(self, **kwargs): + def get_kwargs(self, **kwargs) -> dict: """ Creates a full URL to request based on arguments. @@ -228,6 +229,9 @@ def get_kwargs(self, **kwargs): url = url[1:] bits.append(url) kwargs["url"] = self.url + posixpath.join(*bits) + if 'timeout' not in kwargs: + # apply default HTTP timeout + kwargs['timeout'] = self.timeout return kwargs def raise_for_status(self, resp):