mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): replace an ECR repository when the engine says its create-only properties changed (#8461)
This commit is contained in:
1 parent
f930634fa8
commit
c1bc65060d
3 files changed
+166
-24
No files matched your search
+88
-23
@@ -24,6 +24,7 @@ import java.util.Set;
|
||||
@ApplicationScoped
|
||||
public class EcrCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String REPOSITORY = "AWS::ECR::Repository";
|
||||
private static final int REPOSITORY_NAME_MAX_LENGTH = 256;
|
||||
/** What CreateRepository applies when ImageTagMutability is omitted. */
|
||||
private static final String DEFAULT_IMAGE_TAG_MUTABILITY = "MUTABLE";
|
||||
@@ -36,7 +37,7 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of("AWS::ECR::Repository");
|
||||
return Set.of(REPOSITORY);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,12 +50,57 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
|
||||
return "floci.services.ecr.enabled";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, (type, id, region) ->
|
||||
CfnDeletes.safeDelete("ECR repository", id,
|
||||
() -> ecrService.deleteRepository(id, null, false, region),
|
||||
"RepositoryNotFoundException"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.rollback(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
if (ctx.isUpdate()) {
|
||||
// Rolling this update back undoes it alone; only ContinueUpdateRollback retries the
|
||||
// delete the last rollback could not make.
|
||||
ReplacementCleanup.endRollbackRetry(r);
|
||||
}
|
||||
Map<String, String> attributesBefore = new HashMap<>(r.getAttributes());
|
||||
// The engine's change decides whether the update replaces the repository: RepositoryName and
|
||||
// EncryptionConfiguration are create-only in the registry. A named repository cannot be
|
||||
// replaced under the same name.
|
||||
boolean replaces = ctx.isUpdate() && ctx.changes(REPOSITORY, props).requiresReplacement();
|
||||
String explicitName = ctx.resolveOptional(props, "RepositoryName");
|
||||
// CDK bootstrap requires lower-case repository names; CFN-generated suffixes can include
|
||||
// upper-case characters. Normalize to satisfy the AWS ECR repository name pattern.
|
||||
String repoName = ctx.stablePhysicalName(ctx.resolveOptional(props, "RepositoryName"),
|
||||
r.getLogicalId(), REPOSITORY_NAME_MAX_LENGTH, true).toLowerCase();
|
||||
// upper-case characters. Normalize to satisfy the AWS ECR repository name pattern. A
|
||||
// replacing update that names nothing gets a fresh generated name, not the prior one.
|
||||
String repoName = (replaces && (explicitName == null || explicitName.isBlank())
|
||||
? ctx.generatePhysicalName(r.getLogicalId(), REPOSITORY_NAME_MAX_LENGTH, true)
|
||||
: ctx.stablePhysicalName(explicitName, r.getLogicalId(), REPOSITORY_NAME_MAX_LENGTH, true))
|
||||
.toLowerCase();
|
||||
if (replaces && ctx.reusesPriorEntity(repoName)) {
|
||||
throw CfnExceptions.customNamedReplacement(repoName);
|
||||
}
|
||||
|
||||
String mutability = ctx.resolveOptional(props, "ImageTagMutability");
|
||||
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, ctx);
|
||||
@@ -68,36 +114,55 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
|
||||
// reconciled in place; a replacing update derives a different name and creates, and a name
|
||||
// that already belongs to another repository fails the create as on CloudFormation.
|
||||
boolean reusing = ctx.reusesPriorEntity(repoName);
|
||||
// EncryptionConfiguration is create-only, so it is read only when the repository is created.
|
||||
JsonNode encryption = props != null ? props.get("EncryptionConfiguration") : null;
|
||||
Repository repo = reusing
|
||||
? reconcileExisting(repoName, mutability, tags, region)
|
||||
: ecrService.createRepository(repoName, null, mutability, null, null, null, tags, region);
|
||||
: ecrService.createRepository(repoName, null, mutability, null,
|
||||
ctx.resolveOptional(encryption, "EncryptionType"), ctx.resolveOptional(encryption, "KmsKey"),
|
||||
tags, region);
|
||||
if (!reusing) {
|
||||
r.setPhysicalId(repoName);
|
||||
}
|
||||
|
||||
// Both policies are updatable: a template that declares one sets it, and on the update
|
||||
// path a template that no longer declares one deletes it, tolerating "none set".
|
||||
if (lifecyclePolicy != null) {
|
||||
ecrService.putLifecyclePolicy(repoName, null, lifecyclePolicy, region);
|
||||
} else if (reusing) {
|
||||
CfnDeletes.safeDelete("ECR lifecycle policy", repoName,
|
||||
() -> ecrService.deleteLifecyclePolicy(repoName, null, region),
|
||||
"LifecyclePolicyNotFoundException");
|
||||
}
|
||||
String repositoryPolicy = repositoryPolicyText(props, ctx);
|
||||
if (repositoryPolicy != null) {
|
||||
ecrService.setRepositoryPolicy(repoName, null, repositoryPolicy, region);
|
||||
} else if (reusing) {
|
||||
CfnDeletes.safeDelete("ECR repository policy", repoName,
|
||||
() -> ecrService.deleteRepositoryPolicy(repoName, null, region),
|
||||
"RepositoryPolicyNotFoundException");
|
||||
}
|
||||
// A repository this provision created and could not finish setting up is removed, or left
|
||||
// to the create rollback or the next cleanup when that fails too.
|
||||
CfnRollback.guarded(r, ctx, !reusing, () -> ecrService.deleteRepository(repoName, null, true, region),
|
||||
() -> { }, () -> {
|
||||
// Both policies are updatable: a template that declares one sets it, and on the update
|
||||
// path a template that no longer declares one deletes it, tolerating "none set".
|
||||
if (lifecyclePolicy != null) {
|
||||
ecrService.putLifecyclePolicy(repoName, null, lifecyclePolicy, region);
|
||||
} else if (reusing) {
|
||||
CfnDeletes.safeDelete("ECR lifecycle policy", repoName,
|
||||
() -> ecrService.deleteLifecyclePolicy(repoName, null, region),
|
||||
"LifecyclePolicyNotFoundException");
|
||||
}
|
||||
String repositoryPolicy = repositoryPolicyText(props, ctx);
|
||||
if (repositoryPolicy != null) {
|
||||
ecrService.setRepositoryPolicy(repoName, null, repositoryPolicy, region);
|
||||
} else if (reusing) {
|
||||
CfnDeletes.safeDelete("ECR repository policy", repoName,
|
||||
() -> ecrService.deleteRepositoryPolicy(repoName, null, region),
|
||||
"RepositoryPolicyNotFoundException");
|
||||
}
|
||||
});
|
||||
|
||||
r.setPhysicalId(repoName);
|
||||
r.getAttributes().put("Arn", repo.getRepositoryArn());
|
||||
r.getAttributes().put("RepositoryUri", repo.getRepositoryUri());
|
||||
// The repository the prior physical id named is deleted once the update commits, but only
|
||||
// when it is empty: EmptyOnDelete is not read, and an empty-only delete is what AWS does
|
||||
// by default. A create under an existing name fails, so a replacement here is always one
|
||||
// this update created.
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore, replaces && !reusing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
ecrService.deleteRepository(physicalId, null, true, region);
|
||||
CfnDeletes.safeDelete("ECR repository", physicalId,
|
||||
() -> ecrService.deleteRepository(physicalId, null, true, region),
|
||||
"RepositoryNotFoundException");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user