(garbage-collection): `cdk gc` fails on ECR with `InvalidParameterException` when a ListImages page has only untagged images

Aperta Adatta ai principianti
#2,179 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
aws, typescript
Ambito
cli

Direzione di ricerca

Apri packages/@aws-cdk/toolkit-lib/lib/api/garbage-collection/garbage-collector.ts e leggi readRepoInBatches insieme a imageMap(): la map scarta le immagini non taggate, e l'array imageIds risultante viene comunque inviato a DescribeImages e BatchGetImage. La correzione consiste nell'avanzare il token di continuazione e saltare la pagina quando imageIds è vuoto. Cerca sotto i test garbage-collection di toolkit-lib la copertura esistente di readRepoInBatches da estendere con una pagina interamente non taggata; fatto significa che il ciclo percorre ogni pagina senza una chiamata con imageIds vuoto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

aws-cdk bug needs-triage
Describe the bug

cdk gc fails on the ECR part with EcrGarbageCollectionFailed when the bootstrap ECR repository contains untagged images.

In readRepoInBatches, imageMap() skips images without a tag. When every image in a ListImages page is untagged, imageIds becomes empty, and it is still passed to DescribeImages and BatchGetImage. ECR rejects an empty imageIds.

In our repository, all 274 untagged images are child manifests of the 137 OCI image indexes pushed as Docker image assets. The first 2 of the 399 ListImages pages contain only these untagged images, so the unpatched CLI fails at 0%.

Regression Issue
  • Select this option if this issue appears to be a regression.
Last Known Working CDK Version

This is not a regression: imageMap() has skipped untagged images, and the empty imageIds is passed to DescribeImages, since ECR garbage collection was added in v2.165.0 (aws/aws-cdk#31841).

Expected Behavior

Pages without tagged images are skipped, and garbage collection continues.

Current Behavior
$ cdk gc aws://<account-id>/ap-northeast-1 --unstable=gc --type=ecr --action=print -v
[14:58:35] CDK Toolkit CLI version: 2.1148.1 (build fcf5e34)
...
[14:58:40] Parsing through 450 stacks
[15:02:28] Done parsing through stacks
[15:02:28] Waiting for stack CDKToolkit to finish creating or updating...
[15:05:24] Found bootstrap repo cdk-hnb659fds-container-assets-<account-id>-ap-northeast-1 with 39828 images
[15:05:24] Parsing through 39828 images in batches
[0.00%] 0 files scanned: 0 assets (0.00 MiB) eligible for deletion.
EcrGarbageCollectionFailed: InvalidParameterException: Invalid parameter at 'imageIds' failed to satisfy constraint: 'Member must have length greater than or equal to 1'

The bootstrap ECR repository has 37,888 images: 37,614 tagged (137 of them are OCI image indexes) and 274 untagged.

Reproduction Steps
  1. Have untagged images in the bootstrap ECR repository, enough that one ListImages page contains only untagged images.

  2. Run:

    cdk gc aws://<account-id>/<region> --unstable=gc --type=ecr --action=print
    
Possible Solution

Skip the page when imageIds is empty, in readRepoInBatches of packages/@aws-cdk/toolkit-lib/lib/api/garbage-collection/garbage-collector.ts:

const imageIds = Object.keys(images).map(key => ({ imageDigest: key }));
if (imageIds.length === 0) {
  continuationToken = response.nextToken;
  if (!continuationToken) break;
  continue;
}

With this change applied to a local copy of the 2.1148.1 CLI, cdk gc --type=ecr --action=print --created-buffer-days=7 -v on the same repository scanned to the last batch:

[18:20:35] 971 images total
...
[93.18%] 37111 files scanned: 37020 assets (30890.90 GiB) eligible for deletion.

The percentage stays below 100% because the total counts ListImages entries (one per tag, untagged included), while the scanned count includes only tagged images older than --created-buffer-days.

Additional Information/Context

Workaround: run cdk gc --type=s3 to skip ECR.

CDK CLI Version

2.1148.1 (build fcf5e34)

Framework Version

2.262.0

Node.js Version

v24.22.1

OS

macOS 27.2.1

Language

Python

Language Version

Python (3.12.13)

Other information

No response

Lingua principale
TypeScript
Stelle
110
Fork
138
Merge medio
1g 9h
PR unite (30g)
56

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di aws/aws-cdk-cli

Tutte le issue di aws/aws-cdk-cli

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.