Skip to content

Commit a0f7e5c

Browse files
committed
feat: terminate orphan runners in the second scale down cycle
1 parent c4a7e88 commit a0f7e5c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
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
@@ -22,6 +22,7 @@ export interface ListRunnerFilters {
2222
runnerType?: RunnerType;
2323
runnerOwner?: string;
2424
environment?: string;
25+
orphan?: boolean;
2526
statuses?: string[];
2627
}
2728

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CreateFleetCommandInput,
44
CreateFleetInstance,
55
CreateFleetResult,
6+
CreateTagsCommand,
67
DefaultTargetCapacityType,
78
DescribeInstancesCommand,
89
DescribeInstancesResult,
@@ -16,7 +17,7 @@ import { mockClient } from 'aws-sdk-client-mock';
1617
import 'aws-sdk-client-mock-jest';
1718

1819
import ScaleError from './../scale-runners/ScaleError';
19-
import { createRunner, listEC2Runners, terminateRunner } from './runners';
20+
import { createRunner, listEC2Runners, tag, terminateRunner } from './runners';
2021
import { RunnerInfo, RunnerInputParameters, RunnerType } from './runners.d';
2122

2223
process.env.AWS_REGION = 'eu-east-1';
@@ -182,6 +183,26 @@ describe('terminate runner', () => {
182183
});
183184
});
184185

186+
describe('tag runner', () => {
187+
beforeEach(() => {
188+
jest.clearAllMocks();
189+
});
190+
it('adding extra tag', async () => {
191+
mockEC2Client.on(CreateTagsCommand).resolves({});
192+
const runner: RunnerInfo = {
193+
instanceId: 'instance-2',
194+
owner: 'owner-2',
195+
type: 'Repo',
196+
};
197+
await tag(runner.instanceId, [{ Key: 'ghr:orphan', Value: 'truer' }]);
198+
199+
expect(mockEC2Client).toHaveReceivedCommandWith(CreateTagsCommand, {
200+
Resources: [runner.instanceId],
201+
Tags: [{ Key: 'ghr:orphan', Value: 'truer' }],
202+
});
203+
});
204+
});
205+
185206
describe('create runner', () => {
186207
const defaultRunnerConfig: RunnerConfig = {
187208
allocationStrategy: SpotAllocationStrategy.CAPACITY_OPTIMIZED,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {
22
CreateFleetCommand,
33
CreateFleetResult,
4+
CreateTagsCommand,
45
DescribeInstancesCommand,
56
DescribeInstancesResult,
67
EC2Client,
78
FleetLaunchTemplateOverridesRequest,
9+
Tag,
810
TerminateInstancesCommand,
911
_InstanceType,
1012
} from '@aws-sdk/client-ec2';
@@ -46,6 +48,9 @@ function constructFilters(filters?: Runners.ListRunnerFilters): Ec2Filter[][] {
4648
ec2FiltersBase.push({ Name: `tag:ghr:Type`, Values: [filters.runnerType] });
4749
ec2FiltersBase.push({ Name: `tag:ghr:Owner`, Values: [filters.runnerOwner] });
4850
}
51+
if (filters.orphan) {
52+
ec2FiltersBase.push({ Name: 'tag:ghr:orphan', Values: ['true'] });
53+
}
4954
}
5055

5156
for (const key of ['tag:ghr:Application']) {
@@ -100,6 +105,12 @@ export async function terminateRunner(instanceId: string): Promise<void> {
100105
logger.info(`Runner ${instanceId} has been terminated.`);
101106
}
102107

108+
export async function tag(instanceId: string, tags: Tag[]): Promise<void> {
109+
logger.info(`Tagging '${instanceId}'`, { tags });
110+
const ec2 = getTracedAWSV3Client(new EC2Client({ region: process.env.AWS_REGION }));
111+
await ec2.send(new CreateTagsCommand({ Resources: [instanceId], Tags: tags }));
112+
}
113+
103114
function generateFleetOverrides(
104115
subnetIds: string[],
105116
instancesTypes: string[],

0 commit comments

Comments
 (0)