forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.ts
724 lines (635 loc) · 23.3 KB
/
repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import { EOL } from 'os';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import { IResource, Lazy, RemovalPolicy, Resource, Stack, Token, TokenComparison } from '@aws-cdk/core';
import { IConstruct, Construct } from 'constructs';
import { CfnRepository } from './ecr.generated';
import { LifecycleRule, TagStatus } from './lifecycle';
/**
* Represents an ECR repository.
*/
export interface IRepository extends IResource {
/**
* The name of the repository
* @attribute
*/
readonly repositoryName: string;
/**
* The ARN of the repository
* @attribute
*/
readonly repositoryArn: string;
/**
* The URI of this repository (represents the latest image):
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY
*
* @attribute
*/
readonly repositoryUri: string;
/**
* Returns the URI of the repository for a certain tag. Can be used in `docker push/pull`.
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY[:TAG]
*
* @param tag Image tag to use (tools usually default to "latest" if omitted)
*/
repositoryUriForTag(tag?: string): string;
/**
* Returns the URI of the repository for a certain tag. Can be used in `docker push/pull`.
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY[@DIGEST]
*
* @param digest Image digest to use (tools usually default to the image with the "latest" tag if omitted)
*/
repositoryUriForDigest(digest?: string): string;
/**
* Add a policy statement to the repository's resource policy
*/
addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult;
/**
* Grant the given principal identity permissions to perform the actions on this repository
*/
grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant;
/**
* Grant the given identity permissions to pull images in this repository.
*/
grantPull(grantee: iam.IGrantable): iam.Grant;
/**
* Grant the given identity permissions to pull and push images to this repository.
*/
grantPullPush(grantee: iam.IGrantable): iam.Grant;
/**
* Define a CloudWatch event that triggers when something happens to this repository
*
* Requires that there exists at least one CloudTrail Trail in your account
* that captures the event. This method will not create the Trail.
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
onCloudTrailEvent(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines an AWS CloudWatch event rule that can trigger a target when an image is pushed to this
* repository.
*
* Requires that there exists at least one CloudTrail Trail in your account
* that captures the event. This method will not create the Trail.
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
onCloudTrailImagePushed(id: string, options?: OnCloudTrailImagePushedOptions): events.Rule;
/**
* Defines an AWS CloudWatch event rule that can trigger a target when the image scan is completed
*
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
onImageScanCompleted(id: string, options?: OnImageScanCompletedOptions): events.Rule;
/**
* Defines a CloudWatch event rule which triggers for repository events. Use
* `rule.addEventPattern(pattern)` to specify a filter.
*/
onEvent(id: string, options?: events.OnEventOptions): events.Rule;
}
/**
* Base class for ECR repository. Reused between imported repositories and owned repositories.
*/
export abstract class RepositoryBase extends Resource implements IRepository {
/**
* The name of the repository
*/
public abstract readonly repositoryName: string;
/**
* The ARN of the repository
*/
public abstract readonly repositoryArn: string;
/**
* Add a policy statement to the repository's resource policy
*/
public abstract addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult;
/**
* The URI of this repository (represents the latest image):
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY
*
*/
public get repositoryUri() {
return this.repositoryUriForTag();
}
/**
* Returns the URL of the repository. Can be used in `docker push/pull`.
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY[:TAG]
*
* @param tag Optional image tag
*/
public repositoryUriForTag(tag?: string): string {
const tagSuffix = tag ? `:${tag}` : '';
return this.repositoryUriWithSuffix(tagSuffix);
}
/**
* Returns the URL of the repository. Can be used in `docker push/pull`.
*
* ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY[@DIGEST]
*
* @param digest Optional image digest
*/
public repositoryUriForDigest(digest?: string): string {
const digestSuffix = digest ? `@${digest}` : '';
return this.repositoryUriWithSuffix(digestSuffix);
}
/**
* Returns the repository URI, with an appended suffix, if provided.
* @param suffix An image tag or an image digest.
* @private
*/
private repositoryUriWithSuffix(suffix?: string): string {
const parts = this.stack.parseArn(this.repositoryArn);
return `${parts.account}.dkr.ecr.${parts.region}.${this.stack.urlSuffix}/${this.repositoryName}${suffix}`;
}
/**
* Define a CloudWatch event that triggers when something happens to this repository
*
* Requires that there exists at least one CloudTrail Trail in your account
* that captures the event. This method will not create the Trail.
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
public onCloudTrailEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
const rule = new events.Rule(this, id, options);
rule.addTarget(options.target);
rule.addEventPattern({
source: ['aws.ecr'],
detailType: ['AWS API Call via CloudTrail'],
detail: {
requestParameters: {
repositoryName: [this.repositoryName],
},
},
});
return rule;
}
/**
* Defines an AWS CloudWatch event rule that can trigger a target when an image is pushed to this
* repository.
*
* Requires that there exists at least one CloudTrail Trail in your account
* that captures the event. This method will not create the Trail.
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
public onCloudTrailImagePushed(id: string, options: OnCloudTrailImagePushedOptions = {}): events.Rule {
const rule = this.onCloudTrailEvent(id, options);
rule.addEventPattern({
detail: {
eventName: ['PutImage'],
requestParameters: {
imageTag: options.imageTag ? [options.imageTag] : undefined,
},
},
});
return rule;
}
/**
* Defines an AWS CloudWatch event rule that can trigger a target when an image scan is completed
*
*
* @param id The id of the rule
* @param options Options for adding the rule
*/
public onImageScanCompleted(id: string, options: OnImageScanCompletedOptions = {}): events.Rule {
const rule = new events.Rule(this, id, options);
rule.addTarget(options.target);
rule.addEventPattern({
source: ['aws.ecr'],
detailType: ['ECR Image Scan'],
detail: {
'repository-name': [this.repositoryName],
'scan-status': ['COMPLETE'],
'image-tags': options.imageTags ?? undefined,
},
});
return rule;
}
/**
* Defines a CloudWatch event rule which triggers for repository events. Use
* `rule.addEventPattern(pattern)` to specify a filter.
*/
public onEvent(id: string, options: events.OnEventOptions = {}) {
const rule = new events.Rule(this, id, options);
rule.addEventPattern({
source: ['aws.ecr'],
resources: [this.repositoryArn],
});
rule.addTarget(options.target);
return rule;
}
/**
* Grant the given principal identity permissions to perform the actions on this repository
*/
public grant(grantee: iam.IGrantable, ...actions: string[]) {
const accountToTrust = this.principalCannotBeSafelyAddedToResourcePolicy(grantee);
if (accountToTrust) {
// If the principal is from a different account,
// that means addToPrincipalOrResource() will update the Resource Policy of this repo to trust that principal.
// However, ECR verifies that the principal used in the Policy exists,
// and will error out if it doesn't.
// Because of that, if the principal is a newly created resource,
// and there is not a dependency relationship between the Stacks of this repo and the principal,
// trust the entire account of the principal instead
// (otherwise, deploying this repo will fail)
this.addToResourcePolicy(new iam.PolicyStatement({
actions,
principals: [new iam.AccountPrincipal(accountToTrust)],
}));
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [this.repositoryArn],
scope: this,
});
} else {
return iam.Grant.addToPrincipalOrResource({
grantee,
actions,
resourceArns: [this.repositoryArn],
resourceSelfArns: [],
resource: this,
});
}
}
/**
* Grant the given identity permissions to use the images in this repository
*/
public grantPull(grantee: iam.IGrantable) {
const ret = this.grant(grantee, 'ecr:BatchCheckLayerAvailability', 'ecr:GetDownloadUrlForLayer', 'ecr:BatchGetImage');
iam.Grant.addToPrincipal({
grantee,
actions: ['ecr:GetAuthorizationToken'],
resourceArns: ['*'],
scope: this,
});
return ret;
}
/**
* Grant the given identity permissions to pull and push images to this repository.
*/
public grantPullPush(grantee: iam.IGrantable) {
this.grantPull(grantee);
return this.grant(grantee,
'ecr:PutImage',
'ecr:InitiateLayerUpload',
'ecr:UploadLayerPart',
'ecr:CompleteLayerUpload');
}
/**
* Returns the account ID of the account to put into the Resource Policy
* if we cannot put a reference to the principal itself there,
* and 'undefined' in case we can.
*/
private principalCannotBeSafelyAddedToResourcePolicy(grantee: iam.IGrantable): string | undefined {
// A principal cannot be safely added to the Resource Policy of this repo if:
// 1. The principal is from a different account.
// 2. The principal is a new resource (meaning, not imported).
// 3. The Stack this repo belongs to doesn't depend on the Stack the principal belongs to.
// condition #1
const principal = grantee.grantPrincipal;
const principalAccount = principal.principalAccount;
if (!principalAccount) {
return undefined;
}
const repoAndPrincipalAccountCompare = Token.compareStrings(this.env.account, principalAccount);
if (repoAndPrincipalAccountCompare === TokenComparison.BOTH_UNRESOLVED ||
repoAndPrincipalAccountCompare === TokenComparison.SAME) {
return undefined;
}
// condition #2
if (!iam.principalIsANewlyCreatedResource(principal)) {
return undefined;
}
// condition #3
const principalStack = Stack.of(principal);
if (this.stack.dependencies.includes(principalStack)) {
return undefined;
}
return principalAccount;
}
}
/**
* Options for the onCloudTrailImagePushed method
*/
export interface OnCloudTrailImagePushedOptions extends events.OnEventOptions {
/**
* Only watch changes to this image tag
*
* @default - Watch changes to all tags
*/
readonly imageTag?: string;
}
/**
* Options for the OnImageScanCompleted method
*/
export interface OnImageScanCompletedOptions extends events.OnEventOptions {
/**
* Only watch changes to the image tags spedified.
* Leave it undefined to watch the full repository.
*
* @default - Watch the changes to the repository with all image tags
*/
readonly imageTags?: string[];
}
export interface RepositoryProps {
/**
* Name for this repository
*
* @default Automatically generated name.
*/
readonly repositoryName?: string;
/**
* Life cycle rules to apply to this registry
*
* @default No life cycle rules
*/
readonly lifecycleRules?: LifecycleRule[];
/**
* The AWS account ID associated with the registry that contains the repository.
*
* @see https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_PutLifecyclePolicy.html
* @default The default registry is assumed.
*/
readonly lifecycleRegistryId?: string;
/**
* Determine what happens to the repository when the resource/stack is deleted.
*
* @default RemovalPolicy.Retain
*/
readonly removalPolicy?: RemovalPolicy;
/**
* Enable the scan on push when creating the repository
*
* @default false
*/
readonly imageScanOnPush?: boolean;
/**
* The tag mutability setting for the repository. If this parameter is omitted, the default setting of MUTABLE will be used which will allow image tags to be overwritten.
*
* @default TagMutability.MUTABLE
*/
readonly imageTagMutability?: TagMutability;
}
export interface RepositoryAttributes {
readonly repositoryName: string;
readonly repositoryArn: string;
}
/**
* Define an ECR repository
*/
export class Repository extends RepositoryBase {
/**
* Import a repository
*/
public static fromRepositoryAttributes(scope: Construct, id: string, attrs: RepositoryAttributes): IRepository {
class Import extends RepositoryBase {
public readonly repositoryName = attrs.repositoryName;
public readonly repositoryArn = attrs.repositoryArn;
public addToResourcePolicy(_statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
// dropped
return { statementAdded: false };
}
}
return new Import(scope, id);
}
public static fromRepositoryArn(scope: Construct, id: string, repositoryArn: string): IRepository {
// if repositoryArn is a token, the repository name is also required. this is because
// repository names can include "/" (e.g. foo/bar/myrepo) and it is impossible to
// parse the name from an ARN using CloudFormation's split/select.
if (Token.isUnresolved(repositoryArn)) {
throw new Error('"repositoryArn" is a late-bound value, and therefore "repositoryName" is required. Use `fromRepositoryAttributes` instead');
}
const repositoryName = repositoryArn.split('/').slice(1).join('/');
class Import extends RepositoryBase {
public repositoryName = repositoryName;
public repositoryArn = repositoryArn;
public addToResourcePolicy(_statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
// dropped
return { statementAdded: false };
}
}
return new Import(scope, id);
}
public static fromRepositoryName(scope: Construct, id: string, repositoryName: string): IRepository {
class Import extends RepositoryBase {
public repositoryName = repositoryName;
public repositoryArn = Repository.arnForLocalRepository(repositoryName, scope);
public addToResourcePolicy(_statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
// dropped
return { statementAdded: false };
}
}
return new Import(scope, id);
}
/**
* Returns an ECR ARN for a repository that resides in the same account/region
* as the current stack.
*/
public static arnForLocalRepository(repositoryName: string, scope: IConstruct, account?: string): string {
return Stack.of(scope).formatArn({
account,
service: 'ecr',
resource: 'repository',
resourceName: repositoryName,
});
}
private static validateRepositoryName(physicalName: string) {
const repositoryName = physicalName;
if (!repositoryName || Token.isUnresolved(repositoryName)) {
// the name is a late-bound value, not a defined string,
// so skip validation
return;
}
const errors: string[] = [];
// Rules codified from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecr-repository.html
if (repositoryName.length < 2 || repositoryName.length > 256) {
errors.push('Repository name must be at least 2 and no more than 256 characters');
}
const isPatternMatch = /^(?:[a-z0-9]+(?:[._-][a-z0-9]+)*\/)*[a-z0-9]+(?:[._-][a-z0-9]+)*$/.test(repositoryName);
if (!isPatternMatch) {
errors.push('Repository name must follow the specified pattern: (?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*');
}
if (errors.length > 0) {
throw new Error(`Invalid ECR repository name (value: ${repositoryName})${EOL}${errors.join(EOL)}`);
}
}
public readonly repositoryName: string;
public readonly repositoryArn: string;
private readonly lifecycleRules = new Array<LifecycleRule>();
private readonly registryId?: string;
private policyDocument?: iam.PolicyDocument;
constructor(scope: Construct, id: string, props: RepositoryProps = {}) {
super(scope, id, {
physicalName: props.repositoryName,
});
Repository.validateRepositoryName(this.physicalName);
const resource = new CfnRepository(this, 'Resource', {
repositoryName: this.physicalName,
// It says "Text", but they actually mean "Object".
repositoryPolicyText: Lazy.any({ produce: () => this.policyDocument }),
lifecyclePolicy: Lazy.any({ produce: () => this.renderLifecyclePolicy() }),
imageScanningConfiguration: !props.imageScanOnPush ? undefined : {
scanOnPush: true,
},
imageTagMutability: props.imageTagMutability || undefined,
});
resource.applyRemovalPolicy(props.removalPolicy);
this.registryId = props.lifecycleRegistryId;
if (props.lifecycleRules) {
props.lifecycleRules.forEach(this.addLifecycleRule.bind(this));
}
this.repositoryName = this.getResourceNameAttribute(resource.ref);
this.repositoryArn = this.getResourceArnAttribute(resource.attrArn, {
service: 'ecr',
resource: 'repository',
resourceName: this.physicalName,
});
}
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
if (this.policyDocument === undefined) {
this.policyDocument = new iam.PolicyDocument();
}
this.policyDocument.addStatements(statement);
return { statementAdded: false, policyDependable: this.policyDocument };
}
protected validate(): string[] {
const errors = super.validate();
errors.push(...this.policyDocument?.validateForResourcePolicy() || []);
return errors;
}
/**
* Add a life cycle rule to the repository
*
* Life cycle rules automatically expire images from the repository that match
* certain conditions.
*/
public addLifecycleRule(rule: LifecycleRule) {
// Validate rule here so users get errors at the expected location
if (rule.tagStatus === undefined) {
rule = { ...rule, tagStatus: rule.tagPrefixList === undefined ? TagStatus.ANY : TagStatus.TAGGED };
}
if (rule.tagStatus === TagStatus.TAGGED && (rule.tagPrefixList === undefined || rule.tagPrefixList.length === 0)) {
throw new Error('TagStatus.Tagged requires the specification of a tagPrefixList');
}
if (rule.tagStatus !== TagStatus.TAGGED && rule.tagPrefixList !== undefined) {
throw new Error('tagPrefixList can only be specified when tagStatus is set to Tagged');
}
if ((rule.maxImageAge !== undefined) === (rule.maxImageCount !== undefined)) {
throw new Error(`Life cycle rule must contain exactly one of 'maxImageAge' and 'maxImageCount', got: ${JSON.stringify(rule)}`);
}
if (rule.tagStatus === TagStatus.ANY && this.lifecycleRules.filter(r => r.tagStatus === TagStatus.ANY).length > 0) {
throw new Error('Life cycle can only have one TagStatus.Any rule');
}
this.lifecycleRules.push({ ...rule });
}
/**
* Render the life cycle policy object
*/
private renderLifecyclePolicy(): CfnRepository.LifecyclePolicyProperty | undefined {
const stack = Stack.of(this);
let lifecyclePolicyText: any;
if (this.lifecycleRules.length === 0 && !this.registryId) { return undefined; }
if (this.lifecycleRules.length > 0) {
lifecyclePolicyText = JSON.stringify(stack.resolve({
rules: this.orderedLifecycleRules().map(renderLifecycleRule),
}));
}
return {
lifecyclePolicyText,
registryId: this.registryId,
};
}
/**
* Return life cycle rules with automatic ordering applied.
*
* Also applies validation of the 'any' rule.
*/
private orderedLifecycleRules(): LifecycleRule[] {
if (this.lifecycleRules.length === 0) { return []; }
const prioritizedRules = this.lifecycleRules.filter(r => r.rulePriority !== undefined && r.tagStatus !== TagStatus.ANY);
const autoPrioritizedRules = this.lifecycleRules.filter(r => r.rulePriority === undefined && r.tagStatus !== TagStatus.ANY);
const anyRules = this.lifecycleRules.filter(r => r.tagStatus === TagStatus.ANY);
if (anyRules.length > 0 && anyRules[0].rulePriority !== undefined && autoPrioritizedRules.length > 0) {
// Supporting this is too complex for very little value. We just prohibit it.
throw new Error("Cannot combine prioritized TagStatus.Any rule with unprioritized rules. Remove rulePriority from the 'Any' rule.");
}
const prios = prioritizedRules.map(r => r.rulePriority!);
let autoPrio = (prios.length > 0 ? Math.max(...prios) : 0) + 1;
const ret = new Array<LifecycleRule>();
for (const rule of prioritizedRules.concat(autoPrioritizedRules).concat(anyRules)) {
ret.push({
...rule,
rulePriority: rule.rulePriority ?? autoPrio++,
});
}
// Do validation on the final array--might still be wrong because the user supplied all prios, but incorrectly.
validateAnyRuleLast(ret);
return ret;
}
}
function validateAnyRuleLast(rules: LifecycleRule[]) {
const anyRules = rules.filter(r => r.tagStatus === TagStatus.ANY);
if (anyRules.length === 1) {
const maxPrio = Math.max(...rules.map(r => r.rulePriority!));
if (anyRules[0].rulePriority !== maxPrio) {
throw new Error(`TagStatus.Any rule must have highest priority, has ${anyRules[0].rulePriority} which is smaller than ${maxPrio}`);
}
}
}
/**
* Render the lifecycle rule to JSON
*/
function renderLifecycleRule(rule: LifecycleRule) {
return {
rulePriority: rule.rulePriority,
description: rule.description,
selection: {
tagStatus: rule.tagStatus || TagStatus.ANY,
tagPrefixList: rule.tagPrefixList,
countType: rule.maxImageAge !== undefined ? CountType.SINCE_IMAGE_PUSHED : CountType.IMAGE_COUNT_MORE_THAN,
countNumber: rule.maxImageAge?.toDays() ?? rule.maxImageCount,
countUnit: rule.maxImageAge !== undefined ? 'days' : undefined,
},
action: {
type: 'expire',
},
};
}
/**
* Select images based on counts
*/
const enum CountType {
/**
* Set a limit on the number of images in your repository
*/
IMAGE_COUNT_MORE_THAN = 'imageCountMoreThan',
/**
* Set an age limit on the images in your repository
*/
SINCE_IMAGE_PUSHED = 'sinceImagePushed',
}
/**
* The tag mutability setting for your repository.
*/
export enum TagMutability {
/**
* allow image tags to be overwritten.
*/
MUTABLE = 'MUTABLE',
/**
* all image tags within the repository will be immutable which will prevent them from being overwritten.
*/
IMMUTABLE = 'IMMUTABLE',
}