-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoperator.py
496 lines (434 loc) · 17.8 KB
/
operator.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python
import asyncio
import kopf
import kubernetes_asyncio
import logging
from anarchy import Anarchy
from anarchygovernor import AnarchyGovernor
from anarchysubject import AnarchySubject
from anarchyaction import AnarchyAction
from anarchyrun import AnarchyRun
from anarchyrunner import AnarchyRunner
from configure_kopf_logging import configure_kopf_logging
from infinite_relative_backoff import InfiniteRelativeBackoff
@kopf.on.startup()
async def on_startup(settings: kopf.OperatorSettings, logger, **_):
await Anarchy.on_startup()
await AnarchyGovernor.preload()
# Never give up from network errors
settings.networking.error_backoffs = InfiniteRelativeBackoff()
# Store last handled configuration in status
settings.persistence.diffbase_storage = kopf.StatusDiffBaseStorage(field='status.diffBase')
# Use operator domain as finalizer
settings.persistence.finalizer = Anarchy.domain
# Store progress in status. Some objects may be too large to store status in metadata annotations
settings.persistence.progress_storage = kopf.StatusProgressStorage(field='status.kopf.progress')
# Only create events for warnings and errors
settings.posting.level = logging.WARNING
# Disable scanning for crds and namespaces
settings.scanning.disabled = True
configure_kopf_logging()
@kopf.on.cleanup()
async def on_cleanup(**_):
await Anarchy.on_cleanup()
@kopf.on.event(Anarchy.domain, Anarchy.version, 'anarchygovernors')
async def governor_event(event, logger, **_):
await AnarchyGovernor.handle_event(event)
@kopf.on.create(
Anarchy.domain, Anarchy.version, 'anarchysubjects',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def subject_create(**kwargs):
anarchy_subject = AnarchySubject.load(**kwargs)
async with anarchy_subject.lock:
await anarchy_subject.handle_create()
@kopf.on.delete(
Anarchy.domain, Anarchy.version, 'anarchysubjects',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def subject_delete(**kwargs):
anarchy_subject = AnarchySubject.load(**kwargs)
async with anarchy_subject.lock:
await anarchy_subject.handle_delete()
@kopf.on.resume(
Anarchy.domain, Anarchy.version, 'anarchysubjects',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def subject_resume(meta, name, **kwargs):
if 'deletionTimestamp' in meta \
and Anarchy.domain not in meta['finalizers'] \
and Anarchy.subject_label not in meta['finalizers']:
logging.info(f"Ignoring functionally deleted AnarchySubject {name} on resume")
return
anarchy_subject = AnarchySubject.load(meta=meta, name=name, **kwargs)
async with anarchy_subject.lock:
await anarchy_subject.handle_resume()
@kopf.on.update(
Anarchy.domain, Anarchy.version, 'anarchysubjects',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def subject_update(new, old, **kwargs):
anarchy_subject = AnarchySubject.load(**kwargs)
if old['spec'] != new['spec']:
async with anarchy_subject.lock:
await anarchy_subject.handle_update(previous_state=old)
@kopf.on.event(
Anarchy.domain, Anarchy.version, 'anarchysubjects',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def subject_event(event, **_):
obj = event.get('object')
if not obj or obj.get('apiVersion') != Anarchy.api_version:
return
metadata = obj['metadata']
name = metadata['name']
finalizers = metadata.get('finalizers', [])
if event['type'] == 'DELETED':
logging.info(f"Remove deleted AnarchySubject {name} from cache")
AnarchySubject.cache.pop(name, None)
elif Anarchy.domain in finalizers:
# Subject is still handled through kopf framework
pass
elif Anarchy.subject_label in finalizers:
# Kopf managed finalizer has been removed but subject label remains,
# This should only happen during delete handling.
if 'deletionTimestamp' in metadata:
anarchy_subject = AnarchySubject.load_definition(obj)
async with anarchy_subject.lock:
await anarchy_subject.handle_post_delete_event()
else:
logging.error(
f"AnarchySubject {name} does not have {Anarchy.domain} label "
f"but does have {Anarchy.subject_label} but is not deleting?"
)
else:
logging.debug(f"Ignoring deleting AnarchySubject {name}")
@kopf.on.create(
Anarchy.domain, Anarchy.version, 'anarchyactions',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def action_create(**kwargs):
anarchy_action = AnarchyAction.load(**kwargs)
anarchy_subject = await anarchy_action.get_subject()
if not anarchy_subject:
await anarchy_action.raise_error_if_still_exists(
f"{anarchy_action} references missing AnarchySubject {anarchy_action.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_action} when {anarchy_subject} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_action.handle_create(anarchy_subject)
@kopf.on.delete(
Anarchy.domain, Anarchy.version, 'anarchyactions',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def action_delete(**kwargs):
anarchy_action = AnarchyAction.load(**kwargs)
anarchy_action.remove_from_cache()
anarchy_subject = await anarchy_action.get_subject()
if not anarchy_subject:
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_action} when {anarchy_subject} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_action.handle_delete(anarchy_subject)
@kopf.on.resume(
Anarchy.domain, Anarchy.version, 'anarchyactions',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def action_resume(**kwargs):
anarchy_action = AnarchyAction.load(**kwargs)
anarchy_subject = await anarchy_action.get_subject()
if not anarchy_subject:
await anarchy_action.raise_error_if_still_exists(
f"{anarchy_action} references missing AnarchySubject {anarchy_action.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_action} when {anarchy_subject} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_action.handle_resume(anarchy_subject)
@kopf.on.update(
Anarchy.domain, Anarchy.version, 'anarchyactions',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def action_update(**kwargs):
anarchy_action = AnarchyAction.load(**kwargs)
anarchy_subject = await anarchy_action.get_subject()
if not anarchy_subject:
if anarchy_action.is_delete_handler:
return
await anarchy_action.raise_error_if_still_exists(
f"{anarchy_action} references missing AnarchySubject {anarchy_action.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_action} when {anarchy_subject} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_action.handle_update(anarchy_subject)
@kopf.daemon(
Anarchy.domain, Anarchy.version, 'anarchyactions',
cancellation_timeout=1,
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def action_daemon(stopped, **kwargs):
anarchy_action = AnarchyAction.load(**kwargs)
anarchy_subject = await anarchy_action.get_subject()
if not anarchy_subject:
await anarchy_action.raise_error_if_still_exists(
f"{anarchy_action} references missing AnarchySubject {anarchy_action.subject_name}"
)
return
try:
while not stopped:
async with anarchy_subject.lock:
await anarchy_subject.refresh()
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_action} when {anarchy_subject} is marked with ignore",
delay=30
)
sleep_interval = await anarchy_action.manage(anarchy_subject)
if sleep_interval:
await asyncio.sleep(sleep_interval)
except asyncio.CancelledError:
pass
@kopf.on.delete(
Anarchy.domain, Anarchy.version, 'anarchyruns',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def run_delete(**kwargs):
anarchy_run = AnarchyRun.load(**kwargs)
anarchy_run.remove_from_cache()
anarchy_subject = await anarchy_run.get_subject()
if not anarchy_subject:
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_subject} is marked with ignore",
delay=30
)
if anarchy_run.has_action:
anarchy_action = await anarchy_run.get_action()
else:
anarchy_action = None
if anarchy_action and anarchy_action.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_action} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_run.handle_delete(anarchy_subject, anarchy_action)
@kopf.on.resume(
Anarchy.domain, Anarchy.version, 'anarchyruns',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def run_resume(**kwargs):
anarchy_run = AnarchyRun.load(**kwargs)
anarchy_subject = await anarchy_run.get_subject()
if not anarchy_subject:
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchySubject {anarchy_run.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_subject} is marked with ignore",
delay=30
)
if anarchy_run.has_action:
anarchy_action = await anarchy_run.get_action()
if not anarchy_action:
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchyAction {anarchy_run.action_name}"
)
return
else:
anarchy_action = None
if anarchy_action and anarchy_action.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_action} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
await anarchy_run.handle_resume(anarchy_subject, anarchy_action)
@kopf.on.update(
Anarchy.domain, Anarchy.version, 'anarchyruns',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def run_update(new, old, **kwargs):
anarchy_run = AnarchyRun.load(**kwargs)
anarchy_subject = await anarchy_run.get_subject()
if not anarchy_subject:
if anarchy_run.is_delete_handler:
return
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchySubject {anarchy_run.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_subject} is marked with ignore",
delay=30
)
if anarchy_run.has_action:
anarchy_action = await anarchy_run.get_action()
if not anarchy_action:
if anarchy_run.is_delete_handler:
return
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchyAction {anarchy_run.action_name}"
)
return
else:
anarchy_action = None
if anarchy_action and anarchy_action.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_action} is marked with ignore",
delay=30
)
new_runner_state = new['metadata'].get('labels', {}).get(Anarchy.runner_label)
old_runner_state = old['metadata'].get('labels', {}).get(Anarchy.runner_label)
async with anarchy_subject.lock:
if new_runner_state != old_runner_state:
if new_runner_state == 'canceled':
await anarchy_run.handle_canceled(anarchy_subject, anarchy_action)
elif new_runner_state == 'failed':
await anarchy_run.handle_failed(anarchy_subject, anarchy_action)
elif new_runner_state == 'lost':
await anarchy_run.handle_lost(anarchy_subject, anarchy_action)
elif new_runner_state == 'pending':
await anarchy_run.handle_pending(anarchy_subject, anarchy_action)
elif new_runner_state == 'queued':
logging.error(f"{anarchy_run} returned to state queued?")
elif new_runner_state == 'successful':
await anarchy_run.handle_success(anarchy_subject, anarchy_action)
else:
await anarchy_run.handle_running(anarchy_subject, anarchy_action)
@kopf.daemon(
Anarchy.domain, Anarchy.version, 'anarchyruns',
cancellation_timeout=1,
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def run_daemon(stopped, **kwargs):
anarchy_run = AnarchyRun.load(**kwargs)
anarchy_subject = await anarchy_run.get_subject()
if not anarchy_subject:
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchySubject {anarchy_run.subject_name}"
)
return
if anarchy_subject.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_subject} is marked with ignore",
delay=30
)
if anarchy_run.has_action:
anarchy_action = await anarchy_run.get_action()
if not anarchy_action:
await anarchy_run.raise_error_if_still_exists(
f"{anarchy_run} references missing AnarchyAction {anarchy_run.action_name}"
)
return
else:
anarchy_action = None
try:
while not stopped:
await anarchy_run.refresh()
await anarchy_subject.refresh()
if anarchy_run.ignore or anarchy_subject.ignore:
return
if anarchy_action:
await anarchy_action.refresh()
if anarchy_action.ignore:
raise kopf.TemporaryError(
f"Refusing to handle {anarchy_run} when {anarchy_action} is marked with ignore",
delay=30
)
async with anarchy_subject.lock:
sleep_interval = await anarchy_run.manage(anarchy_subject, anarchy_action)
if sleep_interval:
await asyncio.sleep(sleep_interval)
except asyncio.CancelledError:
pass
if not Anarchy.running_all_in_one:
@kopf.on.create(
Anarchy.domain, Anarchy.version, 'anarchyrunners',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def runner_create(logger, **kwargs):
anarchy_runner = AnarchyRunner.load(**kwargs)
async with anarchy_runner.lock:
await anarchy_runner.handle_create(logger=logger)
@kopf.on.delete(
Anarchy.domain, Anarchy.version, 'anarchyrunners',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def runner_delete(logger, **kwargs):
anarchy_runner = AnarchyRunner.load(**kwargs)
async with anarchy_runner.lock:
await anarchy_runner.handle_delete(logger=logger)
@kopf.on.resume(
Anarchy.domain, Anarchy.version, 'anarchyrunners',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def runner_resume(logger, **kwargs):
anarchy_runner = AnarchyRunner.load(**kwargs)
async with anarchy_runner.lock:
await anarchy_runner.handle_resume(logger=logger)
@kopf.on.update(
Anarchy.domain, Anarchy.version, 'anarchyrunners',
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def runner_update(logger, **kwargs):
anarchy_runner = AnarchyRunner.load(**kwargs)
async with anarchy_runner.lock:
await anarchy_runner.handle_update(logger=logger)
@kopf.daemon(
Anarchy.domain, Anarchy.version, 'anarchyrunners',
cancellation_timeout=1,
labels={Anarchy.ignore_label: kopf.ABSENT},
)
async def runner_daemon(logger, stopped, **kwargs):
anarchy_runner = AnarchyRunner.load(**kwargs)
while not stopped:
try:
async with anarchy_runner.lock:
await anarchy_runner.manage_pods(logger=logger)
await asyncio.sleep(anarchy_runner.scaling_check_interval)
except asyncio.CancelledError:
pass
except Exception:
logger.exception(f"Exception in daemon for {anarchy_runner}")
@kopf.on.event('pods', labels={Anarchy.runner_label: kopf.PRESENT})
async def runner_pod_event(event, logger, **_):
obj = event.get('object')
if not obj or obj.get('kind') != 'Pod':
logging.warning(f"Weird event {event}")
return
pod = Anarchy.k8s_obj_from_dict(obj, kubernetes_asyncio.client.V1Pod)
anarchy_runner_name = pod.metadata.labels[Anarchy.runner_label]
anarchy_runner = await AnarchyRunner.get(anarchy_runner_name)
if not anarchy_runner:
logger.warning(f"AnarchyRunner {anarchy_runner_name} not found for Pod {pod.metadata.name}")
return
if anarchy_runner.ignore:
return
async with anarchy_runner.lock:
if event['type'] == 'DELETED':
await anarchy_runner.handle_runner_pod_deleted(pod=pod, logger=logger)
elif Anarchy.runner_terminating_label not in pod.metadata.labels:
await anarchy_runner.handle_runner_pod_event(pod=pod, logger=logger)