-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathprobes.py
146 lines (130 loc) · 4.23 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
import logging
from functools import partial
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__ = ["statefulset_fully_available", "statefulset_not_fully_available"]
logger = logging.getLogger("chaostoolkit")
def _statefulset_readiness_has_state(
name: str,
ready: bool,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
secrets: Secrets = None,
):
"""
Check wether if the given statefulSet 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_stateful_set,
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_stateful_set,
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():
statefulset = event["object"]
status = statefulset.status
spec = statefulset.spec
logger.debug(
f"StatefulSet '{statefulset.metadata.name}' {event['type']}: "
f"Current Revision: {status.current_revision} - "
f"Ready Replicas {status.ready_replicas} - "
f"Current Replicas {status.current_replicas} - "
f"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 statefulset_not_fully_available(
name: str,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
raise_on_fully_available: bool = True,
secrets: Secrets = None,
):
"""
Wait until the statefulSet 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 _statefulset_readiness_has_state(
name,
False,
ns,
label_selector,
timeout,
secrets,
):
return True
else:
m = f"microservice '{name}' failed to stop running within {timeout}s"
if not raise_on_fully_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)
def statefulset_fully_available(
name: str,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
raise_on_not_fully_available: bool = True,
secrets: Secrets = None,
):
"""
Wait until all the statefulSet 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 _statefulset_readiness_has_state(
name,
True,
ns,
label_selector,
timeout,
secrets,
):
return True
else:
m = f"microservice '{name}' failed to recover within {timeout}s"
if not raise_on_not_fully_available:
logger.debug(m)
return False
else:
raise ActivityFailed(m)