-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add spot termination handler (#4176)
## Description This PR adds a lambda to to log and metric spot termination based on the cloudtrail event `BidEvictedEvent`. The feature is experimental and disabled by default. ## Future directions The current implemenation only helps to make spot termination visible to an admin team. For the future we want to enrich a runner with information via tagging what job is active, This allows to let the termination handler also inform the user by adding a job annotation once a spot termination occurs. ## Migration No migration is required. By default the watcher is disabled. - logging for the watcher is changed - resources will be recreated for notification warning watcher --------- Co-authored-by: philips-labs-pr|bot <philips-labs-pr[bot]@users.noreply.github.com> Co-authored-by: Stuart Pearson <[email protected]>
- Loading branch information
1 parent
3fb1729
commit 8ba0a82
Showing
35 changed files
with
861 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { EC2Client, DescribeInstancesCommand, DescribeInstancesResult } from '@aws-sdk/client-ec2'; | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import { getInstances, tagFilter } from './ec2'; | ||
|
||
const ec2Mock = mockClient(EC2Client); | ||
|
||
describe('getInstances', () => { | ||
beforeEach(() => { | ||
ec2Mock.reset(); | ||
}); | ||
|
||
it('should return the instance when found', async () => { | ||
const instanceId = 'i-1234567890abcdef0'; | ||
const instance = { InstanceId: instanceId }; | ||
ec2Mock.on(DescribeInstancesCommand).resolves({ | ||
Reservations: [{ Instances: [instance] }], | ||
}); | ||
|
||
const result = await getInstances(new EC2Client({}), [instanceId]); | ||
expect(result).toEqual([instance]); | ||
}); | ||
|
||
describe('should return null when the instance is not found', () => { | ||
it.each([{ Reservations: [] }, {}, { Reservations: undefined }])( | ||
'with %p', | ||
async (item: DescribeInstancesResult) => { | ||
const instanceId = 'i-1234567890abcdef0'; | ||
ec2Mock.on(DescribeInstancesCommand).resolves(item); | ||
|
||
const result = await getInstances(new EC2Client({}), [instanceId]); | ||
expect(result).toEqual([]); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
describe('tagFilter', () => { | ||
describe('should return true when the instance matches the tag filters', () => { | ||
it.each([{ Environment: 'production' }, { Environment: 'prod' }])( | ||
'with %p', | ||
(tagFilters: Record<string, string>) => { | ||
const instance = { | ||
Tags: [ | ||
{ Key: 'Name', Value: 'test-instance' }, | ||
{ Key: 'Environment', Value: 'production' }, | ||
], | ||
}; | ||
|
||
const result = tagFilter(instance, tagFilters); | ||
expect(result).toBe(true); | ||
}, | ||
); | ||
}); | ||
|
||
it('should return false when the instance does not have all the tags', () => { | ||
const instance = { | ||
Tags: [{ Key: 'Name', Value: 'test-instance' }], | ||
}; | ||
const tagFilters = { Name: 'test', Environment: 'prod' }; | ||
|
||
const result = tagFilter(instance, tagFilters); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when the instance does not have any tags', () => { | ||
const instance = {}; | ||
const tagFilters = { Name: 'test', Environment: 'prod' }; | ||
|
||
const result = tagFilter(instance, tagFilters); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true if the tag filters are empty', () => { | ||
const instance = { | ||
Tags: [ | ||
{ Key: 'Name', Value: 'test-instance' }, | ||
{ Key: 'Environment', Value: 'production' }, | ||
], | ||
}; | ||
const tagFilters = {}; | ||
|
||
const result = tagFilter(instance, tagFilters); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if instance is null', () => { | ||
const instance = null; | ||
const tagFilters = { Name: 'test', Environment: 'prod' }; | ||
|
||
const result = tagFilter(instance, tagFilters); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { DescribeInstancesCommand, EC2Client, Instance } from '@aws-sdk/client-ec2'; | ||
|
||
export async function getInstances(ec2: EC2Client, instanceId: string[]): Promise<Instance[]> { | ||
const result = await ec2.send(new DescribeInstancesCommand({ InstanceIds: instanceId })); | ||
const instances = result.Reservations?.[0]?.Instances; | ||
return instances ?? []; | ||
} | ||
|
||
export function tagFilter(instance: Instance | null, tagFilters: Record<string, string>): boolean { | ||
return Object.keys(tagFilters).every((key) => { | ||
return instance?.Tags?.find((tag) => tag.Key === key && tag.Value?.startsWith(tagFilters[key])); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.