This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seoperator2.py
executable file
·707 lines (581 loc) · 31.6 KB
/
seoperator2.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/python3 -u
import abc
import collections
import email.utils
import functools
import inspect
import json
import os
import pickle
import signal
import socket
import subprocess
import sys
import threading
import time
import traceback
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime, timezone
from fnmatch import fnmatch
from typing import Any, Dict, Generic, Iterable, Iterator, List, Optional, Tuple, Type, TypeVar
T = TypeVar("T")
# A simple decorator that can be used to wrap a function so that it is retried with a backoff strategy.
def retry(
exceptions: Optional[Tuple[Type[BaseException], ...]] = None,
delays: Optional[List[float]] = None):
# Repeat http connection and data processing errors by default.
if exceptions is None:
exceptions = (urllib.error.URLError, urllib.error.HTTPError, ValueError)
# The default backoff strategy.
if delays is None:
delays = [0.5, 1, 1, 3, 5]
def retry_decorator(f):
@functools.wraps(f)
def true_retry_decorator(*args, **kwargs):
i = 0
while True:
try:
return f(*args, **kwargs)
except exceptions as e:
# Set eventid in log. That customization is a bit hacky, but it's simple and enough.
print_kwargs = {}
for idx, val in enumerate(inspect.getfullargspec(f).args):
if val == "event":
event = args[idx]
if hasattr(event, 'eventid'):
print_kwargs["eventid"] = event.eventid
break
func_name = f.__qualname__
exception_name = e.__class__.__qualname__
if i >= len(delays):
print_(f"Function {func_name} failed due to {exception_name}. "
f"Mo more retries, it was the last attempt.",
**print_kwargs)
raise e
delay = delays[i]
print_(f"Function {func_name} failed due to {exception_name}. "
f"Retrying in {delay}s",
**print_kwargs)
# Todo: instead of time.sleep, use exit_threading_event from LifeManager.
# Is it possible to do this without singleton/global variable/etc?
# Which is the ok solution?
# if life_manager.exit_threading_event.wait(delay):
# print_(f"Function {func_name} failed due to {exception_name}. "
# "Retry cancelled due to ongoing graceful shutdown.",
# **print_kwargs)
# raise e
time.sleep(delay)
i += 1
return true_retry_decorator
return retry_decorator
# Serialize arbitrary Python objects to JSON.
# Fixes: TypeError: Object of type Xyz is not JSON serializable
# Fix consists of JsonSerializable, JsonSerializableEncoder.
class JsonSerializable(abc.ABC):
@abc.abstractmethod
def to_json(self) -> Any:
pass
def __str__(self) -> str:
return str(self.to_json())
def __repr__(self) -> str:
return (f"{self.__class__.__name__}("
f"{repr(self.to_json())})")
# Take a look at the documentation of JsonSerializable.
# https://docs.python.org/3/library/json.html#json.JSONEncoder.default
class JsonSerializableEncoder(json.JSONEncoder):
def default(self, o: Any):
if isinstance(o, JsonSerializable):
return o.to_json()
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, o)
# Print in a version that produces output containing json.
def print_(message: str, **kwargs) -> None:
timestamp = datetime.now(timezone.utc).replace(microsecond=0).isoformat()
print(json.dumps({"timestamp": timestamp, "message": message, **kwargs}, cls=JsonSerializableEncoder), flush=True)
# An interface that provides methods to "cache" (save) state.
# The state is stored on disk, under the specified key.
class Cacheable(Generic[T]):
def __init__(self, cache_dir: str) -> None:
super().__init__()
self._cache_dir: str = cache_dir
self._cache_enabled: bool = os.path.isdir(cache_dir)
def _cache_read(self, key: str, default_value: T) -> T:
if not self._cache_enabled:
return default_value
cache_file = os.path.join(self._cache_dir, key)
try:
with open(cache_file, "rb") as handle:
return pickle.load(handle)
except (OSError, IOError):
return default_value
def _cache_write(self, key: str, value: T) -> None:
if not self._cache_enabled:
pass
cache_file = os.path.join(self._cache_dir, key)
with open(cache_file, "wb") as handle:
pickle.dump(value, handle)
# A list that saves the state to disk with each change.
# The list recreates its last state in the constructor, so it is immune to container restarts.
class CacheableList(Cacheable[List[T]], Iterable[T], JsonSerializable):
def __init__(self, cache_dir: str, name: str) -> None:
super().__init__(cache_dir)
self._name: str = name
self._list: List[T] = super()._cache_read(name, [])
def append(self, value: T) -> None:
self._list.append(value)
self._cache_write(self._name, self._list)
def remove(self, value: T) -> None:
self._list.remove(value)
self._cache_write(self._name, self._list)
def __len__(self) -> int:
return len(self._list)
def __iter__(self) -> Iterator[T]:
return iter(self._list)
def to_json(self) -> List[T]:
return self._list
# This class has easily accessible information about the name of the VM on which this script is running.
# The "Azure Instance Metadata Service (Linux)" is helpful.
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service?tabs=linux
class ThisHostnames(JsonSerializable):
@retry()
def __init__(self, api_metadata_instance: str, socket_timeout_seconds: int) -> None:
super().__init__()
request = urllib.request.Request(api_metadata_instance)
request.add_header("Metadata", "true")
with urllib.request.urlopen(request, timeout=socket_timeout_seconds) as response:
if response.status // 100 != 2:
raise ValueError("Instance metadata API responded with code {response.status}.")
metadata_instance = json.loads(response.read())
self.hostname: str = socket.gethostname()
self.compute_name: str = metadata_instance["compute"]["name"]
self.node_name: str = self._compute_name_to_node_name(self.compute_name)
# example
# input: aks-default-36328368-vmss_18
# output: aks-default-36328368-vmss00000i
@staticmethod
def _compute_name_to_node_name(compute_name: str) -> str:
name_prefix, vm_index_base10 = compute_name.split("_")
vm_index_base36 = ThisHostnames._b36_encode(int(vm_index_base10))
return name_prefix + vm_index_base36.rjust(6, "0")
@staticmethod
def _b36_encode(num: int) -> str:
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
result = ""
while not result or num > 0:
num, i = divmod(num, 36)
result = digits[i] + result
return result
def to_json(self) -> Dict[str, Any]:
return {
"hostname": self.hostname,
"compute_name": self.compute_name,
"node_name": self.node_name,
}
# An object-oriented representation of a single "scheduled event".
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/scheduled-events#query-for-events
class ScheduledEvent(JsonSerializable):
NOT_A_DATE = datetime.fromtimestamp(0, timezone.utc)
NOT_A_DATE_ISO = datetime.fromtimestamp(0, timezone.utc).isoformat()
def __init__(self, event: Dict[str, Any]) -> None:
super().__init__()
self._raw: Dict[str, Any] = event
self.eventid: str = event["EventId"]
self.eventtype: str = event["EventType"]
self.resourcetype: str = event["ResourceType"]
self.resources: List[str] = event["Resources"]
self.eventstatus: str = event["EventStatus"]
self.notbefore: datetime = self._parsedate_to_datetime(event["NotBefore"])
self.description: str = event["Description"]
self.eventsource: str = event["EventSource"]
self.durationinseconds: int = event["DurationInSeconds"]
self.seo2startedat: datetime = datetime.fromisoformat(event.get("Seo2StartedAt", ScheduledEvent.NOT_A_DATE_ISO))
def mark_as_started(self) -> None:
self.seo2startedat = datetime.now(timezone.utc)
def seconds_since_started(self) -> float:
return abs((datetime.now(timezone.utc) - self.seo2startedat).total_seconds())
@staticmethod
# https://bugs.python.org/issue30681
def _parsedate_to_datetime(value) -> datetime:
try:
result = email.utils.parsedate_to_datetime(value)
if result is None:
raise ValueError
return result
except (TypeError, ValueError):
return ScheduledEvent.NOT_A_DATE
def to_json(self) -> Dict[str, Any]:
return {
**self._raw,
"Seo2StartedAt": self.seo2startedat.isoformat()
}
# An object-oriented representation of a whole "scheduled events" response.
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/scheduled-events#query-for-events
class ScheduledEvents(Iterable[ScheduledEvent], JsonSerializable):
def __init__(self, events: Dict[str, Any]) -> None:
super().__init__()
self._raw: Dict[str, Any] = events
self.document_incarnation: int = events["DocumentIncarnation"]
self.events: List[ScheduledEvent] = list(map(ScheduledEvent, events.get("Events", [])))
def __len__(self) -> int:
return len(self.events)
def __iter__(self) -> Iterator[ScheduledEvent]:
return iter(self.events)
def to_json(self) -> Dict[str, Any]:
return self._raw
# A tool to perform operations on the "scheduled events" API.
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/scheduled-events#query-for-events
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/scheduled-events#start-an-event
class ScheduledEventsManager:
def __init__(self,
api_metadata_scheduledevents: str,
socket_timeout_seconds: int,
delay_before_program_close_seconds: int) -> None:
super().__init__()
self.api_metadata_scheduledevents: str = api_metadata_scheduledevents
self.socket_timeout: int = socket_timeout_seconds
self.delay_before_program_close_seconds: int = delay_before_program_close_seconds
@retry()
def query_for_events(self) -> ScheduledEvents:
request = urllib.request.Request(self.api_metadata_scheduledevents)
request.add_header("Metadata", "true")
with urllib.request.urlopen(request, timeout=self.socket_timeout) as response:
if response.status // 100 != 2:
raise ValueError("ScheduledEvents API responded with code {response.status}.")
metadata_scheduledevents = json.loads(response.read())
return ScheduledEvents(metadata_scheduledevents)
@retry()
def start_an_event(self, event: ScheduledEvent) -> str:
# A node redeploy can follow immediately, sleep as at the end of the program.
# Give some time to external monitoring to collect logs.
time.sleep(self.delay_before_program_close_seconds)
data = {"StartRequests": [{"EventId": event.eventid}]}
data_bytes = json.dumps(data).encode("utf-8")
request = urllib.request.Request(self.api_metadata_scheduledevents, data=data_bytes)
request.add_header("Metadata", "true")
request.add_header("Content-Type", "application/json")
with urllib.request.urlopen(request, timeout=self.socket_timeout) as response:
if response.status // 100 != 2:
raise ValueError("ScheduledEvents API responded with code {response.status}.")
return response.read().decode('utf-8', 'ignore')
# Subprocess related tools, external dependencies somehow have to be running.
class SubprocessUtils:
def __init__(self):
raise AssertionError
@staticmethod
def subprocess_run_async(cmd: List[str], **_print_kwargs) -> 'subprocess.Popen[str]':
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
print_(f"Running a command {cmd}.", subprocess=proc.pid, **_print_kwargs)
proc_output_reader = threading.Thread(target=SubprocessUtils._subprocess_stdout_reader,
args=(proc,),
kwargs=_print_kwargs)
proc_output_reader.start()
return proc
@staticmethod
# https://stackoverflow.com/a/18423003
def _subprocess_stdout_reader(proc: 'subprocess.Popen[str]', **print_kwargs) -> None:
if proc.stdout is not None:
# pyre-ignore[16]: https://github.com/facebook/pyre-check/issues/221
for message in proc.stdout:
print_(message, subprocess=proc.pid, **print_kwargs)
@staticmethod
def subprocess_run_sync(cmd: List[str], **_print_kwargs) -> 'subprocess.CompletedProcess[str]':
print_(f"Running a command {cmd}.", subprocess=-1, **_print_kwargs)
return subprocess.run(cmd, text=True, capture_output=True, check=False)
# An object-oriented representation of a "kubectl version" response.
class KubectlVersion(JsonSerializable):
def __init__(self, client_version: str, server_version: str, stderr: str) -> None:
super().__init__()
self.client_version: str = client_version
self.server_version: str = server_version
self.stderr: str = stderr
def to_json(self) -> Dict[str, Any]:
return {
"client_version": self.client_version,
"server_version": self.server_version,
"stderr": self.stderr
}
# Tool to perform operations with the "kubectl" tool.
class KubectlManager:
def __init__(self,
cache_dir: str,
kubectl_drain_options: List[str],
this_hostnames: ThisHostnames) -> None:
super().__init__()
self.kubectl_cordon_cache: CacheableList[ScheduledEvent] = CacheableList(cache_dir, "kubectl_cordon_cache")
self.kubectl_drain_options: List[str] = kubectl_drain_options
self.this_hostnames: ThisHostnames = this_hostnames
@retry((subprocess.SubprocessError, ValueError))
def kubectl_cordon(self, event: ScheduledEvent) -> None:
proc = SubprocessUtils.subprocess_run_async(
["kubectl", "cordon", self.this_hostnames.node_name],
eventid=event.eventid)
exit_code = proc.wait()
if exit_code != 0:
raise ValueError("kubectl cordon operation failed with code {exit_code}.")
self.kubectl_cordon_cache.append(event)
@retry((subprocess.SubprocessError, ValueError))
def kubectl_drain(self, event: ScheduledEvent) -> None:
proc = SubprocessUtils.subprocess_run_async(
["kubectl", "drain", self.this_hostnames.node_name, *self.kubectl_drain_options],
eventid=event.eventid)
exit_code = proc.wait()
if exit_code != 0:
raise ValueError("kubectl drain operation failed with code {exit_code}.")
@retry((subprocess.SubprocessError, ValueError))
def kubectl_uncordon(self, event: ScheduledEvent) -> None:
proc = SubprocessUtils.subprocess_run_async(
["kubectl", "uncordon", self.this_hostnames.node_name],
eventid=event.eventid)
exit_code = proc.wait()
if exit_code != 0:
raise ValueError("kubectl uncordon operation failed with code {exit_code}.")
self.kubectl_cordon_cache.remove(event)
@staticmethod
@retry((subprocess.SubprocessError, ValueError))
def kubectl_version() -> KubectlVersion:
kubectl_version_proc = SubprocessUtils.subprocess_run_sync(["kubectl", "version", "-o", "json"])
if kubectl_version_proc.returncode != 0:
exit_code = kubectl_version_proc.returncode
stderr = kubectl_version_proc.stderr
raise ValueError(f"kubectl version operation failed with code {exit_code}, stderr='{stderr}'.")
try:
versions = json.loads(kubectl_version_proc.stdout)
except json.JSONDecodeError:
print_("Failed to parse 'kubectl version' response.")
versions = {}
client_version = versions.get("clientVersion", {}).get("gitVersion", None)
server_version = versions.get("serverVersion", {}).get("gitVersion", None)
stderr = kubectl_version_proc.stderr.rstrip()
return KubectlVersion(client_version, server_version, stderr)
# An object-oriented representation of a "processing-rules" config key element.
class ProcessingRule(JsonSerializable):
def __init__(self, rule: Dict[str, Any]) -> None:
super().__init__()
self.rule_type: str = rule["rule-type"]
self.event_type_is: List[str] = rule["event-type-is"]
self.and_duration_in_seconds_less_equal_to: Optional[int] = rule.get("and-duration-in-seconds-less-equal-to")
self.and_duration_in_seconds_greater_equal_to: Optional[int] = \
rule.get("and-duration-in-seconds-greater-equal-to")
self.and_compute_name_matches: Optional[str] = rule.get("and-compute-name-matches")
self.and_compute_name_not_matches: Optional[str] = rule.get("and-compute-name-not-matches")
self.and_node_name_matches: Optional[str] = rule.get("and-node-name-matches")
self.and_node_name_not_matches: Optional[str] = rule.get("and-node-name-not-matches")
def to_json(self) -> Dict[str, Any]:
return {
"rule_type": self.rule_type,
"event_type_is": self.event_type_is,
"and_duration_in_seconds_less_equal_to": self.and_duration_in_seconds_less_equal_to,
"and_duration_in_seconds_greater_equal_to": self.and_duration_in_seconds_greater_equal_to,
"and_compute_name_matches": self.and_compute_name_matches,
"and_compute_name_not_matches": self.and_compute_name_not_matches,
"and_node_name_matches": self.and_node_name_matches,
"and_node_name_not_matches": self.and_node_name_not_matches,
}
# A tool that looks at all the rules in List[ProcessingRule]
# and gives a final answer as to whether the event should be handled.
class ProcessingRuleProcessor:
def __init__(self,
processing_rules: List[ProcessingRule],
this_hostnames: ThisHostnames) -> None:
super().__init__()
self.processing_rules: List[ProcessingRule] = processing_rules
self.this_hostnames: ThisHostnames = this_hostnames
def all_considered_should_handle(self, event: ScheduledEvent) -> bool:
for processing_rule in self.processing_rules:
if self._handle_event_if(processing_rule, event):
return True
if self._ignore_event_if(processing_rule, event):
return False
return True
def _handle_event_if(self, processing_rule: ProcessingRule, event: ScheduledEvent) -> bool:
return processing_rule.rule_type == "handle-event-if" and self._process_event_if(processing_rule, event)
def _ignore_event_if(self, processing_rule: ProcessingRule, event: ScheduledEvent) -> bool:
return processing_rule.rule_type == "ignore-event-if" and self._process_event_if(processing_rule, event)
def _process_event_if(self, processing_rule: ProcessingRule, event: ScheduledEvent) -> bool:
res = event.eventtype in processing_rule.event_type_is
res &= (processing_rule.and_duration_in_seconds_less_equal_to is None
or processing_rule.and_duration_in_seconds_less_equal_to >= event.durationinseconds > 0)
res &= (processing_rule.and_duration_in_seconds_greater_equal_to is None
or event.durationinseconds >= processing_rule.and_duration_in_seconds_greater_equal_to
or event.durationinseconds < 0)
res &= (processing_rule.and_compute_name_matches is None
or fnmatch(self.this_hostnames.compute_name, processing_rule.and_compute_name_matches))
res &= (processing_rule.and_compute_name_not_matches is None
or not fnmatch(self.this_hostnames.compute_name, processing_rule.and_compute_name_not_matches))
res &= (processing_rule.and_node_name_matches is None
or fnmatch(self.this_hostnames.node_name, processing_rule.and_node_name_matches))
res &= (processing_rule.and_node_name_not_matches is None
or not fnmatch(self.this_hostnames.node_name, processing_rule.and_node_name_not_matches))
return res
# Configuration - what can be set with parameters to the script.
class Config(JsonSerializable):
def __init__(self, config_file: str, cache_dir: str) -> None:
super().__init__()
config_data = {}
if os.path.isfile(config_file):
with open(config_file, encoding='utf-8') as f:
config_data = json.load(f)
self.config_file: str = config_file
self.cache_dir: str = cache_dir
self.api_metadata_instance: str = \
config_data.get("api-metadata-instance",
"http://169.254.169.254/metadata/scheduledevents?api-version=2020-07-01")
self.api_metadata_scheduledevents: str = \
config_data.get("api-metadata-scheduledevents",
"http://169.254.169.254/metadata/instance?api-version=2021-02-01")
self.polling_frequency_seconds: int = config_data.get("polling-frequency-seconds", 60)
self.socket_timeout_seconds: int = config_data.get("socket-timeout-seconds", 10)
self.processing_rules: List[ProcessingRule] = \
list(map(ProcessingRule, config_data.get("processing-rules", [])))
self.kubectl_drain_options: List[str] = config_data.get("kubectl-drain-options", [])
self.delay_before_uncordon_seconds: int = config_data.get("delay-before-uncordon-seconds", 120)
self.delay_before_program_close_seconds: int = config_data.get("delay-before-program-close-seconds", 5)
def to_json(self) -> Dict[str, Any]:
return {
"config_file": self.config_file,
"cache_dir": self.cache_dir,
"api_metadata_instance": self.api_metadata_instance,
"api_metadata_scheduledevents": self.api_metadata_scheduledevents,
"polling_frequency_seconds": self.polling_frequency_seconds,
"socket_timeout_seconds": self.socket_timeout_seconds,
"processing_rules": self.processing_rules,
"kubectl_drain_options": self.kubectl_drain_options,
"delay_before_program_close_seconds": self.delay_before_program_close_seconds,
}
# The operator is here. Everything else is unnecessary.
class Seoperator2:
def __init__(self,
cache_dir: str,
processing_rules_processor: ProcessingRuleProcessor,
this_hostnames: ThisHostnames,
scheduled_events_manager: ScheduledEventsManager,
kubectl_manager: KubectlManager,
delay_before_uncordon_seconds: int) -> None:
super().__init__()
self.processing_rules_processor: ProcessingRuleProcessor = processing_rules_processor
self.this_hostnames: ThisHostnames = this_hostnames
self.scheduled_events_manager: ScheduledEventsManager = scheduled_events_manager
self.kubectl_manager: KubectlManager = kubectl_manager
self.delay_before_uncordon_seconds: int = delay_before_uncordon_seconds
self.already_processed_events: CacheableList[str] = CacheableList(cache_dir, "already_processed_events")
def handle_scheduled_events(self, events: ScheduledEvents) -> None:
# If an event is finished, it will no longer be reported by the scheduledevents API.
# uncordon nodes affected by scheduled events in the past.
for cached_event in self.kubectl_manager.kubectl_cordon_cache:
if not any(cached_event.eventid == event.eventid for event in events) \
and cached_event.seconds_since_started() > self.delay_before_uncordon_seconds:
print_(f"Found an event from the past {cached_event.eventid}.", eventid=cached_event.eventid)
print_(f"Handling the past event {cached_event.eventid}.", eventid=cached_event.eventid)
self.kubectl_manager.kubectl_uncordon(cached_event)
print_(f"Handled the past event {cached_event.eventid}.", eventid=cached_event.eventid)
if len(events) == 0:
return
print_(f"The current list of planned events includes {len(events)} events.", events=events)
events2: Iterator[ScheduledEvent] = iter(events)
events2 = filter(lambda event: event.eventid not in self.already_processed_events, events2)
events2 = filter(lambda event: self.this_hostnames.compute_name in event.resources, events2)
events2 = filter(lambda event: event.resourcetype == "VirtualMachine", events2)
events2 = filter(lambda event: event.eventstatus == "Scheduled", events2)
events2 = filter(self.processing_rules_processor.all_considered_should_handle, events2)
for event in events2:
print_(f"Found an event {event.eventid} ({event.eventtype}).", eventid=event.eventid)
print_(f"Handling the event {event.eventid}.", eventid=event.eventtype)
self.handle_scheduled_event(event)
print_(f"Handled the event {event.eventid}.", eventid=event.eventid)
self.already_processed_events.append(event.eventid)
def handle_scheduled_event(self, event: ScheduledEvent) -> None:
self.kubectl_manager.kubectl_cordon(event)
self.kubectl_manager.kubectl_drain(event)
if len(event.resources) > 1:
# The event confirmation applies to all resources in the event,
# it is not possible to start the event for one machine.
# As we do not want to start the event prematurely, we are not starting the event at all.
# We could also choose the leader and (on that node) do handling for each resource separately,
# but then we will increase the processing time and/or memory usage (both of them are in short supply).
# Azure will start the event when the time to handle it has passed.
# Azure will then delete the node when it notices that it is not being used.
print_(f"Not starting the scheduled event {event.eventid},"
f"the event affects multiple nodes.", eventid=event.eventid)
self.kubectl_manager.kubectl_cordon_cache.remove(event)
else:
print_(f"Starting the scheduled event {event.eventid}.", eventid=event.eventid)
response = self.scheduled_events_manager.start_an_event(event)
print_(f"Started the scheduled event {event.eventid}, response='{response}'.", eventid=event.eventid)
# Manager that takes care of graceful shutdown.
class LifeManager:
def __init__(self, delay_before_program_close_seconds: int) -> None:
super().__init__()
self.delay_before_program_close_seconds: int = delay_before_program_close_seconds
self.exit_threading_event: threading.Event = threading.Event()
self.signal_counters: Dict[str, int] = collections.defaultdict(int)
for some_signal in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]:
signal.signal(some_signal, self.signals_handler)
def signals_handler(self, signal_number: int, current_stack_frame: Any):
signal_name = signal.Signals(signal_number).name
self.signal_counters[signal_name] += 1
if self.signal_counters[signal_name] == 1:
print_(f"Program has been interrupted by the {signal_name}, graceful shutdown in progress.")
# Give some time to external monitoring to collect logs.
time.sleep(self.delay_before_program_close_seconds)
self.exit_threading_event.set()
else:
print_(f"Program has been interrupted by the {signal_name} again, forced shutdown in progress.")
sys.exit(-1)
def main():
try:
print_("The operator started working.")
config_file_path = sys.argv[1] if len(sys.argv) > 1 else "/no/custom/config"
cache_dir = sys.argv[2] if len(sys.argv) > 2 else "/do/not/store"
# Disable automatic proxy server detection.
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service?tabs=linux#proxies
# https://docs.python.org/3.9/howto/urllib2.html#proxies
proxy_support = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
# Initializing helper classes.
config = Config(config_file_path,
cache_dir)
life_manager = LifeManager(config.delay_before_program_close_seconds)
this_hostnames = ThisHostnames(config.api_metadata_instance,
config.socket_timeout_seconds)
scheduled_events_manager = ScheduledEventsManager(config.api_metadata_scheduledevents,
config.socket_timeout_seconds,
config.delay_before_program_close_seconds)
kubectl_manager = KubectlManager(config.cache_dir,
config.kubectl_drain_options,
this_hostnames)
processing_rules_processor = ProcessingRuleProcessor(config.processing_rules,
this_hostnames)
operator = Seoperator2(config.cache_dir,
processing_rules_processor,
this_hostnames,
scheduled_events_manager,
kubectl_manager,
config.delay_before_uncordon_seconds)
# Checking the environment.
app_version = datetime.fromtimestamp(os.path.getmtime(__file__)).astimezone().isoformat()
sys_version = sys.version_info
kubectl_version = KubectlManager.kubectl_version()
print_("The operator has been initialized.",
app_version=app_version,
sys_version=sys_version,
kubectl_version=kubectl_version,
config=config,
this_hostnames=this_hostnames,
already_processed_events=operator.already_processed_events,
kubectl_cordon_cache=kubectl_manager.kubectl_cordon_cache,
exit_threading_event_is_set=life_manager.exit_threading_event.is_set())
while True:
# print_("The operator is still working.")
data = scheduled_events_manager.query_for_events()
operator.handle_scheduled_events(data)
if life_manager.exit_threading_event.wait(config.polling_frequency_seconds):
break
except BaseException as e:
traceback_formatted = str(traceback.format_exc())
print_(f"There was a fatal error in my main loop, {e.__class__.__name__}.", traceback=traceback_formatted)
sys.exit(1)
if __name__ == "__main__":
main()