-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-runner-scaler.go
More file actions
367 lines (292 loc) · 8.14 KB
/
Copy pathgithub-runner-scaler.go
File metadata and controls
367 lines (292 loc) · 8.14 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
var httpClient = &http.Client{
Timeout: 10 * time.Second,
}
// disregards repos updated more than a year ago
const archiveThreshold = 1 * 365 * 24 * time.Hour // 1 years
// Repo represents a GitHub repository
type Repo struct {
FullName string `json:"full_name"`
UpdatedAt time.Time `json:"updated_at"`
}
// WorkflowRun represents a workflow run in a GitHub repository
type WorkflowRun struct {
Status string `json:"status"`
ID int64 `json:"id"`
}
var (
cacheLock sync.Mutex // To handle concurrency
cachedJobs int
lastUpdateTime time.Time
cacheTimeout time.Duration
)
var (
repoCacheLock sync.Mutex
cachedRepos []Repo
repoLastUpdate time.Time
repoCacheTimeout = 10 * time.Minute
)
type Job struct {
Status string `json:"status"`
Labels []string `json:"labels"`
}
// Detect whether the API is public GitHub or GitHub Enterprise, and adjust the endpoint accordingly
func buildAPIURL(baseURL, endpoint string) string {
// Check if we're using public GitHub (https://api.github.com)
if strings.HasPrefix(baseURL, "https://api.github.com") {
// Public GitHub case (no need for /api/v3)
return fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), endpoint)
}
// GitHub Enterprise case (use /api/v3)
return fmt.Sprintf("%s/api/v3/%s", strings.TrimSuffix(baseURL, "/"), endpoint)
}
// GetRepos fetches the repositories for the given organization
func GetRepos(githubURL, org, token string) ([]Repo, error) {
var allRepos []Repo
page := 1
for {
url := buildAPIURL(
githubURL,
fmt.Sprintf("orgs/%s/repos?per_page=100&page=%d", org, page),
)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching repos: %s", resp.Status)
}
var repos []Repo
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
return nil, err
}
if len(repos) == 0 {
break
}
allRepos = append(allRepos, repos...)
page++
}
return allRepos, nil
}
func GetReposCached(githubURL, org, token string) ([]Repo, error) {
repoCacheLock.Lock()
defer repoCacheLock.Unlock()
if time.Since(repoLastUpdate) < repoCacheTimeout && cachedRepos != nil {
log.Println("Returning cached repos")
return cachedRepos, nil
}
log.Println("Refreshing repo list from GitHub")
repos, err := GetRepos(githubURL, org, token)
if err != nil {
return nil, err
}
var activeRepos []Repo
cutoff := time.Now().Add(-archiveThreshold)
for _, r := range repos {
if r.UpdatedAt.After(cutoff) {
activeRepos = append(activeRepos, r)
}
}
cachedRepos = activeRepos
repoLastUpdate = time.Now()
return cachedRepos, nil
}
// GetWorkflowRuns fetches the workflow runs for a specific repository
func GetWorkflowRuns(githubURL, repo, token string) ([]WorkflowRun, error) {
var allRuns []WorkflowRun
page := 1
for {
url := buildAPIURL(
githubURL,
fmt.Sprintf("repos/%s/actions/runs?per_page=100&page=%d", repo, page),
)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching workflow runs: %s", resp.Status)
}
var response struct {
WorkflowRuns []WorkflowRun `json:"workflow_runs"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
if len(response.WorkflowRuns) == 0 {
break
}
allRuns = append(allRuns, response.WorkflowRuns...)
page++
}
return allRuns, nil
}
func GetJobsForRun(githubURL, repo string, runID int64, token string) ([]Job, error) {
var allJobs []Job
page := 1
for {
url := buildAPIURL(
githubURL,
fmt.Sprintf(
"repos/%s/actions/runs/%d/jobs?per_page=100&page=%d",
repo, runID, page,
),
)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching jobs: %s", resp.Status)
}
var response struct {
Jobs []Job `json:"jobs"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
if len(response.Jobs) == 0 {
break
}
allJobs = append(allJobs, response.Jobs...)
page++
}
return allJobs, nil
}
func matchesRunnerLabel(labels []string, runnerLabel string) bool {
if runnerLabel == "" {
return true // no filtering → all jobs count
}
for _, l := range labels {
if l == runnerLabel {
return true
}
}
return false
}
func CountQueuedJobs(githubURL, org, token, runnerLabel string) (int, error) {
repos, err := GetReposCached(githubURL, org, token)
if err != nil {
return 0, err
}
total := 0
for _, repo := range repos {
runs, err := GetWorkflowRuns(githubURL, repo.FullName, token)
if err != nil {
return 0, err
}
for _, run := range runs {
if run.Status != "queued" && run.Status != "in_progress" {
continue
}
jobs, err := GetJobsForRun(githubURL, repo.FullName, run.ID, token)
if err != nil {
return 0, err
}
for _, job := range jobs {
if job.Status == "queued" && matchesRunnerLabel(job.Labels, runnerLabel) {
total++
}
}
}
}
return total, nil
}
// API handler to expose the queued jobs count with caching
func QueuedJobsHandler(w http.ResponseWriter, r *http.Request) {
githubURL := os.Getenv("GITHUB_URL")
org := os.Getenv("GITHUB_ORGANIZATION")
token := os.Getenv("GITHUB_TOKEN")
if githubURL == "" {
githubURL = "https://api.github.com"
}
cacheLock.Lock()
defer cacheLock.Unlock()
// Check if the cache is still valid
if time.Since(lastUpdateTime) < cacheTimeout {
log.Println("Returning cached result")
response := map[string]int{
"queued_jobs": cachedJobs,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
// Otherwise, compute the queued jobs and update the cache
runnerLabel := os.Getenv("GITHUB_RUNNER_LABEL") // optional env variable
if runnerLabel == "" {
runnerLabel = "" // optional: default is empty = count all jobs
}
queuedJobs, err := CountQueuedJobs(githubURL, org, token, runnerLabel)
if err != nil {
http.Error(w, fmt.Sprintf("error counting queued jobs: %v", err), http.StatusInternalServerError)
return
}
// Update cache
cachedJobs = queuedJobs
lastUpdateTime = time.Now()
// Respond with the updated count in JSON format
response := map[string]int{
"queued_jobs": queuedJobs,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
// Set cache timeout from the environment variable
timeoutStr := os.Getenv("GITHUB_RUNNER_SCALER_CACHE_TIMEOUT")
if timeoutStr == "" {
timeoutStr = "60" // Default cache timeout is 60 seconds
}
timeout, err := strconv.Atoi(timeoutStr)
if err != nil {
log.Printf("Invalid GITHUB_RUNNER_SCALER_CACHE_TIMEOUT: %v - using default", err)
timeout = 60
}
cacheTimeout = time.Duration(timeout) * time.Second
// Set up the HTTP server and route
http.HandleFunc("/queued_jobs", QueuedJobsHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Starting server on port %s with cache timeout of %d seconds...", port, timeout)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}