Skip to content

Commit

Permalink
unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc committed Oct 1, 2024
1 parent 890daaf commit e85c622
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export class TestFixture extends ShellHelper {
}

public async cdkGarbageCollect(options: CdkGarbageCollectionCommandOptions): Promise<string> {
const args = ['gc'];
const args = ['gc', '--unstable=gc']; // TODO: remove when stabilizing
if (options.days) {
args.push('--days', String(options.days));
}
Expand Down
32 changes: 18 additions & 14 deletions packages/aws-cdk/lib/api/garbage-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,31 @@ 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);
return hasTag ? null : obj;
}),
).then(results => results.filter((obj): obj is S3Asset => obj !== null))
: [];


print(chalk.blue(`${deletables.length} deletable assets`));
print(chalk.white(`${taggables.length} taggable assets`));
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down Expand Up @@ -674,6 +675,9 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
});

case 'gc':
if (!args.unstable.includes('gc')) {
throw new Error('Unstable feature use: \'gc\' is unstable. It must be opted in via \'--unstable\', e.g. \'cdk gc --unstable=gc\'');
}
return cli.garbageCollect(args.ENVIRONMENTS, {
dryRun: args['dry-run'],
tagOnly: args['tag-only'],
Expand Down

0 comments on commit e85c622

Please sign in to comment.