|
| 1 | +import cdk = require('@aws-cdk/cdk'); |
| 2 | +import { cloudformation, RepositoryArn, RepositoryName } from './ecr.generated'; |
| 3 | +import { CountType, LifecycleRule, TagStatus } from './lifecycle'; |
| 4 | +import { RepositoryRef } from "./repository-ref"; |
| 5 | + |
| 6 | +export interface RepositoryProps { |
| 7 | + /** |
| 8 | + * Name for this repository |
| 9 | + * |
| 10 | + * @default Automatically generated name. |
| 11 | + */ |
| 12 | + repositoryName?: string; |
| 13 | + |
| 14 | + /** |
| 15 | + * Life cycle rules to apply to this registry |
| 16 | + * |
| 17 | + * @default No life cycle rules |
| 18 | + */ |
| 19 | + lifecycleRules?: LifecycleRule[]; |
| 20 | + |
| 21 | + /** |
| 22 | + * The AWS account ID associated with the registry that contains the repository. |
| 23 | + * |
| 24 | + * @see https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_PutLifecyclePolicy.html |
| 25 | + * @default The default registry is assumed. |
| 26 | + */ |
| 27 | + lifecycleRegistryId?: string; |
| 28 | + |
| 29 | + /** |
| 30 | + * Retain the repository on stack deletion |
| 31 | + * |
| 32 | + * If you don't set this to true, the registry must be empty, otherwise |
| 33 | + * your stack deletion will fail. |
| 34 | + * |
| 35 | + * @default false |
| 36 | + */ |
| 37 | + retain?: boolean; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Define an ECR repository |
| 42 | + */ |
| 43 | +export class Repository extends RepositoryRef { |
| 44 | + public readonly repositoryName: RepositoryName; |
| 45 | + public readonly repositoryArn: RepositoryArn; |
| 46 | + private readonly lifecycleRules = new Array<LifecycleRule>(); |
| 47 | + private readonly registryId?: string; |
| 48 | + private policyDocument?: cdk.PolicyDocument; |
| 49 | + |
| 50 | + constructor(parent: cdk.Construct, id: string, props: RepositoryProps = {}) { |
| 51 | + super(parent, id); |
| 52 | + |
| 53 | + const resource = new cloudformation.RepositoryResource(this, 'Resource', { |
| 54 | + repositoryName: props.repositoryName, |
| 55 | + // It says "Text", but they actually mean "Object". |
| 56 | + repositoryPolicyText: this.policyDocument, |
| 57 | + lifecyclePolicy: new cdk.Token(() => this.renderLifecyclePolicy()), |
| 58 | + }); |
| 59 | + |
| 60 | + if (props.retain) { |
| 61 | + resource.options.deletionPolicy = cdk.DeletionPolicy.Retain; |
| 62 | + } |
| 63 | + |
| 64 | + this.registryId = props.lifecycleRegistryId; |
| 65 | + if (props.lifecycleRules) { |
| 66 | + props.lifecycleRules.forEach(this.addLifecycleRule.bind(this)); |
| 67 | + } |
| 68 | + |
| 69 | + this.repositoryName = resource.ref; |
| 70 | + this.repositoryArn = resource.repositoryArn; |
| 71 | + } |
| 72 | + |
| 73 | + public addToResourcePolicy(statement: cdk.PolicyStatement) { |
| 74 | + if (this.policyDocument === undefined) { |
| 75 | + this.policyDocument = new cdk.PolicyDocument(); |
| 76 | + } |
| 77 | + this.policyDocument.addStatement(statement); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Add a life cycle rule to the repository |
| 82 | + * |
| 83 | + * Life cycle rules automatically expire images from the repository that match |
| 84 | + * certain conditions. |
| 85 | + */ |
| 86 | + public addLifecycleRule(rule: LifecycleRule) { |
| 87 | + // Validate rule here so users get errors at the expected location |
| 88 | + if (rule.tagStatus === undefined) { |
| 89 | + rule.tagStatus = rule.tagPrefixList === undefined ? TagStatus.Any : TagStatus.Tagged; |
| 90 | + } |
| 91 | + |
| 92 | + if (rule.tagStatus === TagStatus.Tagged && (rule.tagPrefixList === undefined || rule.tagPrefixList.length === 0)) { |
| 93 | + throw new Error('TagStatus.Tagged requires the specification of a tagPrefixList'); |
| 94 | + } |
| 95 | + if (rule.tagStatus !== TagStatus.Tagged && rule.tagPrefixList !== undefined) { |
| 96 | + throw new Error('tagPrefixList can only be specified when tagStatus is set to Tagged'); |
| 97 | + } |
| 98 | + if ((rule.maxImageAgeDays !== undefined) === (rule.maxImageCount !== undefined)) { |
| 99 | + throw new Error(`Life cycle rule must contain exactly one of 'maxImageAgeDays' and 'maxImageCount', got: ${JSON.stringify(rule)}`); |
| 100 | + } |
| 101 | + |
| 102 | + if (rule.tagStatus === TagStatus.Any && this.lifecycleRules.filter(r => r.tagStatus === TagStatus.Any).length > 0) { |
| 103 | + throw new Error('Life cycle can only have one TagStatus.Any rule'); |
| 104 | + } |
| 105 | + |
| 106 | + this.lifecycleRules.push({ ...rule }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Render the life cycle policy object |
| 111 | + */ |
| 112 | + private renderLifecyclePolicy(): cloudformation.RepositoryResource.LifecyclePolicyProperty | undefined { |
| 113 | + let lifecyclePolicyText: any; |
| 114 | + |
| 115 | + if (this.lifecycleRules.length === 0 && !this.registryId) { return undefined; } |
| 116 | + |
| 117 | + if (this.lifecycleRules.length > 0) { |
| 118 | + lifecyclePolicyText = JSON.stringify(cdk.resolve({ |
| 119 | + rules: this.orderedLifecycleRules().map(renderLifecycleRule), |
| 120 | + })); |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + lifecyclePolicyText, |
| 125 | + registryId: this.registryId, |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Return life cycle rules with automatic ordering applied. |
| 131 | + * |
| 132 | + * Also applies validation of the 'any' rule. |
| 133 | + */ |
| 134 | + private orderedLifecycleRules(): LifecycleRule[] { |
| 135 | + if (this.lifecycleRules.length === 0) { return []; } |
| 136 | + |
| 137 | + const prioritizedRules = this.lifecycleRules.filter(r => r.rulePriority !== undefined && r.tagStatus !== TagStatus.Any); |
| 138 | + const autoPrioritizedRules = this.lifecycleRules.filter(r => r.rulePriority === undefined && r.tagStatus !== TagStatus.Any); |
| 139 | + const anyRules = this.lifecycleRules.filter(r => r.tagStatus === TagStatus.Any); |
| 140 | + if (anyRules.length > 0 && anyRules[0].rulePriority !== undefined && autoPrioritizedRules.length > 0) { |
| 141 | + // Supporting this is too complex for very little value. We just prohibit it. |
| 142 | + throw new Error("Cannot combine prioritized TagStatus.Any rule with unprioritized rules. Remove rulePriority from the 'Any' rule."); |
| 143 | + } |
| 144 | + |
| 145 | + const prios = prioritizedRules.map(r => r.rulePriority!); |
| 146 | + let autoPrio = (prios.length > 0 ? Math.max(...prios) : 0) + 1; |
| 147 | + |
| 148 | + const ret = new Array<LifecycleRule>(); |
| 149 | + for (const rule of prioritizedRules.concat(autoPrioritizedRules).concat(anyRules)) { |
| 150 | + ret.push({ |
| 151 | + ...rule, |
| 152 | + rulePriority: rule.rulePriority !== undefined ? rule.rulePriority : autoPrio++ |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + // Do validation on the final array--might still be wrong because the user supplied all prios, but incorrectly. |
| 157 | + validateAnyRuleLast(ret); |
| 158 | + return ret; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +function validateAnyRuleLast(rules: LifecycleRule[]) { |
| 163 | + const anyRules = rules.filter(r => r.tagStatus === TagStatus.Any); |
| 164 | + if (anyRules.length === 1) { |
| 165 | + const maxPrio = Math.max(...rules.map(r => r.rulePriority!)); |
| 166 | + if (anyRules[0].rulePriority !== maxPrio) { |
| 167 | + throw new Error(`TagStatus.Any rule must have highest priority, has ${anyRules[0].rulePriority} which is smaller than ${maxPrio}`); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Render the lifecycle rule to JSON |
| 174 | + */ |
| 175 | +function renderLifecycleRule(rule: LifecycleRule) { |
| 176 | + return { |
| 177 | + rulePriority: rule.rulePriority, |
| 178 | + description: rule.description, |
| 179 | + selection: { |
| 180 | + tagStatus: rule.tagStatus || TagStatus.Any, |
| 181 | + tagPrefixList: rule.tagPrefixList, |
| 182 | + countType: rule.maxImageAgeDays !== undefined ? CountType.SinceImagePushed : CountType.ImageCountMoreThan, |
| 183 | + countNumber: rule.maxImageAgeDays !== undefined ? rule.maxImageAgeDays : rule.maxImageCount, |
| 184 | + countUnit: rule.maxImageAgeDays !== undefined ? 'days' : undefined, |
| 185 | + }, |
| 186 | + action: { |
| 187 | + type: 'expire' |
| 188 | + } |
| 189 | + }; |
| 190 | +} |
0 commit comments