Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit d17f441

Browse files
npalmnavdeepg2021
andcommitted
feat(webhook): Support multiple arrays of tags is matchers. (#2736)
* feat(multi-runner): Support multiple arrays of tags in matchers * Run ci for webhook * fix terraform type * fix formatting * fix terraform type * fix terraform type * fix: some fixes. * fix: readme. * fix example workflow labes * add test for multiple label match * add test for multiple label match Co-authored-by: navdeepg2021 <[email protected]>
1 parent 12a403d commit d17f441

File tree

9 files changed

+82
-40
lines changed

9 files changed

+82
-40
lines changed

Diff for: examples/multi-runner/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This module shows how to create GitHub action runners with multiple runner configuration together in one deployment. This example has the configurations for the following runner types with the relevant labels supported by them as matchers:
44

55
- Linux ARM64 `["self-hosted", "linux", "arm64", "amazon"]`
6-
- Linux Ubuntu `["self-hosted", "linux", "x64", "ubuntu"]`
6+
- Linux Ubuntu `["self-hosted", "linux", "x64", "ubuntu-latest"]` or `["self-hosted", "linux", "x64", "ubuntu-2204"]``
77
- Linux X64 `["self-hosted", "linux", "x64", "amazon"]`
88
- Windows X64 `["self-hosted", "windows", "x64", "servercore-2022"]`
99

Diff for: examples/multi-runner/main.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module "multi-runner" {
1212
multi_runner_config = {
1313
"linux-arm64" = {
1414
matcherConfig : {
15-
labelMatchers = ["self-hosted", "linux", "arm64", "amazon"]
15+
labelMatchers = [["self-hosted", "linux", "arm64", "amazon"]]
1616
exactMatch = true
1717
}
1818
fifo = true
@@ -33,7 +33,7 @@ module "multi-runner" {
3333
},
3434
"linux-ubuntu" = {
3535
matcherConfig : {
36-
labelMatchers = ["self-hosted", "linux", "x64", "ubuntu"]
36+
labelMatchers = [["self-hosted", "linux", "x64", "ubuntu-latest"], ["self-hosted", "linux", "x64", "ubuntu-2204"]]
3737
exactMatch = true
3838
}
3939
fifo = true
@@ -45,12 +45,12 @@ module "multi-runner" {
4545
runner_config = {
4646
runner_os = "linux"
4747
runner_architecture = "x64"
48-
runner_extra_labels = "ubuntu"
48+
runner_extra_labels = "ubuntu-latest,ubuntu-2204"
49+
runner_run_as = "ubuntu"
4950
enable_ssm_on_runners = true
5051
instance_types = ["m5ad.large", "m5a.large"]
5152
runners_maximum_count = 1
5253
scale_down_schedule_expression = "cron(* * * * ? *)"
53-
runner_run_as = "ubuntu"
5454
userdata_template = "./templates/user-data.sh"
5555
ami_owners = ["099720109477"] # Canonical's Amazon account ID
5656

@@ -93,7 +93,7 @@ module "multi-runner" {
9393
},
9494
"windows-x64" = {
9595
matcherConfig : {
96-
labelMatchers = ["self-hosted", "windows", "x64", "servercore-2022"]
96+
labelMatchers = [["self-hosted", "windows", "x64", "servercore-2022"]]
9797
exactMatch = true
9898
}
9999
fifo = true
@@ -114,7 +114,7 @@ module "multi-runner" {
114114
},
115115
"linux-x64" = {
116116
matcherConfig : {
117-
labelMatchers = ["self-hosted", "linux", "x64", "amazon"]
117+
labelMatchers = [["self-hosted", "linux", "x64", "amazon"]]
118118
exactMatch = false
119119
}
120120
fifo = true

Diff for: modules/multi-runner/README.md

+3-3
Large diffs are not rendered by default.

Diff for: modules/multi-runner/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ variable "multi_runner_config" {
108108
})
109109

110110
matcherConfig = object({
111-
labelMatchers = list(string)
111+
labelMatchers = list(list(string))
112112
exactMatch = optional(bool, false)
113113
})
114114
fifo = optional(bool, false)
@@ -165,7 +165,7 @@ variable "multi_runner_config" {
165165
pool_config: "The configuration for updating the pool. The `pool_size` to adjust to by the events triggered by the `schedule_expression`. For example you can configure a cron expression for week days to adjust the pool to 10 and another expression for the weekend to adjust the pool to 1."
166166
}
167167
matcherConfig: {
168-
labelMatchers: "The list of labels supported by the runner configuration."
168+
labelMatchers: "The list of list of labels supported by the runner configuration. `[[self-hosted, linux, x64, example]]`"
169169
exactMatch: "If set to true all labels in the workflow job must match the GitHub labels (os, architecture and `self-hosted`). When false if __any__ workflow label matches it will trigger the webhook."
170170
}
171171
fifo: "Enable a FIFO queue to remain the order of events received by the webhook. Suggest to set to true for repo level runners."

Diff for: modules/webhook/lambdas/webhook/src/sqs/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export interface ActionRequestMessage {
1515
}
1616

1717
export interface MatcherConfig {
18-
labelMatchers: string[];
18+
labelMatchers: string[][];
1919
exactMatch: bool;
2020
}
21+
2122
export interface QueueConfig {
2223
matcherConfig: MatcherConfig;
2324
id: string;

Diff for: modules/webhook/lambdas/webhook/src/webhook/handler.test.ts

+53-16
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ describe('handler', () => {
148148
{
149149
...queuesConfig[0],
150150
matcherConfig: {
151-
labelMatchers: ['self-hosted', 'test'],
151+
labelMatchers: [['self-hosted', 'test']],
152152
exactMatch: true,
153153
},
154154
},
155155
{
156156
...queuesConfig[1],
157157
matcherConfig: {
158-
labelMatchers: ['self-hosted', 'test1'],
158+
labelMatchers: [['self-hosted', 'test1']],
159159
exactMatch: true,
160160
},
161161
},
@@ -180,14 +180,14 @@ describe('handler', () => {
180180
{
181181
...queuesConfig[0],
182182
matcherConfig: {
183-
labelMatchers: ['linux', 'TEST', 'self-hosted'],
183+
labelMatchers: [['linux', 'TEST', 'self-hosted']],
184184
exactMatch: true,
185185
},
186186
},
187187
{
188188
...queuesConfig[1],
189189
matcherConfig: {
190-
labelMatchers: ['self-hosted', 'test1'],
190+
labelMatchers: [['self-hosted', 'test1']],
191191
exactMatch: true,
192192
},
193193
},
@@ -212,14 +212,14 @@ describe('handler', () => {
212212
{
213213
...queuesConfig[0],
214214
matcherConfig: {
215-
labelMatchers: ['self-hosted', 'test', 'test2'],
215+
labelMatchers: [['self-hosted', 'test', 'test2']],
216216
exactMatch: true,
217217
},
218218
},
219219
{
220220
...queuesConfig[1],
221221
matcherConfig: {
222-
labelMatchers: ['self-hosted', 'test1'],
222+
labelMatchers: [['self-hosted', 'test1']],
223223
exactMatch: true,
224224
},
225225
},
@@ -244,14 +244,14 @@ describe('handler', () => {
244244
{
245245
...queuesConfig[0],
246246
matcherConfig: {
247-
labelMatchers: ['self-hosted', 'x64', 'linux', 'test'],
247+
labelMatchers: [['self-hosted', 'x64', 'linux', 'test']],
248248
exactMatch: true,
249249
},
250250
},
251251
{
252252
...queuesConfig[1],
253253
matcherConfig: {
254-
labelMatchers: ['self-hosted', 'x64', 'linux', 'test1'],
254+
labelMatchers: [['self-hosted', 'x64', 'linux', 'test1']],
255255
exactMatch: true,
256256
},
257257
},
@@ -276,14 +276,14 @@ describe('handler', () => {
276276
{
277277
...queuesConfig[0],
278278
matcherConfig: {
279-
labelMatchers: ['self-hosted', 'test', 'test2'],
279+
labelMatchers: [['self-hosted', 'test', 'test2']],
280280
exactMatch: true,
281281
},
282282
},
283283
{
284284
...queuesConfig[1],
285285
matcherConfig: {
286-
labelMatchers: ['self-hosted', 'x64'],
286+
labelMatchers: [['self-hosted', 'x64']],
287287
exactMatch: false,
288288
},
289289
},
@@ -308,14 +308,14 @@ describe('handler', () => {
308308
{
309309
...queuesConfig[0],
310310
matcherConfig: {
311-
labelMatchers: ['self-hosted', 'x64', 'linux', 'test'],
311+
labelMatchers: [['self-hosted', 'x64', 'linux', 'test']],
312312
exactMatch: false,
313313
},
314314
},
315315
{
316316
...queuesConfig[1],
317317
matcherConfig: {
318-
labelMatchers: ['self-hosted', 'x64', 'linux', 'test1'],
318+
labelMatchers: [['self-hosted', 'x64', 'linux', 'test1']],
319319
exactMatch: false,
320320
},
321321
},
@@ -339,15 +339,15 @@ describe('handler', () => {
339339
{
340340
...queuesConfig[0],
341341
matcherConfig: {
342-
labelMatchers: ['self-hosted'],
342+
labelMatchers: [['self-hosted']],
343343
exactMatch: false,
344344
},
345345
id: 'ubuntu-queue-id',
346346
},
347347
{
348348
...queuesConfig[1],
349349
matcherConfig: {
350-
labelMatchers: ['self-hosted'],
350+
labelMatchers: [['self-hosted']],
351351
exactMatch: false,
352352
},
353353
id: 'default-queue-id',
@@ -380,15 +380,15 @@ describe('handler', () => {
380380
{
381381
...queuesConfig[0],
382382
matcherConfig: {
383-
labelMatchers: ['self-hosted'],
383+
labelMatchers: [['self-hosted']],
384384
exactMatch: false,
385385
},
386386
id: 'ubuntu-queue-id',
387387
},
388388
{
389389
...queuesConfig[1],
390390
matcherConfig: {
391-
labelMatchers: ['self-hosted'],
391+
labelMatchers: [['self-hosted']],
392392
exactMatch: false,
393393
},
394394
id: 'default-queue-id',
@@ -418,6 +418,43 @@ describe('handler', () => {
418418
});
419419
});
420420

421+
it('Check webhook will accept jobs when matchers accepts multiple labels.', async () => {
422+
process.env.RUNNER_CONFIG = JSON.stringify([
423+
{
424+
...queuesConfig[0],
425+
matcherConfig: {
426+
labelMatchers: [
427+
['self-hosted', 'arm64', 'linux', 'ubuntu-latest'],
428+
['self-hosted', 'arm64', 'linux', 'ubuntu-2204'],
429+
],
430+
exactMatch: false,
431+
},
432+
id: 'ubuntu-queue-id',
433+
},
434+
]);
435+
const event = JSON.stringify({
436+
...workflowjob_event,
437+
workflow_job: {
438+
...workflowjob_event.workflow_job,
439+
labels: ['self-hosted', 'linux', 'arm64', 'ubuntu-latest'],
440+
},
441+
});
442+
const resp = await handle(
443+
{ 'X-Hub-Signature': await webhooks.sign(event), 'X-GitHub-Event': 'workflow_job' },
444+
event,
445+
);
446+
expect(resp.statusCode).toBe(201);
447+
expect(sendActionRequest).toBeCalledWith({
448+
id: workflowjob_event.workflow_job.id,
449+
repositoryName: workflowjob_event.repository.name,
450+
repositoryOwner: workflowjob_event.repository.owner.login,
451+
eventType: 'workflow_job',
452+
installationId: 0,
453+
queueId: 'ubuntu-queue-id',
454+
queueFifo: false,
455+
});
456+
});
457+
421458
describe('Test for check_run is ignored.', () => {
422459
it('handles check_run events', async () => {
423460
const event = JSON.stringify(checkrun_event);

Diff for: modules/webhook/lambdas/webhook/src/webhook/handler.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,22 @@ function isRepoNotAllowed(repoFullName: string, repositoryWhiteList: string[]):
161161
return repositoryWhiteList.length > 0 && !repositoryWhiteList.includes(repoFullName);
162162
}
163163

164-
function canRunJob(workflowJobLabels: string[], runnerLabels: string[], workflowLabelCheckAll: boolean): boolean {
165-
runnerLabels = runnerLabels.map((element) => {
166-
return element.toLowerCase();
164+
function canRunJob(
165+
workflowJobLabels: string[],
166+
runnerLabelsMatchers: string[][],
167+
workflowLabelCheckAll: boolean,
168+
): boolean {
169+
runnerLabelsMatchers = runnerLabelsMatchers.map((runnerLabel) => {
170+
return runnerLabel.map((label) => label.toLowerCase());
167171
});
168172
const match = workflowLabelCheckAll
169-
? workflowJobLabels.every((l) => runnerLabels.includes(l.toLowerCase()))
170-
: workflowJobLabels.some((l) => runnerLabels.includes(l.toLowerCase()));
173+
? workflowJobLabels.every((wl) => runnerLabelsMatchers.some((rl) => rl.includes(wl.toLowerCase())))
174+
: workflowJobLabels.some((wl) => runnerLabelsMatchers.some((rl) => rl.includes(wl.toLowerCase())));
171175

172176
logger.debug(
173177
`Received workflow job event with labels: '${JSON.stringify(workflowJobLabels)}'. The event does ${
174178
match ? '' : 'NOT '
175-
}match the runner labels: '${Array.from(runnerLabels).join(',')}'`,
179+
}match the runner labels: '${Array.from(runnerLabelsMatchers).join(',')}'`,
176180
LogFields.print(),
177181
);
178182
return match;

Diff for: modules/webhook/lambdas/webhook/test/resources/multi_runner_configurations.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"arn": "queueARN",
55
"fifo": false,
66
"matcherConfig": {
7-
"labelMatchers": [
7+
"labelMatchers": [[
88
"self-hosted",
99
"linux",
1010
"x64",
1111
"ubuntu"
12-
],
12+
]],
1313
"exactMatch": true
1414
}
1515
},
@@ -18,12 +18,12 @@
1818
"arn": "queueARN",
1919
"fifo": false,
2020
"matcherConfig": {
21-
"labelMatchers": [
21+
"labelMatchers": [[
2222
"self-hosted",
2323
"linux",
2424
"x64",
2525
"latest"
26-
],
26+
]],
2727
"exactMatch": false
2828
}
2929
}

Diff for: modules/webhook/variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ variable "runner_config" {
2828
id = string
2929
fifo = bool
3030
matcherConfig = object({
31-
labelMatchers = list(string)
31+
labelMatchers = list(list(string))
3232
exactMatch = bool
3333
})
3434
}))

0 commit comments

Comments
 (0)