generated from arcalot/arcaflow-plugin-template-python
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharcaflow_plugin_kill_pod.py
346 lines (298 loc) · 10.8 KB
/
arcaflow_plugin_kill_pod.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
import random
import re
import sys
import time
import typing
from dataclasses import dataclass, field
from datetime import datetime
from traceback import format_exc
from arcaflow_plugin_sdk import plugin, validation
from kubernetes import client, config
from kubernetes.client import ApiException, V1DeleteOptions, V1Pod, V1PodList
def setup_kubernetes(kubeconfig_path):
if kubeconfig_path is None:
kubeconfig_path = config.KUBE_CONFIG_DEFAULT_LOCATION
kubeconfig = config.kube_config.KubeConfigMerger(kubeconfig_path)
if kubeconfig.config is None:
raise Exception(
"Invalid kube-config file: %s. " "No configuration found." % kubeconfig_path
)
loader = config.kube_config.KubeConfigLoader(
config_dict=kubeconfig.config,
)
client_config = client.Configuration()
loader.load_and_set(client_config)
return client.ApiClient(configuration=client_config)
def _find_pods(core_v1, label_selector, name_pattern, namespace_pattern):
pods: typing.List[V1Pod] = []
_continue = None
finished = False
while not finished:
pod_response: V1PodList = core_v1.list_pod_for_all_namespaces(
watch=False,
label_selector=label_selector,
field_selector="status.phase=Running",
_continue=_continue
)
for pod in pod_response.items:
pod: V1Pod
if (
name_pattern is None or name_pattern.match(pod.metadata.name)
) and namespace_pattern.match(pod.metadata.namespace):
pods.append(pod)
_continue = pod_response.metadata._continue
if _continue is None:
finished = True
return pods
@dataclass
class Pod:
namespace: str
name: str
creation_timestamp : str
@dataclass
class PodKillSuccessOutput:
pods: typing.Dict[int, Pod] = field(
metadata={
"name": "Pods removed",
"description": "Map between timestamps and the pods removed. The timestamp is provided in nanoseconds.",
}
)
@dataclass
class PodWaitSuccessOutput:
pods: typing.List[Pod] = field(
metadata={
"name": "Pods",
"description": "List of pods that have been found to run.",
}
)
@dataclass
class PodErrorOutput:
error: str
@dataclass
class KillPodConfig:
"""
This is a configuration structure specific to pod kill scenario. It describes which pod from which
namespace(s) to select for killing and how many pods to kill.
"""
namespace_pattern: re.Pattern = field(
metadata={
"name": "Namespace pattern",
"description": "Regular expression for target pod namespaces.",
}
)
name_pattern: typing.Annotated[
typing.Optional[re.Pattern], validation.required_if_not("label_selector")
] = field(
default=None,
metadata={
"name": "Name pattern",
"description": "Regular expression for target pods. Required if label_selector is not set.",
},
)
kill: typing.Annotated[int, validation.min(1)] = field(
default=1,
metadata={
"name": "Number of pods to kill",
"description": "How many pods should we attempt to kill?",
},
)
label_selector: typing.Annotated[
typing.Optional[str],
validation.required_if_not("name_pattern"),
] = field(
default=None,
metadata={
"name": "Label selector",
"description": "Kubernetes label selector for the target pods. Required if name_pattern is not set.\n"
"See https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ for details.",
},
)
kubeconfig_path: typing.Optional[str] = field(
default=None,
metadata={
"name": "Kubeconfig path",
"description": "Path to your Kubeconfig file. Defaults to ~/.kube/config.\n"
"See https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ for "
"details.",
},
)
timeout: int = field(
default=180,
metadata={
"name": "Timeout",
"description": "Timeout to wait for the target pod(s) to be removed in seconds.",
},
)
krkn_pod_recovery_time: int = field(
default=60,
metadata={
"name": "Recovery Time",
"description": "The Expected Recovery time fo the pod (used by Krkn to monitor the pod lifecycle)",
},
)
backoff: int = field(
default=1,
metadata={
"name": "Backoff",
"description": "How many seconds to wait between checks for the target pod status.",
},
)
@plugin.step(
"kill-pods",
"Kill pods",
"Kill pods as specified by parameters",
{"success": PodKillSuccessOutput, "error": PodErrorOutput},
)
def kill_pods(
cfg: KillPodConfig,
) -> typing.Tuple[str, typing.Union[PodKillSuccessOutput, PodErrorOutput]]:
try:
with setup_kubernetes(cfg.kubeconfig_path) as cli:
core_v1 = client.CoreV1Api(cli)
# region Select target pods
pods = _find_pods(
core_v1, cfg.label_selector, cfg.name_pattern, cfg.namespace_pattern
)
if len(pods) < cfg.kill:
return "error", PodErrorOutput(
"Not enough pods match the criteria, expected {} but found only {} pods".format(
cfg.kill, len(pods)
)
)
random.shuffle(pods)
# endregion
# region Remove pods
killed_pods: typing.Dict[int, Pod] = {}
watch_pods: typing.List[Pod] = []
for i in range(cfg.kill):
pod = pods[i]
kill_time=time.time_ns()
core_v1.delete_namespaced_pod(
pod.metadata.name,
pod.metadata.namespace,
body=V1DeleteOptions(
grace_period_seconds=0,
),
)
p = Pod(pod.metadata.namespace, pod.metadata.name, str(pod.metadata.creation_timestamp))
killed_pods[kill_time] = p
watch_pods.append(p)
# endregion
# region Wait for pods to be removed
start_time = time.time()
while len(watch_pods) > 0:
time.sleep(cfg.backoff)
new_watch_pods: typing.List[Pod] = []
for p in watch_pods:
try:
read_pod = core_v1.read_namespaced_pod(p.name, p.namespace)
if read_pod.metadata.name != p.name:
return "error", PodErrorOutput(
"Error retrieveing pod {}".format(p.name)
)
if str(read_pod.metadata.creation_timestamp.tzinfo) > p.creation_timestamp:
continue
new_watch_pods.append(p)
except ApiException as e:
if e.status != 404:
raise
watch_pods = new_watch_pods
current_time = time.time()
if current_time - start_time > cfg.timeout:
return "error", PodErrorOutput(
"Timeout while waiting for pods to be removed."
)
return "success", PodKillSuccessOutput(killed_pods)
# endregion
except Exception:
return "error", PodErrorOutput(format_exc())
@dataclass
class WaitForPodsConfig:
"""
WaitForPodsConfig is a configuration structure for wait-for-pod steps.
"""
namespace_pattern: re.Pattern
name_pattern: typing.Annotated[
typing.Optional[re.Pattern], validation.required_if_not("label_selector")
] = None
label_selector: typing.Annotated[
typing.Optional[str],
validation.min(1),
validation.required_if_not("name_pattern"),
] = None
count: typing.Annotated[int, validation.min(1)] = field(
default=1,
metadata={
"name": "Pod count",
"description": "Wait for at least this many pods to exist",
},
)
timeout: typing.Annotated[int, validation.min(1)] = field(
default=180,
metadata={"name": "Timeout", "description": "How many seconds to wait for?"},
)
backoff: int = field(
default=1,
metadata={
"name": "Backoff",
"description": "How many seconds to wait between checks for the target pod status.",
},
)
kubeconfig_path: typing.Optional[str] = None
@plugin.step(
"wait-for-pods",
"Wait for pods",
"Wait for the specified number of pods to be present",
{"success": PodWaitSuccessOutput, "error": PodErrorOutput},
)
def wait_for_pods(
cfg: WaitForPodsConfig,
) -> typing.Tuple[str, typing.Union[PodWaitSuccessOutput, PodErrorOutput]]:
try:
with setup_kubernetes(cfg.kubeconfig_path) as cli:
core_v1 = client.CoreV1Api(cli)
timeout = False
start_time = datetime.now()
while not timeout:
pods = _find_pods(
core_v1, cfg.label_selector, cfg.name_pattern, cfg.namespace_pattern
)
ready_pods = 0
for pod in pods:
ready = True
if pod.status:
for container in pod.status.container_statuses:
if not container.ready:
ready = False
else:
ready = False
if ready:
ready_pods += 1
if ready_pods >= cfg.count:
return "success", PodWaitSuccessOutput(
list(
map(
lambda p: Pod(p.metadata.namespace, p.metadata.name, str(p.metadata.creation_timestamp)),
pods,
)
)
)
time.sleep(cfg.backoff)
now_time = datetime.now()
time_diff = now_time - start_time
if time_diff.seconds > cfg.timeout:
return "error", PodErrorOutput(
"timeout while waiting for pods to come up"
)
except Exception:
return "error", PodErrorOutput(format_exc())
if __name__ == "__main__":
sys.exit(
plugin.run(
plugin.build_schema(
kill_pods,
wait_for_pods,
)
)
)