-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathclient_go_retry_patch.diff
More file actions
132 lines (132 loc) · 4.78 KB
/
Copy pathclient_go_retry_patch.diff
File metadata and controls
132 lines (132 loc) · 4.78 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
diff --git a/kubernetes/client/configuration.py b/kubernetes/client/configuration.py
index 5e150b408..3ef976444 100644
--- a/kubernetes/client/configuration.py
+++ b/kubernetes/client/configuration.py
@@ -359,0 +360,16 @@ conf = client.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.
+ """
+
diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py
index 9fca6dca3..a5fb08c7a 100644
--- a/kubernetes/client/rest.py
+++ b/kubernetes/client/rest.py
@@ -22,0 +23 @@ import urllib3
+from urllib3.util.retry import Retry
@@ -23,0 +25,5 @@ import urllib3
+from kubernetes.utils.retry import (
+ is_retry_after_response,
+ on_retry_after_error,
+ retry_after_backoff,
+)
@@ -107,0 +114,2 @@ class RESTClientObject:
+ self.configuration = configuration
+
@@ -219,0 +228,26 @@ class RESTClientObject:
+ 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
+
@@ -313,8 +347,10 @@ class RESTClientObject:
- 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()
@@ -325,0 +362,49 @@ class RESTClientObject:
+
+ @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