Skip to content

Commit 9582e4d

Browse files
stuartp44Brend-Smits
authored andcommitted
feat(runner): add source parameter to distinguish between scale-up and pool lambda
1 parent 07bd193 commit 9582e4d

File tree

7 files changed

+87
-6
lines changed

7 files changed

+87
-6
lines changed

lambdas/functions/control-plane/src/aws/runners.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface RunnerInputParameters {
4242
instanceAllocationStrategy: SpotAllocationStrategy;
4343
};
4444
numberOfRunners: number;
45+
source: 'scale-up-lambda' | 'pool-lambda';
4546
amiIdSsmParameterName?: string;
4647
tracingEnabled?: boolean;
4748
onDemandFailoverOnError?: string[];

lambdas/functions/control-plane/src/aws/runners.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,16 @@ describe('create runner', () => {
318318
allocationStrategy: SpotAllocationStrategy.CAPACITY_OPTIMIZED,
319319
capacityType: 'spot',
320320
type: 'Org',
321+
scaleErrors: [],
322+
source: 'scale-up-lambda',
321323
};
322324

323325
const defaultExpectedFleetRequestValues: ExpectedFleetRequestValues = {
324326
type: 'Org',
325327
capacityType: 'spot',
326328
allocationStrategy: SpotAllocationStrategy.CAPACITY_OPTIMIZED,
327329
totalTargetCapacity: 1,
330+
source: 'scale-up-lambda',
328331
};
329332

330333
beforeEach(() => {
@@ -365,6 +368,25 @@ describe('create runner', () => {
365368
});
366369
});
367370

371+
it('calls create fleet of multiple instances with pool-lambda source when specified', async () => {
372+
const instances = [{ InstanceIds: ['i-1234', 'i-5678', 'i-9012'] }];
373+
374+
mockEC2Client.on(CreateFleetCommand).resolves({ Instances: instances });
375+
376+
await createRunner({
377+
...createRunnerConfig({ ...defaultRunnerConfig, source: 'pool-lambda' }),
378+
numberOfRunners: 3,
379+
});
380+
381+
expect(mockEC2Client).toHaveReceivedCommandWith(CreateFleetCommand, {
382+
...expectedCreateFleetRequest({
383+
...defaultExpectedFleetRequestValues,
384+
totalTargetCapacity: 3,
385+
source: 'pool-lambda',
386+
}),
387+
});
388+
});
389+
368390
it('calls create fleet of 1 instance with the on-demand capacity', async () => {
369391
await createRunner(createRunnerConfig({ ...defaultRunnerConfig, capacityType: 'on-demand' }));
370392
expect(mockEC2Client).toHaveReceivedCommandWith(CreateFleetCommand, {
@@ -425,6 +447,28 @@ describe('create runner', () => {
425447
}),
426448
});
427449
});
450+
451+
it('calls create fleet with source set to scale-up-lambda when source is specified', async () => {
452+
await createRunner(createRunnerConfig({ ...defaultRunnerConfig, source: 'scale-up-lambda' }));
453+
454+
expect(mockEC2Client).toHaveReceivedCommandWith(CreateFleetCommand, {
455+
...expectedCreateFleetRequest({
456+
...defaultExpectedFleetRequestValues,
457+
source: 'scale-up-lambda',
458+
}),
459+
});
460+
});
461+
462+
it('calls create fleet with source set to pool-lambda when source is specified', async () => {
463+
await createRunner(createRunnerConfig({ ...defaultRunnerConfig, source: 'pool-lambda' }));
464+
465+
expect(mockEC2Client).toHaveReceivedCommandWith(CreateFleetCommand, {
466+
...expectedCreateFleetRequest({
467+
...defaultExpectedFleetRequestValues,
468+
source: 'pool-lambda',
469+
}),
470+
});
471+
});
428472
});
429473

