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

Commit 66e2a66

Browse files
fix: Fix pool logic with runner name prefix (#3303)
* fix: Use runner name when determining pool runner count * fix: reduce cyclomatic complexity a little * fix: let to const * fix: Cleaner implimentation from review * fix: Formatting * fix: Add runner name prefix test * fix: Actually test prefixed names * fix: formatting * Correct test description and rename runner configuration variable --------- Co-authored-by: Navdeep Gupta <[email protected]>
1 parent 0bebeef commit 66e2a66

File tree

2 files changed

+92
-34
lines changed

2 files changed

+92
-34
lines changed

Diff for: lambdas/functions/control-plane/src/pool/pool.test.ts

+90-34
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,41 @@ const ec2InstancesRegistered = [
7272
},
7373
];
7474

75+
const githubRunnersRegistered = [
76+
{
77+
id: 1,
78+
name: 'i-1-idle',
79+
os: 'linux',
80+
status: 'online',
81+
busy: false,
82+
labels: [],
83+
},
84+
{
85+
id: 2,
86+
name: 'i-2-busy',
87+
os: 'linux',
88+
status: 'online',
89+
busy: true,
90+
labels: [],
91+
},
92+
{
93+
id: 3,
94+
name: 'i-3-offline',
95+
os: 'linux',
96+
status: 'offline',
97+
busy: false,
98+
labels: [],
99+
},
100+
{
101+
id: 3,
102+
name: 'i-4-idle-older-than-minimum-time-running',
103+
os: 'linux',
104+
status: 'online',
105+
busy: false,
106+
labels: [],
107+
},
108+
];
109+
75110
beforeEach(() => {
76111
nock.disableNetConnect();
77112
jest.resetModules();
@@ -99,40 +134,7 @@ beforeEach(() => {
99134
};
100135
mockOctokit.actions.createRegistrationTokenForOrg.mockImplementation(() => mockTokenReturnValue);
101136

102-
mockOctokit.paginate.mockImplementation(() => [
103-
{
104-
id: 1,
105-
name: 'i-1-idle',
106-
os: 'linux',
107-
status: 'online',
108-
busy: false,
109-
labels: [],
110-
},
111-
{
112-
id: 2,
113-
name: 'i-2-busy',
114-
os: 'linux',
115-
status: 'online',
116-
busy: true,
117-
labels: [],
118-
},
119-
{
120-
id: 3,
121-
name: 'i-3-offline',
122-
os: 'linux',
123-
status: 'offline',
124-
busy: false,
125-
labels: [],
126-
},
127-
{
128-
id: 3,
129-
name: 'i-4-idle-older-than-minimum-time-running',
130-
os: 'linux',
131-
status: 'online',
132-
busy: false,
133-
labels: [],
134-
},
135-
]);
137+
mockOctokit.paginate.mockImplementation(() => githubRunnersRegistered);
136138

137139
mockListRunners.mockImplementation(async () => ec2InstancesRegistered);
138140

@@ -251,4 +253,58 @@ describe('Test simple pool.', () => {
251253
);
252254
});
253255
});
256+
257+
describe('With Runner Name Prefix', () => {
258+
beforeEach(() => {
259+
process.env.RUNNER_NAME_PREFIX = 'runner-prefix_';
260+
});
261+
262+
it('Should top up with fewer runners when there are idle prefixed runners', async () => {
263+
// Add prefixed runners to github
264+
mockOctokit.paginate.mockImplementation(async () => [
265+
...githubRunnersRegistered,
266+
{
267+
id: 5,
268+
name: 'runner-prefix_i-5-idle',
269+
os: 'linux',
270+
status: 'online',
271+
busy: false,
272+
labels: [],
273+
},
274+
{
275+
id: 6,
276+
name: 'runner-prefix_i-6-idle',
277+
os: 'linux',
278+
status: 'online',
279+
busy: false,
280+
labels: [],
281+
},
282+
]);
283+
284+
// Add instances in ec2
285+
mockListRunners.mockImplementation(async () => [
286+
...ec2InstancesRegistered,
287+
{
288+
instanceId: 'i-5-idle',
289+
launchTime: new Date(),
290+
type: 'Org',
291+
owner: ORG,
292+
},
293+
{
294+
instanceId: 'i-6-idle',
295+
launchTime: new Date(),
296+
type: 'Org',
297+
owner: ORG,
298+
},
299+
]);
300+
301+
await expect(await adjust({ poolSize: 5 })).resolves;
302+
// 2 idle, 2 prefixed idle top up with 1 to match a pool of 5
303+
expect(createRunners).toHaveBeenCalledWith(
304+
expect.anything(),
305+
expect.objectContaining({ numberOfRunners: 1 }),
306+
expect.anything(),
307+
);
308+
});
309+
});
254310
});

Diff for: lambdas/functions/control-plane/src/pool/pool.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export async function adjust(event: PoolEvent): Promise<void> {
2020
logger.info(`Checking current pool size against pool of size: ${event.poolSize}`);
2121
const runnerExtraLabels = process.env.RUNNER_EXTRA_LABELS;
2222
const runnerGroup = process.env.RUNNER_GROUP_NAME;
23+
const runnerNamePrefix = process.env.RUNNER_NAME_PREFIX;
2324
const environment = process.env.ENVIRONMENT;
2425
const ghesBaseUrl = process.env.GHES_URL;
2526
const ssmTokenPath = process.env.SSM_TOKEN_PATH;
@@ -53,6 +54,7 @@ export async function adjust(event: PoolEvent): Promise<void> {
5354
);
5455
const runnerStatus = new Map<string, RunnerStatus>();
5556
for (const runner of runners) {
57+
runner.name = runnerNamePrefix ? runner.name.replace(runnerNamePrefix, '') : runner.name;
5658
runnerStatus.set(runner.name, { busy: runner.busy, status: runner.status });
5759
}
5860

0 commit comments

Comments
 (0)