-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecr): add option to auto delete images upon ECR repo removal
- Loading branch information
Showing
6 changed files
with
529 additions
and
1 deletion.
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.