430474
describe('create runner with errors', () => {
@@ -433,12 +477,14 @@ describe('create runner with errors', () => {
433477
capacityType: 'spot',
434478
type: 'Repo',
435479
scaleErrors: ['UnfulfillableCapacity', 'MaxSpotInstanceCountExceeded'],
480+
source: 'scale-up-lambda',
436481
};
437482
const defaultExpectedFleetRequestValues: ExpectedFleetRequestValues = {
438483
type: 'Repo',
439484
capacityType: 'spot',
440485
allocationStrategy: SpotAllocationStrategy.CAPACITY_OPTIMIZED,
441486
totalTargetCapacity: 1,
487+
source: 'scale-up-lambda',
442488
};
443489
beforeEach(() => {
444490
vi.clearAllMocks();
@@ -546,12 +592,15 @@ describe('create runner with errors fail over to OnDemand', () => {
546592
capacityType: 'spot',
547593
type: 'Repo',
548594
onDemandFailoverOnError: ['InsufficientInstanceCapacity'],
595+
scaleErrors: [],
596+
source: 'scale-up-lambda',
549597
};
550598
const defaultExpectedFleetRequestValues: ExpectedFleetRequestValues = {
551599
type: 'Repo',
552600
capacityType: 'spot',
553601
allocationStrategy: SpotAllocationStrategy.CAPACITY_OPTIMIZED,
554602
totalTargetCapacity: 1,
603+
source: 'scale-up-lambda',
555604
};
556605
beforeEach(() => {
557606
vi.clearAllMocks();
@@ -704,6 +753,7 @@ interface RunnerConfig {
704753
tracingEnabled?: boolean;
705754
onDemandFailoverOnError?: string[];
706755
scaleErrors: string[];
756+
source: 'scale-up-lambda' | 'pool-lambda';
707757
}
708758

709759
function createRunnerConfig(runnerConfig: RunnerConfig): RunnerInputParameters {
@@ -724,6 +774,7 @@ function createRunnerConfig(runnerConfig: RunnerConfig): RunnerInputParameters {
724774
tracingEnabled: runnerConfig.tracingEnabled,
725775
onDemandFailoverOnError: runnerConfig.onDemandFailoverOnError,
726776
scaleErrors: runnerConfig.scaleErrors,
777+
source: runnerConfig.source,
727778
};
728779
}
729780

@@ -735,14 +786,15 @@ interface ExpectedFleetRequestValues {
735786
totalTargetCapacity: number;
736787
imageId?: string;
737788
tracingEnabled?: boolean;
789+
source: 'scale-up-lambda' | 'pool-lambda';
738790
}
739791

740792
function expectedCreateFleetRequest(expectedValues: ExpectedFleetRequestValues): CreateFleetCommandInput {
741793
const tags = [
742794
{ Key: 'ghr:Application', Value: 'github-action-runner' },
743795
{
744796
Key: 'ghr:created_by',
745-
Value: expectedValues.totalTargetCapacity > 1 ? 'pool-lambda' : 'scale-up-lambda',
797+
Value: expectedValues.source,
746798
},
747799
{ Key: 'ghr:Type', Value: expectedValues.type },
748800
{ Key: 'ghr:Owner', Value: REPO_NAME },

lambdas/functions/control-plane/src/aws/runners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ async function createInstances(
241241
) {
242242
const tags = [
243243
{ Key: 'ghr:Application', Value: 'github-action-runner' },
244-
{ Key: 'ghr:created_by', Value: runnerParameters.numberOfRunners === 1 ? 'scale-up-lambda' : 'pool-lambda' },
244+
{ Key: 'ghr:created_by', Value: runnerParameters.source },
245245
{ Key: 'ghr:Type', Value: runnerParameters.runnerType },
246246
{ Key: 'ghr:Owner', Value: runnerParameters.runnerOwner },
247247
];

lambdas/functions/control-plane/src/pool/pool.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,13 @@ describe('Test simple pool.', () => {
192192
it('Top up pool with pool size 2 registered.', async () => {
193193
await adjust({ poolSize: 3 });
194194
expect(createRunners).toHaveBeenCalledTimes(1);
195-
expect(createRunners).toHaveBeenCalledWith(expect.anything(), expect.anything(), 1, expect.anything());
195+
expect(createRunners).toHaveBeenCalledWith(
196+
expect.anything(),
197+
expect.anything(),
198+
1,
199+
expect.anything(),
200+
'pool-lambda',
201+
);
196202
});
197203

198204
it('Should not top up if pool size is reached.', async () => {
@@ -268,7 +274,13 @@ describe('Test simple pool.', () => {
268274
it('Top up if the pool size is set to 5', async () => {
269275
await adjust({ poolSize: 5 });
270276
// 2 idle, top up with 3 to match a pool of 5
271-
expect(createRunners).toHaveBeenCalledWith(expect.anything(), expect.anything(), 3, expect.anything());
277+
expect(createRunners).toHaveBeenCalledWith(
278+
expect.anything(),
279+
expect.anything(),
280+
3,
281+
expect.anything(),
282+
'pool-lambda',
283+
);
272284
});
273285
});
274286

@@ -283,7 +295,13 @@ describe('Test simple pool.', () => {
283295
it('Top up if the pool size is set to 5', async () => {
284296
await adjust({ poolSize: 5 });
285297
// 2 idle, top up with 3 to match a pool of 5
286-
expect(createRunners).toHaveBeenCalledWith(expect.anything(), expect.anything(), 3, expect.anything());
298+
expect(createRunners).toHaveBeenCalledWith(
299+
expect.anything(),
300+
expect.anything(),
301+
3,
302+
expect.anything(),
303+
'pool-lambda',
304+
);
287305
});
288306
});
289307

@@ -333,7 +351,13 @@ describe('Test simple pool.', () => {
333351

334352
await adjust({ poolSize: 5 });
335353
// 2 idle, 2 prefixed idle top up with 1 to match a pool of 5
336-
expect(createRunners).toHaveBeenCalledWith(expect.anything(), expect.anything(), 1, expect.anything());
354+
expect(createRunners).toHaveBeenCalledWith(
355+
expect.anything(),
356+
expect.anything(),
357+
1,
358+
expect.anything(),
359+
'pool-lambda',
360+
);
337361
});
338362
});
339363
});

lambdas/functions/control-plane/src/pool/pool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export async function adjust(event: PoolEvent): Promise<void> {
106106
},
107107
topUp,
108108
githubInstallationClient,
109+
'pool-lambda',
109110
);
110111
} else {
111112
logger.info(`Pool will not be topped up. Found ${numberOfRunnersInPool} managed idle runners.`);

lambdas/functions/control-plane/src/scale-runners/scale-up.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const EXPECTED_RUNNER_PARAMS: RunnerInputParameters = {
113113
tracingEnabled: false,
114114
onDemandFailoverOnError: [],
115115
scaleErrors: ['UnfulfillableCapacity', 'MaxSpotInstanceCountExceeded', 'TargetCapacityLimitExceededException'],
116+
source: 'scale-up-lambda',
116117
};
117118
let expectedRunnerParams: RunnerInputParameters;
118119

lambdas/functions/control-plane/src/scale-runners/scale-up.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,13 @@ export async function createRunners(
250250
ec2RunnerConfig: CreateEC2RunnerConfig,
251251
numberOfRunners: number,
252252
ghClient: Octokit,
253+
source: 'scale-up-lambda' | 'pool-lambda' = 'scale-up-lambda',
253254
): Promise<string[]> {
254255
const instances = await createRunner({
255256
runnerType: githubRunnerConfig.runnerType,
256257
runnerOwner: githubRunnerConfig.runnerOwner,
257258
numberOfRunners,
259+
source,
258260
...ec2RunnerConfig,
259261
});
260262
if (instances.length !== 0) {

0 commit comments

Comments
 (0)