-
Notifications
You must be signed in to change notification settings - Fork 660
feat: add spot termination handler #4176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2667fbf
feat: Add spot termination handler
npalm 0da9429
feat: Add spot termination handler
npalm e18aa15
feat: Add spot termination handler
npalm af45ee3
docs: auto update terraform docs
b23a367
please linter
npalm 9156524
clean
npalm 50b1d18
docs: auto update terraform docs
a0cb1b7
clean
npalm a8e2618
clean up
npalm 2cc8aef
add new variable to multi-runner
npalm 6634c81
docs: auto update terraform docs
3e457cc
Apply suggestions from code review
npalm 2bc89a2
Merge branch 'main' into npalm/feature/spot-termination-handler
npalm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.