forked from jaypoulz/release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_prow.py
More file actions
executable file
·214 lines (191 loc) · 8.68 KB
/
monitor_prow.py
File metadata and controls
executable file
·214 lines (191 loc) · 8.68 KB
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
#!/bin/env python3
import glob
import multiprocessing.dummy as multiprocessing
import subprocess
import sys
import tempfile
import time
import json
import os
exec_cmd = lambda *cmd: subprocess.check_output(cmd).decode('utf-8')
RED = exec_cmd('tput', 'setaf', '1')
GREEN = exec_cmd('tput', 'setaf', '2')
YELLOW = exec_cmd('tput', 'setaf', '3')
BOLD = exec_cmd('tput', 'bold')
RESET = exec_cmd('tput', 'sgr0')
CLEAR = exec_cmd('tput', 'clear')
BLACKLIST = [
"Failed to GET .",
"The following repos define a policy or require context",
"requested job is unknown to prow: rehearse",
"requested job is unknown to prow: promote",
"Not enough reviewers found in OWNERS files for files touched by this PR",
"failed to get path: failed to resolve sym link: failed to read",
"nil pointer evaluating *v1.Refs.Repo",
"unrecognized directory name (expected int64)",
"failed to get reader for GCS object: storage: object doesn't exist",
"failed to get reader for GCS object: storage: object doesn't exist",
"googleapi: Error 401: Anonymous caller does not have storage.objects.list access to origin-ci-private., required",
"has required jobs but has protect: false",
"Couldn't find/suggest approvers for each files.",
"remote error: upload-pack: not our ref",
"fatal: remote error: upload-pack: not our ref",
"Error getting ProwJob name for source",
"the cache is not started, can not read objects",
"owner mismatch request by",
"Get : unsupported protocol scheme",
"No available resource",
"context deadline exceeded",
"owner mismatch request by ci-op"
]
def run_oc(args):
command = ['oc', '--context', 'app.ci', '--as', 'system:admin', '--loglevel', '3', '--namespace', 'ci'] + args
try:
process = subprocess.run(command, capture_output=True, check=True)
except subprocess.CalledProcessError as exc:
print(exc.stderr.decode('utf-8'))
raise
return process.stdout.decode('utf-8')
def debug(msg):
if os.environ.get("DEBUG", "") == "true":
print(msg)
def main():
dcs = run_oc(['get', 'deployment', '--output', 'go-template={{ range .items }}{{ $notfirst := false}}{{ range $k, $v := .spec.template.metadata.labels}}{{ if $notfirst}},{{end}}{{$k}}={{$v}}{{$notfirst = true}}{{end}} {{end}}']).split()
with tempfile.TemporaryDirectory() as log_dir:
fs = [(display, log_dir), *((highlight, log_dir, x) for x in dcs)]
with multiprocessing.Pool(len(fs)) as pool:
for _ in pool.imap_unordered(lambda x: x[0](*x[1:]), fs):
pass # a check for exceptions is implicit in the iteration
def display(log_dir):
logs = log_dir + '/*.log'
while True:
sys.stdout.write(CLEAR)
for log in sorted(glob.glob(logs)):
with open(log, encoding='utf-8') as f:
if sys.stdout.write(f.read()):
sys.stdout.write('\n')
time.sleep(5)
def highlight(log_dir, dc):
warn = '"level":"warning"'
error = '"level":"error"'
fatal = '"level":"fatal"'
log = f'{log_dir}/{dc}.log'
while True:
debug(f'deployment/{dc}: gathering info')
header = renderHeader(dc)
lines = []
log_lines = []
for pod in run_oc(['get', 'pods', '--selector', dc, '--output', 'jsonpath={.items[*].metadata.name}']).split():
debug(f'deployment/{dc}: pod/{pod}: gathering info')
lines.extend(renderFlavor(pod, dc))
cmd = ['logs', '--since', '20m', f'pod/{pod}']
if "deck-internal" in dc:
cmd += ['--container', 'deck']
if "component=boskos" in dc and not "-" in dc:
cmd += ['--container', 'boskos']
if "release-controller" in dc and "priv" in dc:
cmd += ['--container', 'controller']
debug(f'deployment/{dc}: pod/{pod}: getting logs')
try:
for l in run_oc(cmd).splitlines():
if any(word in l for word in BLACKLIST):
continue
for marker, key in {
'"query":"': "query",
'"error":"query ': "error"
}.items():
if marker in l:
data = json.loads(l)
data.pop(key)
l = json.dumps(data)
if warn in l:
log_lines.append(YELLOW + l + RESET)
elif error in l or fatal in l:
log_lines.append(RED + l + RESET)
except subprocess.CalledProcessError:
debug(f'deployment/{dc}: pod/{pod}: getting logs failed')
if not log_lines and not lines:
header = f'{header} {GREEN}OK{RESET}'
with open(log, 'w', encoding='utf-8') as f:
f.write('\n'.join([header, *lines, *log_lines[-5:]]))
time.sleep(60)
def renderHeader(dc):
debug(f'deployment/{dc}: rendering header')
name = run_oc(["get", "deployment", "-l", dc, "-o", "name"]).strip()
if name == "":
# Surprise: Deployments may not match their .spec.labelSelector
replicaset = run_oc(["get", "replicaset", "-l", dc, "-o", 'go-template={{ $wtfWhyNoBreakInTemplate := ""}}{{ range .items }}{{ $wtfWhyNoBreakInTemplate = .metadata.name}}{{end}}{{$wtfWhyNoBreakInTemplate}}'.strip()])
if replicaset != "":
name = json.loads(run_oc(["get", "replicaset", replicaset, "-o", "json"])).get("metadata", {}).get("ownerReferences", [])[0].get("name", "")
name = "deployment/" + name
if name != "":
rawdc = json.loads(run_oc(['get', name.rstrip(), '--output', 'json']))
else:
rawdc = {}
spec = rawdc.get("spec", {})
status = rawdc.get("status", {})
desired = spec.get("replicas", 0)
current = status.get("replicas", 0)
updated = status.get("updatedReplicas", 0)
available = status.get("availableReplicas", 0)
version = "<unknown-version>"
containers = spec.get("template", {}).get("spec", {}).get("containers", [])
if "deck-internal" in name:
container_name = "deck"
elif "priv" in dc and "release-controller" in dc:
container_name = "controller"
elif len(containers) == 1:
container_name = containers[0].get("name", "")
else:
container_name = dc
debug(f'deployment/{dc}: got container name {container_name}')
for container in containers:
if container.get("name") == container_name:
image = container.get("image", "")
version = image.split(":")[-1]
headerColor = ''
if desired != current:
headerColor = RED
message = f'{dc} at {version} [{current}/{desired}]'
if updated != desired:
message += f' ({desired - updated} stale replicas)'
if available != desired:
message += f' ({desired - available} unavailable replicas)'
header = f'{BOLD}{headerColor}{message}:{RESET}'
debug(f'deployment/{dc}: got header {header}')
return header
def renderFlavor(pod, dc):
debug(f'deployment/{dc}: pod/{pod}: rendering flavor')
lines = []
raw = json.loads(run_oc(['get', f'pod/{pod}', '--output', 'json']))
status = raw.get("status", {})
phase = status.get("phase", "")
if phase != "Running":
reason = status.get("reason", "")
message = status.get("message", "")
color = YELLOW
if phase in ["Failed", "Unknown", "CrashLoopBackOff"]:
color = RED
lines.append(color + f'pod {pod} is {phase}: {reason}, {message}')
for container in status.get("containerStatuses", []):
debug(f'pod/{pod}: handling status for container {container.get("name", "")}')
if container.get("name") == dc:
state = container.get("state", {})
if "running" not in state:
if "waiting" in state:
reason = state["waiting"].get("reason")
message = state["waiting"].get("message")
lines.append(YELLOW + f'pod {pod} is waiting: {reason}' + RESET)
lines.append(YELLOW + f'\t{message}' + RESET)
if "terminated" in state:
reason = state["terminated"].get("reason")
message = state["terminated"].get("message")
lines.append(RED + f'pod {pod} is terminated: {reason}' + RESET)
lines.append(RED + f'\t{message}' + RESET)
restartCount = container.get("restartCount", 0)
if restartCount != 0:
lines.append(RED + f'pod {pod} has restarted {restartCount} times' + RESET)
debug(f'deployment/{dc}: pod/{pod}: got flavor {lines}')
return lines
if __name__ == '__main__':
main()