-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathprobes.py
261 lines (226 loc) · 7.62 KB
/
probes.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import logging
from functools import partial
from typing import Union
import urllib3
from chaoslib.exceptions import ActivityFailed
from chaoslib.types import Secrets
from kubernetes import client, watch
from chaosk8s import create_k8s_api_client
__all__ = [
"deployment_available_and_healthy",
"deployment_not_fully_available",
"deployment_fully_available",
"deployment_partially_available",
]
logger = logging.getLogger("chaostoolkit")
def deployment_available_and_healthy(
name: str,
ns: str = "default",
label_selector: str = None,
raise_on_unavailable: bool = True,
secrets: Secrets = None,
) -> Union[bool, None]:
"""
Lookup a deployment by `name` in the namespace `ns`.
The selected resources are matched by the given `label_selector`.
Raises :exc:`chaoslib.exceptions.ActivityFailed` when the state is not
as expected. Unless `raise_on_unavailable` is set to `False` which means
the probe will return `False` rather than raise the exception.
"""
field_selector = f"metadata.name={name}"
api = create_k8s_api_client(secrets)
v1 = client.AppsV1Api(api)
if label_selector:
ret = v1.list_namespaced_deployment(
ns, field_selector=field_selector, label_selector=label_selector
)
else:
ret = v1.list_namespaced_deployment(ns, field_selector=field_selector)
logger.debug(
f"Found {len(ret.items)} deployment(s) named '{name}' in ns '{ns}'"
)
if not ret.items:
m = f"Deployment '{name}' was not found"
if not raise_on_unavailable:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
for d in ret.items:
logger.debug(
f"Deployment has '{d.status.available_replicas}' available replicas"
)
if d.status.available_replicas != d.spec.replicas:
m = f"Deployment '{name}' is not healthy"
if not raise_on_unavailable:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
return True
def deployment_partially_available(
name: str,
ns: str = "default",
label_selector: str = None,
raise_on_not_partially_available: bool = True,
secrets: Secrets = None,
) -> Union[bool, None]:
"""
Check whether if the given deployment state is ready or at-least partially
ready.
Raises :exc:`chaoslib.exceptions.ActivityFailed` when the state is not
as expected. Unless `raise_on_not_partially_available` is set to `False`
which means the probe will return `False` rather than raise the exception.
"""
field_selector = f"metadata.name={name}"
api = create_k8s_api_client(secrets)
v1 = client.AppsV1Api(api)
if label_selector:
ret = v1.list_namespaced_deployment(
ns, field_selector=field_selector, label_selector=label_selector
)
else:
ret = v1.list_namespaced_deployment(ns, field_selector=field_selector)
logger.debug(
f"Found {len(ret.items)} deployment(s) named '{name}' in ns '{ns}'"
)
if not ret.items:
m = f"Deployment '{name}' was not found"
if not raise_on_not_partially_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
for d in ret.items:
logger.debug(
f"Deployment has '{d.status.available_replicas}' available replicas"
)
if d.status.available_replicas >= 1:
return True
else:
m = f"Deployment '{name}' is not healthy"
if not raise_on_not_partially_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
def _deployment_readiness_has_state(
name: str,
ready: bool,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
secrets: Secrets = None,
) -> Union[bool, None]:
"""
Check wether if the given deployment state is ready or not
according to the ready paramter.
If the state is not reached after `timeout` seconds, a
:exc:`chaoslib.exceptions.ActivityFailed` exception is raised.
"""
field_selector = f"metadata.name={name}"
api = create_k8s_api_client(secrets)
v1 = client.AppsV1Api(api)
w = watch.Watch()
timeout = int(timeout)
if label_selector is None:
watch_events = partial(
w.stream,
v1.list_namespaced_deployment,
namespace=ns,
field_selector=field_selector,
_request_timeout=timeout,
)
else:
label_selector = label_selector.format(name=name)
watch_events = partial(
w.stream,
v1.list_namespaced_deployment,
namespace=ns,
field_selector=field_selector,
label_selector=label_selector,
_request_timeout=timeout,
)
try:
logger.debug(f"Watching events for {timeout}s")
for event in watch_events():
deployment = event["object"]
status = deployment.status
spec = deployment.spec
logger.debug(
f"Deployment '{deployment.metadata.name}' {event['type']}: "
f"Ready Replicas {status.ready_replicas} - "
f"Unavailable Replicas {status.unavailable_replicas} - "
f"Desired Replicas {spec.replicas}"
)
readiness = status.ready_replicas == spec.replicas
if ready == readiness:
w.stop()
return True
except urllib3.exceptions.ReadTimeoutError:
logger.debug("Timed out!")
return False
def deployment_not_fully_available(
name: str,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
raise_on_fully_available: bool = True,
secrets: Secrets = None,
) -> Union[bool, None]:
"""
Wait until the deployment gets into an intermediate state where not all
expected replicas are available. Once this state is reached, return `True`.
If the state is not reached after `timeout` seconds, a
:exc:`chaoslib.exceptions.ActivityFailed` exception is raised.
If `raise_on_fully_available` is set to `False`, return `False` instead
of raising the exception.
"""
if _deployment_readiness_has_state(
name,
False,
ns,
label_selector,
timeout,
secrets,
):
return True
else:
m = f"deployment '{name}' failed to stop running within {timeout}s"
if not raise_on_fully_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
def deployment_fully_available(
name: str,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
raise_on_not_fully_available: bool = True,
secrets: Secrets = None,
) -> Union[bool, None]:
"""
Wait until all the deployment expected replicas are available.
Once this state is reached, return `True`.
If the state is not reached after `timeout` seconds, a
:exc:`chaoslib.exceptions.ActivityFailed` exception is raised.
If `raise_on_not_fully_available` is set to `False`, return `False` instead
of raising the exception.
"""
if _deployment_readiness_has_state(
name,
True,
ns,
label_selector,
timeout,
secrets,
):
return True
else:
m = f"deployment '{name}' failed to recover within {timeout}s"
if not raise_on_not_fully_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)