From e85c622d3700827fcd57ecb30a26ca2932b3e4e6 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy Date: Tue, 1 Oct 2024 16:17:08 -0400 Subject: [PATCH] unstable --- .../cli-integ/lib/with-cdk-app.ts | 2 +- packages/aws-cdk/lib/api/garbage-collector.ts | 32 +++++++++++-------- packages/aws-cdk/lib/cli.ts | 4 +++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts b/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts index 46dc2829552ae..da28e07d2a6ae 100644 --- a/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts +++ b/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts @@ -462,7 +462,7 @@ export class TestFixture extends ShellHelper { } public async cdkGarbageCollect(options: CdkGarbageCollectionCommandOptions): Promise { - const args = ['gc']; + const args = ['gc', '--unstable=gc']; // TODO: remove when stabilizing if (options.days) { args.push('--days', String(options.days)); } diff --git a/packages/aws-cdk/lib/api/garbage-collector.ts b/packages/aws-cdk/lib/api/garbage-collector.ts index 2029c44caad21..eedd989cf23d5 100644 --- a/packages/aws-cdk/lib/api/garbage-collector.ts +++ b/packages/aws-cdk/lib/api/garbage-collector.ts @@ -175,19 +175,24 @@ export class GarbageCollector { return !activeAssets.contains(obj.getHash()); }); - const deletables: S3Asset[] = graceDays > 0 - ? await Promise.all( - isolated.map(async (obj) => { - const tagTime = await obj.getTag(s3, ISOLATED_TAG); - if (tagTime && olderThan(Number(tagTime), currentTime, graceDays)) { - return obj; - } - return null; - }), - ).then(results => results.filter((obj): obj is S3Asset => obj !== null)) - : isolated; - - const taggables: S3Asset[] = graceDays > 0 + let deletables: S3Asset[] = []; + + // no items are deletable if tagOnly is set + if (this.props.tagOnly) { + deletables = graceDays > 0 + ? await Promise.all( + isolated.map(async (obj) => { + const tagTime = await obj.getTag(s3, ISOLATED_TAG); + if (tagTime && olderThan(Number(tagTime), currentTime, graceDays)) { + return obj; + } + return null; + }), + ).then(results => results.filter((obj): obj is S3Asset => obj !== null)) + : isolated; + } + + const taggables: S3Asset[] = graceDays > 0 ? await Promise.all( isolated.map(async (obj) => { const hasTag = await obj.hasTag(s3, ISOLATED_TAG); @@ -195,7 +200,6 @@ export class GarbageCollector { }), ).then(results => results.filter((obj): obj is S3Asset => obj !== null)) : []; - print(chalk.blue(`${deletables.length} deletable assets`)); print(chalk.white(`${taggables.length} taggable assets`)); diff --git a/packages/aws-cdk/lib/cli.ts b/packages/aws-cdk/lib/cli.ts index 6fa1faa28a1a0..5bbfa450ddc9e 100644 --- a/packages/aws-cdk/lib/cli.ts +++ b/packages/aws-cdk/lib/cli.ts @@ -115,6 +115,7 @@ async function parseCommandLineArguments(args: string[]) { .option('previous-parameters', { type: 'boolean', default: true, desc: 'Use previous values for existing parameters (you must specify all parameters on every deployment if this is disabled)' }), ) .command('gc [ENVIRONMENTS..]', 'Garbage collect assets', (yargs: Argv) => yargs + .option('unstable', { type: 'array', desc: 'Opt in to specific unstable features. Can be specified multiple times.', default: [] }) .option('dry-run', { type: 'boolean', desc: 'List assets instead of garbage collecting them', default: false }) .option('tag-only', { type: 'boolean', desc: 'Tag assets as isolated without deleting them', default: false }) .option('type', { type: 'string', desc: 'Specify either ecr, s3, or all', default: 'all' }) @@ -674,6 +675,9 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise