mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudformation): keep generated names across updates for Scheduler, SQS and MicrovmImage (#2985)
provision runs again on every UpdateStack. #2788 gave seven provisioners ProvisionContext.stablePhysicalName so an unnamed resource keeps the name it already had instead of creating a second one and orphaning the first, and left AWS::SQS::Queue and AWS::Lambda::MicrovmImage for a follow-up. The Scheduler ScheduleGroup provisioner solved the same problem by reading the id off the resource, the pattern AGENTS.md step 4 now documents as an anti-pattern because provision assigns the new id as it works. All three names are createOnly in their registry schemas. Scheduler: stablePhysicalName plus reusesPriorEntity in place of the ConflictException catch; the tag diff goes through ProvisionContext.staleTagKeys. One behaviour change: the old path called createScheduleGroup and adopted only on ConflictException, so a group deleted outside the stack was silently recreated; the new path reads the group first and surfaces ResourceNotFoundException, which is what CloudFormation does. SQS: the physical id is the queue URL, so the prior name is read from the QueueName attribute the way SnsCfnProvisioner reads the topic name beside its ARN, and only when its .fifo suffix still matches FifoQueue, which is createOnly too. On reuse the update goes through SetQueueAttributes, the schema's update handler, because createQueue on an existing name answers QueueAlreadyExists the moment an attribute differs; that also fixes an explicitly named queue whose VisibilityTimeout changed. An explicitly named queue whose FifoQueue changes fails with ValidationError instead of reconciling around it: the schema's own QueueName text says a named queue cannot take an update that requires replacement, and that is the refusal the other provisioners use for a createOnly change under a kept name. AWS::SQS::QueuePolicy keeps its id across updates, the same two-line fix S3::BucketPolicy got. MicrovmImage: stablePhysicalName, and on reuse updateImage (the schema's UpdateMicrovmImage handler) rather than createImage, which would mint a new version on every update. CfnStableNameUpdatePathTest gains one case per type pinning exactly one create across create plus update; the SQS integration test drives an unnamed queue through UpdateStack with a changed VisibilityTimeout against the real service. Left for follow-ups: SQS Tags, the NetworkConnector name, resetting an attribute the template dropped, and recreating a resource deleted out of band the way EcsCapacityCfnProvisioner does, if that leniency is wanted.
This commit is contained in:
1 parent
96d16de998
commit
9162b990ec
9 files changed
+562
-82
No files matched your search
+15
-12
@@ -56,22 +56,25 @@ public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionImage(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "Name");
|
||||
if (name == null || name.isBlank()) {
|
||||
name = ctx.generatePhysicalName(r.getLogicalId(), 64, false);
|
||||
}
|
||||
// Name is a createOnlyProperty and the physical id, so an unnamed image keeps the name it
|
||||
// already had across updates instead of a fresh one each time provision runs.
|
||||
String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "Name"), r.getLogicalId(), 64, false);
|
||||
String codeArtifactUri = null;
|
||||
if (props != null && props.has("CodeArtifact") && props.get("CodeArtifact").has("Uri")) {
|
||||
codeArtifactUri = ctx.engine().resolve(props.get("CodeArtifact").get("Uri"));
|
||||
}
|
||||
LambdaMicrovmsService.MicrovmImage image = microvmsService.createImage(
|
||||
ctx.region(),
|
||||
ctx.accountId(),
|
||||
name,
|
||||
ctx.resolveOptional(props, "BaseImageArn"),
|
||||
ctx.resolveOptional(props, "BuildRoleArn"),
|
||||
codeArtifactUri,
|
||||
ctx.resolveOptional(props, "Description"));
|
||||
String baseImageArn = ctx.resolveOptional(props, "BaseImageArn");
|
||||
String buildRoleArn = ctx.resolveOptional(props, "BuildRoleArn");
|
||||
String description = ctx.resolveOptional(props, "Description");
|
||||
// provision is also the update path. With the name stable, the second UpdateStack reaches
|
||||
// the service with an image that exists; the registry schema's update handler is
|
||||
// UpdateMicrovmImage, so reconcile through updateImage rather than mint a version through
|
||||
// createImage. A replacing update derives a different name and still creates.
|
||||
LambdaMicrovmsService.MicrovmImage image = ctx.reusesPriorEntity(name)
|
||||
? microvmsService.updateImage(ctx.region(), name, baseImageArn, buildRoleArn,
|
||||
codeArtifactUri, description)
|
||||
: microvmsService.createImage(ctx.region(), ctx.accountId(), name, baseImageArn,
|
||||
buildRoleArn, codeArtifactUri, description);
|
||||
r.setPhysicalId(image.name);
|
||||
r.getAttributes().put("ImageArn", image.imageArn);
|
||||
r.getAttributes().put("Arn", image.imageArn);
|
||||
|
||||
+25
-35
@@ -9,9 +9,8 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -39,22 +38,18 @@ public class SchedulerScheduleGroupCfnProvisioner implements CfnResourceProvisio
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "Name");
|
||||
if (name == null || name.isBlank()) {
|
||||
// No explicit name and this resource already has a physical id: keep it across updates
|
||||
// instead of generating a fresh random one each retry, same as LogGroup/Queue do.
|
||||
name = r.getPhysicalId() != null
|
||||
? r.getPhysicalId()
|
||||
: ctx.generatePhysicalName(r.getLogicalId(), 64, false);
|
||||
}
|
||||
// Name is a createOnlyProperty in the registry schema, so an unnamed group keeps the name
|
||||
// it already had across updates instead of getting a fresh random one each time provision
|
||||
// runs. Read from the context, not the resource: provision assigns the new id as it works.
|
||||
String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "Name"), r.getLogicalId(), 64, false);
|
||||
// Tags wrapped in an intrinsic (e.g. Fn::If choosing between two tag lists) is not resolved
|
||||
// here: the engine's Fn::If support is scalar-only, so a conditional list would collapse to
|
||||
// a string rather than the chosen array. tagsAreResolvable is true both when Tags is absent
|
||||
// (the template genuinely wants no tags) and when it is a plain array (the template's actual
|
||||
// desired tags), and false only when Tags is present but not a plain array - the one case
|
||||
// where the desired state genuinely cannot be read, so the retry path below must not treat
|
||||
// "couldn't resolve the intended tags" as "the intended tags are empty" and delete everything
|
||||
// live.
|
||||
// here: the engine collapses a list-valued intrinsic to a string (#2983), so a conditional
|
||||
// list would not arrive as the chosen array. tagsAreResolvable is true both when Tags is
|
||||
// absent (the template genuinely wants no tags) and when it is a plain array (the template's
|
||||
// actual desired tags), and false only when Tags is present but not a plain array, the one
|
||||
// case where the desired state genuinely cannot be read, so the reconcile path below must
|
||||
// not treat "couldn't resolve the intended tags" as "the intended tags are empty" and delete
|
||||
// everything live.
|
||||
boolean hasTagsProperty = props != null && props.has("Tags");
|
||||
boolean tagsAreResolvable = !hasTagsProperty || props.get("Tags").isArray();
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
@@ -68,32 +63,27 @@ public class SchedulerScheduleGroupCfnProvisioner implements CfnResourceProvisio
|
||||
}
|
||||
|
||||
ScheduleGroup group;
|
||||
try {
|
||||
group = schedulerService.createScheduleGroup(name, tags, ctx.region());
|
||||
} catch (AwsException e) {
|
||||
if (!"ConflictException".equals(e.getErrorCode()) || !name.equals(r.getPhysicalId())) {
|
||||
throw e;
|
||||
}
|
||||
// Same-stack create-retry: CloudFormation only retries a resource under the physical id
|
||||
// it previously assigned, so a conflict on that exact name means this logical resource's
|
||||
// own group already exists from an earlier attempt. Adopt it instead of failing the whole
|
||||
// stack on a retry of a step that already succeeded.
|
||||
if (ctx.reusesPriorEntity(name)) {
|
||||
// provision is also the update path, and createScheduleGroup answers ConflictException
|
||||
// for a name that exists. The derived name matching the prior physical id means this
|
||||
// logical resource's own group is already there, so reconcile it rather than create:
|
||||
// per the registry schema the update handler is a tag diff and nothing else. A key
|
||||
// dropped from the template (or the whole Tags list emptied, or Tags removed entirely)
|
||||
// must not linger on the live resource. Skipped only when Tags is present but
|
||||
// unresolvable, where untagging every live key would erase tags for a reason unrelated
|
||||
// to what the template asked for.
|
||||
group = schedulerService.getScheduleGroup(name, ctx.region());
|
||||
// Reconcile tags both ways: a key dropped from the template (or the whole Tags list
|
||||
// emptied, or Tags removed entirely) must not linger on the live resource, matching how
|
||||
// the same-stack retry path applies every other property change rather than only ever
|
||||
// adding. Skipped only when Tags is present but unresolvable (Fn::If) - untagging every
|
||||
// live key there would erase tags for a reason unrelated to what the template asked for.
|
||||
if (tagsAreResolvable) {
|
||||
Set<String> staleKeys = new HashSet<>(group.getTags().keySet());
|
||||
staleKeys.removeAll(tags.keySet());
|
||||
List<String> staleKeys = ProvisionContext.staleTagKeys(group.getTags(), tags);
|
||||
if (!staleKeys.isEmpty()) {
|
||||
schedulerService.untagScheduleGroup(name, ctx.region(), new ArrayList<>(staleKeys));
|
||||
schedulerService.untagScheduleGroup(name, ctx.region(), staleKeys);
|
||||
}
|
||||
}
|
||||
if (!tags.isEmpty()) {
|
||||
schedulerService.tagScheduleGroup(name, ctx.region(), tags);
|
||||
}
|
||||
} else {
|
||||
group = schedulerService.createScheduleGroup(name, tags, ctx.region());
|
||||
}
|
||||
r.setPhysicalId(group.getName());
|
||||
r.getAttributes().put("Arn", group.getArn());
|
||||
|
||||
+49
-11
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Queue;
|
||||
@@ -36,7 +37,7 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
switch (r.getResourceType()) {
|
||||
case "AWS::SQS::Queue" -> provisionQueue(r, props, ctx);
|
||||
case "AWS::SQS::QueuePolicy" -> provisionQueuePolicy(r);
|
||||
case "AWS::SQS::QueuePolicy" -> provisionQueuePolicy(r, ctx);
|
||||
default -> throw new IllegalStateException("SqsCfnProvisioner cannot handle " + r.getResourceType());
|
||||
}
|
||||
}
|
||||
@@ -55,12 +56,23 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {
|
||||
: null;
|
||||
boolean fifo = "true".equalsIgnoreCase(fifoFlag);
|
||||
String queueName = ctx.resolveOptional(props, "QueueName");
|
||||
// The physical id is the queue URL, so ctx.stablePhysicalName does not fit: the prior name
|
||||
// comes from the QueueName attribute recorded at create time, as SnsCfnProvisioner reads
|
||||
// the topic name beside its ARN. Keeping it means an unnamed queue survives an update
|
||||
// instead of being orphaned with its messages. FifoQueue is createOnly like QueueName, so a
|
||||
// prior name whose .fifo suffix no longer matches the flag is a replacing update and gets a
|
||||
// fresh name.
|
||||
String priorName = r.getAttributes() != null ? r.getAttributes().get("QueueName") : null;
|
||||
if (queueName == null || queueName.isBlank()) {
|
||||
// Like real CloudFormation, generated names of FIFO queues must end in .fifo
|
||||
// (SqsService rejects FifoQueue=true otherwise). Keep within the 80-char limit.
|
||||
queueName = fifo
|
||||
? ctx.generatePhysicalName(r.getLogicalId(), 75, false) + ".fifo"
|
||||
: ctx.generatePhysicalName(r.getLogicalId(), 80, false);
|
||||
if (priorName != null && !priorName.isBlank() && priorName.endsWith(".fifo") == fifo) {
|
||||
queueName = priorName;
|
||||
} else {
|
||||
// Like real CloudFormation, generated names of FIFO queues must end in .fifo
|
||||
// (SqsService rejects FifoQueue=true otherwise). Keep within the 80-char limit.
|
||||
queueName = fifo
|
||||
? ctx.generatePhysicalName(r.getLogicalId(), 75, false) + ".fifo"
|
||||
: ctx.generatePhysicalName(r.getLogicalId(), 80, false);
|
||||
}
|
||||
}
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
if (props != null) {
|
||||
@@ -88,18 +100,44 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {
|
||||
attrs.put("RedrivePolicy", ctx.engine().resolveJsonAttribute(props.path("RedrivePolicy")));
|
||||
}
|
||||
}
|
||||
Queue queue = sqsService.createQueue(queueName, attrs, ctx.region());
|
||||
// provision is also the update path. createQueue on a name that exists hands back the
|
||||
// queue only when every attribute matches and answers QueueAlreadyExists otherwise, so the
|
||||
// second UpdateStack that changes VisibilityTimeout must go through SetQueueAttributes, the
|
||||
// registry schema's update handler, rather than a second create. The prior physical id is
|
||||
// the queue URL SetQueueAttributes addresses. A replacing update derives a different name
|
||||
// and still creates.
|
||||
// FifoQueue is createOnly and encoded in the name. An explicitly named queue keeps its name
|
||||
// across updates, so a changed FifoQueue would need a replacement under the same name,
|
||||
// which CloudFormation refuses for a custom-named resource: the SQS registry schema says a
|
||||
// named queue cannot take an update that requires replacement. Fail it, as the other
|
||||
// provisioners do, instead of reconciling everything but the mode.
|
||||
if (ctx.isUpdate() && queueName.equals(priorName) && priorName.endsWith(".fifo") != fifo) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Updating FifoQueue requires resource replacement, which is not supported.", 400);
|
||||
}
|
||||
String queueUrl;
|
||||
if (ctx.isUpdate() && queueName.equals(priorName)) {
|
||||
attrs.remove("FifoQueue");
|
||||
sqsService.setQueueAttributes(ctx.priorPhysicalId(), attrs, ctx.region());
|
||||
queueUrl = ctx.priorPhysicalId();
|
||||
} else {
|
||||
Queue queue = sqsService.createQueue(queueName, attrs, ctx.region());
|
||||
queueUrl = queue.getQueueUrl();
|
||||
}
|
||||
// QueueArn is computed on demand in SqsService#getQueueAttributes and is not stored on the
|
||||
// Queue object, so build it here from region + accountId + queueName. Without this,
|
||||
// Fn::GetAtt [Queue, Arn] references resolve to an empty string.
|
||||
String queueArn = AwsArnUtils.Arn.of("sqs", ctx.region(), ctx.accountId(), queueName).toString();
|
||||
r.setPhysicalId(queue.getQueueUrl());
|
||||
r.setPhysicalId(queueUrl);
|
||||
r.getAttributes().put("Arn", queueArn);
|
||||
r.getAttributes().put("QueueName", queueName);
|
||||
r.getAttributes().put("QueueUrl", queue.getQueueUrl());
|
||||
r.getAttributes().put("QueueUrl", queueUrl);
|
||||
}
|
||||
|
||||
private void provisionQueuePolicy(StackResource r) {
|
||||
r.setPhysicalId("queue-policy-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
private void provisionQueuePolicy(StackResource r, ProvisionContext ctx) {
|
||||
// A policy has no backing entity, so its id only has to stay put across updates.
|
||||
r.setPhysicalId(ctx.isUpdate()
|
||||
? ctx.priorPhysicalId()
|
||||
: "queue-policy-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user