-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_listener.py
174 lines (126 loc) · 4.97 KB
/
event_listener.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
from kubernetes import client, config, watch
import os
import requests
import logging
# Configure logging
logging.basicConfig(
level=logging.DEBUG, # Adjust as needed (DEBUG, ERROR, WARNING, INFO)
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
# logging.FileHandler("event_listener.log"), # Log to a file
logging.StreamHandler() # Also log to the console
]
)
STUDIO_SERVICE_NAME = os.environ.get("STUDIO_SERVICE_NAME", None)
STUDIO_SERVICE_PORT = os.environ.get("STUDIO_SERVICE_PORT", None)
APP_STATUS_ENDPOINT = os.environ.get("APP_STATUS_ENDPOINT", None)
APP_STATUSES_ENDPOINT = os.environ.get("APP_STATUSES_ENDPOINT", None)
BASE_URL = f"http://{STUDIO_SERVICE_NAME}:{STUDIO_SERVICE_PORT}"
APP_STATUS_URL = f"{BASE_URL}/{APP_STATUS_ENDPOINT}"
APP_STATUSES_URL = f"{BASE_URL}/{APP_STATUSES_ENDPOINT}"
logging.debug(f"STUDIO_SERVICE_NAME: {STUDIO_SERVICE_NAME}")
logging.debug(f"STUDIO_SERVICE_PORT: {STUDIO_SERVICE_PORT}")
logging.debug(f"APP_STATUS_ENDPOINT: {APP_STATUS_ENDPOINT}")
logging.debug(f"APP_STATUSES_ENDPOINT: {APP_STATUSES_ENDPOINT}")
K8S_STATUS_MAP = {
"CrashLoopBackOff": "Error",
"Completed": "Retrying...",
"ContainerCreating": "Created",
"PodInitializing": "Pending",
}
config.incluster_config.load_incluster_config()
api = client.CoreV1Api()
w = watch.Watch()
label_selector = "type=app"
namespace = "default"
latest_status = {}
def sync_all_statuses():
values = ""
for pod in api.list_namespaced_pod(
namespace=namespace, label_selector=label_selector
).items:
status = pod.status.phase
release = pod.metadata.labels["release"]
values += f"{release}:{status},"
data = {"values": values}
logging.debug(f"DATA: {data}")
logging.debug("Syncing all statuses...")
post(APP_STATUSES_URL, data=data)
def init_event_listener():
resource_version = ""
while True:
try:
for event in w.stream(
api.list_namespaced_pod,
namespace=namespace,
label_selector=label_selector,
timeout_seconds=20,
resource_version=resource_version,
):
pod = event["object"]
resource_version = pod.metadata.resource_version
status = get_status(pod)
logging.info(f"Synchronizing status: {status}")
# status = pod.status.phase
release = pod.metadata.labels["release"]
event_type = event["type"]
if latest_status.get(release) == status:
logging.info("Status not changed, skipping...")
latest_status[release] = status
if event_type != "DELETED":
logging.info(f"EVENT_TYPE: {event_type}")
logging.info(f"STATUS: {status}")
data = {
"release": release,
"status": status,
}
post(APP_STATUS_URL, data=data)
except client.exceptions.ApiException as e:
if e.status == 410:
# If the resource version is too old, log the issue and reset resource_version
logging.error("Resource version expired, restarting watch from latest version.")
resource_version = None
else:
logging.error(f"An error occurred: {e}")
continue
def get_status(pod):
"""
Returns the status of a pod by looping through each container
and checking the status.
"""
container_statuses = pod.status.container_statuses
if container_statuses is not None:
for container_status in container_statuses:
state = container_status.state
if state is not None:
terminated = state.terminated
if terminated is not None:
reason = terminated.reason
return mapped_status(reason)
waiting = state.waiting
if waiting is not None:
reason = waiting.reason
return mapped_status(reason)
else:
running = state.running
ready = container_status.ready
if running and ready:
return "Running"
else:
return "Pending"
return pod.status.phase
def mapped_status(reason: str) -> str:
return K8S_STATUS_MAP.get(reason, reason)
def post(url: str, data: dict):
try:
response = requests.post(url, data=data, verify=False)
logging.info(f"RESPONSE STATUS CODE: {response.status_code}")
logging.info(f"RESPONSE TEXT: {response.text}")
except requests.exceptions.RequestException as err:
logging.error(err)
logging.error("Service did not respond.")
if __name__ == "__main__":
logging.info("Starting event listener...")
sync_all_statuses()
init_event_listener()