Repository navigation
Expand file tree
/
Copy pathclient_go_retry_patch.diff
More file actions
161 lines (154 loc) · 5.82 KB
/
Copy pathclient_go_retry_patch.diff
File metadata and controls
161 lines (154 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
diff --git a/kubernetes/client/configuration.py b/kubernetes/client/configuration.py
--- a/kubernetes/client/configuration.py
+++ b/kubernetes/client/configuration.py
@@ -365,6 +365,22 @@
self.retries = retries
"""Retry configuration
"""
+ self.client_go_retries = False
+ """Enable Kubernetes client-go-compatible retry semantics.
+
+ When enabled, GET and HEAD requests retry Retry-After responses.
+ The retry ceiling is read from ``retries`` when set; otherwise it
+ follows the client-go default of at most 10 retries.
+ """
+ self.client_go_retry_backoff = None
+ """Backoff for Kubernetes client-go-compatible retries.
+
+ If unset, client-go-compatible GET and HEAD retries use the
+ client-go default retry ceiling with no additional client-side
+ delay beyond Retry-After. When set, ``retries`` still overrides
+ the retry ceiling if it is not None.
+ """
+
# Enable client side validation
self.client_side_validation = client_side_validation
diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py
--- a/kubernetes/client/rest.py
+++ b/kubernetes/client/rest.py
@@ -20,7 +20,13 @@
from urllib.parse import urlparse
import urllib3
+from urllib3.util.retry import Retry
+from kubernetes.utils.retry import (
+ is_retry_after_response,
+ on_retry_after_error,
+ retry_after_backoff,
+)
from kubernetes.client.exceptions import ApiException, ApiValueError
SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
@@ -120,6 +126,8 @@
class RESTClientObject:
def __init__(self, configuration) -> None:
+ self.configuration = configuration
+
# urllib3.PoolManager will pass all kw parameters to connectionpool
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501
@@ -234,6 +242,32 @@
read=_request_timeout[1]
)
+ client_go_read_retries = (
+ method in ['GET', 'HEAD']
+ and getattr(self.configuration, 'client_go_retries', False)
+ )
+ read_retries = None
+ if client_go_read_retries:
+ read_retries = self._urllib3_retries_without_status(
+ getattr(self.configuration, 'retries', None))
+
+ def read_request(check_retry_status=False):
+ kwargs = {}
+ if read_retries is not None:
+ kwargs['retries'] = read_retries
+ response = self.pool_manager.request(
+ method,
+ url,
+ fields={},
+ timeout=timeout,
+ headers=headers,
+ preload_content=False,
+ **kwargs
+ )
+ if check_retry_status:
+ self._raise_retry_after_response(response)
+ return response
+
try:
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
@@ -331,16 +365,67 @@
raise ApiException(status=0, reason=msg)
# For `GET`, `HEAD`
else:
- r = self.pool_manager.request(
- method,
- url,
- fields={},
- timeout=timeout,
- headers=headers,
- preload_content=False
- )
+ if client_go_read_retries:
+ backoff = retry_after_backoff(
+ getattr(self.configuration, 'retries', None),
+ getattr(self.configuration, 'client_go_retry_backoff', None),
+ )
+ r = on_retry_after_error(
+ backoff, self._is_read_retryable,
+ lambda: read_request(True))
+ else:
+ r = read_request()
except urllib3.exceptions.SSLError as e:
msg = "\n".join([type(e).__name__, str(e)])
raise ApiException(status=0, reason=msg)
return RESTResponse(r)
+
+ @classmethod
+ def _is_read_retryable(cls, error):
+ return is_retry_after_response(error)
+
+ @staticmethod
+ def _retry_after_error(response):
+ error = ApiException(status=response.status, reason=response.reason)
+ error.headers = response.getheaders()
+ return error
+
+ @classmethod
+ def _raise_retry_after_response(cls, response):
+ error = cls._retry_after_error(response)
+ if not is_retry_after_response(error):
+ return
+ cls._read_and_close_retry_response(response)
+ raise error
+
+ @staticmethod
+ def _read_and_close_retry_response(response):
+ try:
+ try:
+ content_length = int(response.getheaders().get(
+ 'Content-Length', '-1'))
+ except (TypeError, ValueError):
+ content_length = -1
+ if content_length <= 2 << 10:
+ response.read(2 << 10)
+ finally:
+ response.close()
+
+ @staticmethod
+ def _urllib3_retries_without_status(retries):
+ if retries is False:
+ return False
+ if retries is None:
+ retries = Retry.DEFAULT
+ elif retries is True:
+ retries = Retry.DEFAULT
+ elif isinstance(retries, int):
+ retries = Retry.from_int(retries)
+ if isinstance(retries, Retry):
+ return retries.new(
+ status=0,
+ status_forcelist=(),
+ respect_retry_after_header=False,
+ )
+ return retries