-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(ecr): add option to auto delete images upon ECR repo removal #15932
Closed
Closed
Changes from all commits
Commits
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 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
48 changes: 48 additions & 0 deletions
48
packages/@aws-cdk/aws-ecr/lib/auto-delete-images-handler/index.ts
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,48 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { ECR } from 'aws-sdk'; | ||
|
||
const ecr = new ECR(); | ||
|
||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) { | ||
switch (event.RequestType) { | ||
case 'Create': | ||
case 'Update': | ||
return; | ||
case 'Delete': | ||
return onDelete(event); | ||
} | ||
} | ||
|
||
/** | ||
* Recursively delete all images in the repository | ||
* | ||
* @param ECR.ListImagesRequest the repositoryName & nextToken if presented | ||
*/ | ||
async function emptyRepository(params: ECR.ListImagesRequest) { | ||
const listedImages = await ecr.listImages(params).promise(); | ||
const imageIds = listedImages?.imageIds ?? []; | ||
const nextToken = listedImages.nextToken ?? null; | ||
if (imageIds.length === 0) { | ||
return; | ||
} | ||
|
||
await ecr.batchDeleteImage({ | ||
repositoryName: params.repositoryName, | ||
imageIds, | ||
}).promise(); | ||
|
||
if (nextToken) { | ||
await emptyRepository({ | ||
...params, | ||
nextToken, | ||
}); | ||
} | ||
} | ||
|
||
async function onDelete(deleteEvent: AWSLambda.CloudFormationCustomResourceDeleteEvent) { | ||
const repositoryName = deleteEvent.ResourceProperties?.RepositoryName; | ||
if (!repositoryName) { | ||
throw new Error('No RepositoryName was provided.'); | ||
} | ||
await emptyRepository({ repositoryName }); | ||
} |
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
198 changes: 198 additions & 0 deletions
198
packages/@aws-cdk/aws-ecr/test/auto-delete-images-handler.test.ts
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,198 @@ | ||
const mockECRClient = { | ||
listImages: jest.fn().mockReturnThis(), | ||
batchDeleteImage: jest.fn().mockReturnThis(), | ||
promise: jest.fn(), | ||
}; | ||
|
||
import { handler } from '../lib/auto-delete-images-handler'; | ||
|
||
jest.mock('aws-sdk', () => { | ||
return { ECR: jest.fn(() => mockECRClient) }; | ||
}); | ||
|
||
beforeEach(() => { | ||
mockECRClient.listImages.mockReturnThis(); | ||
mockECRClient.batchDeleteImage.mockReturnThis(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('does nothing on create event', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceCreateEvent> = { | ||
RequestType: 'Create', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
|
||
// WHEN | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockECRClient.listImages).toHaveBeenCalledTimes(0); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('does nothing on update event', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceUpdateEvent> = { | ||
RequestType: 'Update', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
|
||
// WHEN | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockECRClient.listImages).toHaveBeenCalledTimes(0); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('deletes no images on delete event when repository has no images', async () => { | ||
// GIVEN | ||
mockECRClient.promise.mockResolvedValue({ imageIds: [] }); // listedImages() call | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockECRClient.listImages).toHaveBeenCalledTimes(1); | ||
expect(mockECRClient.listImages).toHaveBeenCalledWith({ repositoryName: 'MyRepo' }); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('deletes all images on delete event', async () => { | ||
mockECRClient.promise.mockResolvedValue({ // listedImages() call | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag1', | ||
imageDigest: 'sha256-1', | ||
}, | ||
{ | ||
imageTag: 'tag2', | ||
imageDigest: 'sha256-2', | ||
}, | ||
], | ||
}); | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockECRClient.listImages).toHaveBeenCalledTimes(1); | ||
expect(mockECRClient.listImages).toHaveBeenCalledWith({ repositoryName: 'MyRepo' }); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledTimes(1); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledWith({ | ||
repositoryName: 'MyRepo', | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag1', | ||
imageDigest: 'sha256-1', | ||
}, | ||
{ | ||
imageTag: 'tag2', | ||
imageDigest: 'sha256-2', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('delete event where repo has many images does recurse appropriately', async () => { | ||
// GIVEN | ||
mockECRClient.promise // listedImages() call | ||
.mockResolvedValueOnce({ | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag1', | ||
imageDigest: 'sha256-1', | ||
}, | ||
{ | ||
imageTag: 'tag2', | ||
imageDigest: 'sha256-2', | ||
}, | ||
], | ||
nextToken: 'token1', | ||
}) | ||
.mockResolvedValueOnce(undefined) // batchDeleteImage() call | ||
.mockResolvedValueOnce({ // listedImages() call | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag3', | ||
imageDigest: 'sha256-3', | ||
}, | ||
{ | ||
imageTag: 'tag4', | ||
imageDigest: 'sha256-4', | ||
}, | ||
], | ||
}); | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockECRClient.listImages).toHaveBeenCalledTimes(2); | ||
expect(mockECRClient.listImages).toHaveBeenCalledWith({ repositoryName: 'MyRepo' }); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenCalledTimes(2); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenNthCalledWith(1, { | ||
repositoryName: 'MyRepo', | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag1', | ||
imageDigest: 'sha256-1', | ||
}, | ||
{ | ||
imageTag: 'tag2', | ||
imageDigest: 'sha256-2', | ||
}, | ||
], | ||
}); | ||
expect(mockECRClient.batchDeleteImage).toHaveBeenNthCalledWith(2, { | ||
repositoryName: 'MyRepo', | ||
imageIds: [ | ||
{ | ||
imageTag: 'tag3', | ||
imageDigest: 'sha256-3', | ||
}, | ||
{ | ||
imageTag: 'tag4', | ||
imageDigest: 'sha256-4', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
|
||
// helper function to get around TypeScript expecting a complete event object, | ||
// even though our tests only need some of the fields | ||
async function invokeHandler(event: Partial<AWSLambda.CloudFormationCustomResourceEvent>) { | ||
return handler(event as AWSLambda.CloudFormationCustomResourceEvent); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirintwn If the ECR repo was already deleted out-of-band, this will cause the custom resource deletion to fail with an error.