Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): unhandled nextToken returned by listImagesCommand in garbag…
…e collector for ECR (#32679) ### Issue # (if applicable) Closes #32498 ### Reason for this change When `listImagesCommand` returns nextToken in the `readRepoInBatches` function, nextToken is not passed as an argument for the subsequent `listImagesCommand` execution, causing `listImagesCommand` to continue executing. https://github.com/aws/aws-cdk/blob/v2.173.4/packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts#L621 According to the `listImagesCommand` documentation, if maxResults is not specified, a maximum of 100 images will be returned, so this bug requires at least 100 images in the asset repository. https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ecr/Interface/ListImagesCommandInput/ #### Reproduction Steps The following bash script and Dockerfile saved locally and executed, will push 120 container images to the asset repository. ```bash #!/usr/bin/env bash set -eu ACCOUNT_ID="your account id" REGION="your region" REPO_NAME="cdk-hnb659fds-container-assets-${ACCOUNT_ID}-${REGION}" IMAGE_NAME="test-image" AWS_PROFILE="your AWS profile" echo "Logging in to ECR..." aws ecr get-login-password --region "${REGION}" --profile "${AWS_PROFILE}" \ | docker login --username AWS --password-stdin "${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com" for i in $(seq 1 120); do hash=$(head -c 32 /dev/urandom | xxd -p -c 64) echo "Building and pushing image with tag: ${hash}" touch "${i}.txt" docker build \ --build-arg BUILD_NO="${i}" \ -t "${IMAGE_NAME}:${i}" \ . docker tag "${IMAGE_NAME}:${i}" \ "${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${REPO_NAME}:${hash}" docker push \ "${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${REPO_NAME}:${hash}" rm "${i}.txt" sleep 0.01 done echo "Done!" ``` ```dockerfile FROM scratch ARG BUILD_NO ENV BUILD_NO=${BUILD_NO} COPY ${BUILD_NO}.txt / ``` You can reproduce this bug by running the following command after the images have been pushed. ```bash $ cdk gc aws://{account id}/{region} --type ecr --unstable=gc --created-buffer-days 0 --action full --confirm=true ``` ### Description of changes Fix the problem of correctly handling nextToken when executing `listImagesCommand` in the `readRepoInBatches` function. ### Describe any new or updated permissions being added Nothing. ### Description of how you validated changes Verifying that this bug has been fixed using the CLI integration tests is difficult, so only unit tests are added. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information