-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.py
More file actions
176 lines (136 loc) · 4.24 KB
/
Copy pathcontrol.py
File metadata and controls
176 lines (136 loc) · 4.24 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
#!/usr/bin/env python3
import json
import logging
import os
import subprocess
import threading
import time
from flask import Flask, jsonify
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger("concurrency")
POLL_INTERVAL = int(os.environ.get("POLL_INTERVAL", "30"))
NAMESPACE = os.environ.get("NAMESPACE", "")
MACHINE_LABELS = ("builder", "tester")
PORT = int(os.environ.get("PORT", "8080"))
state = {
"busy_machines": [],
"pending": [],
"running": [],
"last_reconcile": None,
}
app = Flask(__name__)
def oc(*args):
cmd = ["oc"]
if NAMESPACE:
cmd += ["-n", NAMESPACE]
cmd += list(args)
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
log.error("oc %s failed: %s", " ".join(args), result.stderr.strip())
return None
return result.stdout
def get_pipelineruns():
out = oc("get", "pipelinerun", "-o", "json")
if not out:
return []
return json.loads(out).get("items", [])
def is_pending(pr):
return pr.get("spec", {}).get("status", "") == "PipelineRunPending"
def is_running(pr):
spec_status = pr.get("spec", {}).get("status", "")
if spec_status == "PipelineRunPending":
return False
conditions = pr.get("status", {}).get("conditions", [])
for c in conditions:
if c.get("type") == "Succeeded":
status = c.get("status", "")
reason = c.get("reason", "")
if status == "Unknown" and reason in ("Running", "Started", "Pending"):
return True
return False
def get_machine_labels(pr):
labels = pr.get("metadata", {}).get("labels", {})
machines = set()
for key in MACHINE_LABELS:
val = labels.get(key, "")
if val:
machines.add(val)
return machines
def get_busy_machines(pipelineruns):
busy = set()
for pr in pipelineruns:
if is_running(pr):
busy.update(get_machine_labels(pr))
return busy
def start_pipelinerun(name):
patch = json.dumps([{"op": "remove", "path": "/spec/status"}])
out = oc(
"patch", "pipelinerun", name,
"--type=json",
f"-p={patch}",
)
if out is not None:
log.info("Started PipelineRun %s", name)
return True
log.error("Failed to start PipelineRun %s", name)
return False
def pr_name(pr):
return pr["metadata"]["name"]
def reconcile():
pipelineruns = get_pipelineruns()
if not pipelineruns:
return
busy = get_busy_machines(pipelineruns)
pending = [pr for pr in pipelineruns if is_pending(pr)]
running = [pr for pr in pipelineruns if is_running(pr)]
state["busy_machines"] = sorted(busy)
state["pending"] = [pr_name(pr) for pr in pending]
state["running"] = [pr_name(pr) for pr in running]
state["last_reconcile"] = time.strftime("%Y-%m-%d %H:%M:%S")
if busy:
log.info("Busy machines: %s", ", ".join(sorted(busy)))
if not pending:
log.debug("No pending PipelineRuns")
return
log.info("Pending PipelineRuns: %d", len(pending))
for pr in pending:
name = pr_name(pr)
required = get_machine_labels(pr)
if not required:
log.info("%s has no machine labels, starting it", name)
start_pipelinerun(name)
continue
conflict = required & busy
if conflict:
log.info(
"%s waiting on busy machine(s): %s",
name, ", ".join(sorted(conflict)),
)
continue
start_pipelinerun(name)
busy.update(required)
def controller_loop():
log.info(
"Concurrency controller starting (poll=%ds, namespace=%s)",
POLL_INTERVAL, NAMESPACE or "<current>",
)
while True:
try:
reconcile()
except Exception:
log.exception("Error during reconcile")
time.sleep(POLL_INTERVAL)
@app.route("/healthz")
def healthz():
return jsonify({"status": "ok"})
@app.route("/status")
def status():
return jsonify(state)
if __name__ == "__main__":
t = threading.Thread(target=controller_loop, daemon=True)
t.start()
app.run(host="0.0.0.0", port=PORT)