-
Notifications
You must be signed in to change notification settings - Fork 13
/
liveness.py
70 lines (55 loc) · 2.12 KB
/
liveness.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
#!/usr/bin/env python3
# thoth-adviser
# Copyright(C) 2019 - 2021 Fridolin Pokorny
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""A liveness probe run in OpenShift.
This liveness probe is run by OpenShift in a deployment where adviser computes
results. It's intention is to:
1. Notify about soon-to-terminate pod by using SIGUSR1 - adviser's predictor
should wrap up work and perform exploitation phase.
2. Send SIGINT to notify resolver to stop the resolution and report results
computed so far.
This Python script *HAS TO* be run in a container as it kills all the processes
except the main process (PID 1).
"""
import sys
import os
import signal
from pathlib import Path
# Create this file on kill for better reports in adviser logs.
_LIVENESS_PROBE_KILL_FILE = "/tmp/thoth_adviser_cpu_timeout"
def main() -> int:
"""Kill all processes except for main."""
pids = [int(pid) for pid in os.listdir("/proc") if pid.isdigit()]
liveness_file = Path(_LIVENESS_PROBE_KILL_FILE)
if liveness_file.exists():
signal_num = signal.SIGINT
else:
liveness_file.touch()
signal_num = signal.SIGUSR1
for pid in pids:
if pid == 1:
# No attempt to kill main process.
continue
if os.getpid() == pid:
# Do not commit suicide.
continue
print("Killing process with PID %d with %d" % (pid, signal_num))
os.kill(pid, signal_num)
# Let liveness probe always fail with timeout.
signal.pause()
return 1
if __name__ == "__main__":
sys.exit(main())