fix(cloudformation): move a kept load balancer onto the template's subnets (#5881)

* fix(cloudformation): move a kept load balancer onto the template's subnets

An update that changed an ELBv2 load balancer's Subnets (or SubnetMappings)
kept the balancer on its old subnets, so it went on listing subnets the
template had dropped or replaced, which the update cleanup then deleted.
The provisioner now moves a kept balancer onto the template's subnets, as
SetSubnets does, and keeps the prior list in a hidden attribute so a
rolled-back update moves it back. A failed move back is reported as the
rollback failure and keeps the prior list for another try. SubnetMappings
gives the subnets when Subnets is absent.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): refuse a load balancer subnet mapping without a subnet

SubnetId is required in an ELBv2 SubnetMapping, but the provisioner
skipped a mapping without one, or with a blank one, so a template whose
mappings named no subnet created or kept a load balancer with no subnets.
A mapping without SubnetId now fails the resource with the schema error
before anything is created or moved, and a blank SubnetId is passed on
for ELBv2 to refuse as an unknown subnet.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): leave out a subnet mapping an Fn::If drops

SubnetMappings was resolved with AWS::NoValue kept as an empty value, so
a mapping an Fn::If dropped read as a mapping without SubnetId and the
new required-key check failed the stack, and a SubnetId an Fn::If dropped
failed as an unknown blank subnet. SubnetMappings is now resolved with
those values left out, as CloudFormation does: a dropped mapping is
skipped and a dropped SubnetId is reported as the missing required key.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(cloudformation): read stack event reasons decoded

The subnet mapping test matched the raw DescribeStackEvents body, where
ResourceStatusReason is XML-escaped, so the blank SubnetId reason with
its quotes never matched. It now reads the reasons with XmlParser.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): refuse a load balancer with subnets and subnet mappings

With both Subnets and SubnetMappings set the provisioner quietly took
Subnets, where the resource allows one or the other and CreateLoadBalancer
refuses both. Both together now fail the resource with the same
ValidationError before anything is created or moved, and the mappings are
checked for a SubnetId even when Subnets is set.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(cloudformation): pin an empty subnet mapping list beside subnets

An empty SubnetMappings beside Subnets, as an Fn::If that drops every
mapping leaves, is allowed; a test now holds that. The templateSubnets
Javadoc says how a blank SubnetId counts, and the integration test class
says it also covers both properties together.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-18 21:07:26 -04:00
1 parent d273d4f4e0
commit 94288dadd9
4 files changed
+488 -8

No files matched your search

@@ -42,6 +42,14 @@ public final class CfnRollback {
*/
public static final String PIPE_UPDATE_SNAPSHOT_ATTR = "__FlociPipeUpdateSnapshot";
/**
* Holds the subnets a kept ELBv2 load balancer had before the update in flight moved it,
* comma-joined, so a failed stack update can move it back. Written by
* {@code ElbV2CfnProvisioner} after its SetSubnets and spent by its {@code rollbackUpdate},
* retained across a failed move back.
*/
public static final String LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR = "__FlociLoadBalancerSubnetsUpdateSnapshot";
/**
* Holds the targets an EventBridge rule carried before the update in flight reconciled them,
* with the rule name, bus and region needed to address them again, so a failed stack update can
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
import io.github.hectorvent.floci.services.elbv2.model.Action;
import io.github.hectorvent.floci.services.elbv2.model.AvailabilityZone;
import io.github.hectorvent.floci.services.elbv2.model.Listener;
import io.github.hectorvent.floci.services.elbv2.model.LoadBalancer;
import io.github.hectorvent.floci.services.elbv2.model.Rule;
@@ -50,6 +51,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
r.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
switch (r.getResourceType()) {
case LOAD_BALANCER -> provisionLoadBalancer(r, props, ctx);
@@ -99,19 +101,23 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
if ("UPDATE_COMPLETE".equals(resource.getStatus())) {
resource.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
}
return ReplacementCleanup.complete(resource, this::delete);
}
@Override
public void clearUpdate(StackResource resource) {
resource.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
ReplacementCleanup.clear(resource);
}
/**
* A replacement is undone through the cleanup record. Without one, a load balancer or target
* group has nothing to put back: this provisioner never modifies an existing one (an update that
* kept the name only described it, the way the switch did), so nothing changed. A listener or
* rule without a record was modified in place, and putting that back needs a snapshot this
* A replacement is undone through the cleanup record. Without one, a load balancer is moved
* back to the subnets an update took it off, and otherwise it and a target group have nothing to
* put back: an update that kept the name changed nothing else, the way the switch did. A listener
* or rule without a record was modified in place, and putting that back needs a snapshot this
* provisioner does not keep, so the engine reports it as not rolled back, as it did for the switch.
*/
@Override
@@ -119,7 +125,26 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
if (ReplacementCleanup.rollback(resource, this::delete)) {
return true;
}
return LOAD_BALANCER.equals(resource.getResourceType()) || TARGET_GROUP.equals(resource.getResourceType());
if (LOAD_BALANCER.equals(resource.getResourceType())) {
return restorePriorSubnets(resource);
}
return TARGET_GROUP.equals(resource.getResourceType());
}
/**
* A failed move back propagates, so the engine reports its reason rather than an unimplemented
* rollback, and the snapshot stays until a move back succeeds.
*/
private boolean restorePriorSubnets(StackResource resource) {
String prior = resource.getAttributes().get(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
if (prior == null) {
return true;
}
String arn = resource.getPhysicalId();
elbV2Service.setSubnets(AwsArnUtils.parse(arn).region(), arn,
prior.isEmpty() ? List.of() : List.of(prior.split(",")));
resource.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
return true;
}
/**
@@ -135,7 +160,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
String scheme = ctx.resolveOptional(props, "Scheme");
String type = ctx.resolveOptional(props, "Type");
String ipAddressType = ctx.resolveOptional(props, "IpAddressType");
List<String> subnets = ctx.resolveStringList(props, "Subnets");
List<String> subnets = templateSubnets(props, ctx);
List<String> securityGroups = ctx.resolveStringList(props, "SecurityGroups");
Map<String, String> tags = ctx.resolveTags(props, "Tags");
@@ -148,6 +173,9 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
throw e;
}
lb = elbV2Service.describeLoadBalancers(ctx.region(), null, List.of(name), null, null).get(0);
if (ctx.reusesPriorEntity(lb.getLoadBalancerArn())) {
moveToTemplateSubnets(r, lb, subnets, ctx);
}
}
r.setPhysicalId(lb.getLoadBalancerArn());
@@ -158,6 +186,56 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put("LoadBalancerFullName", loadBalancerFullName(lb.getLoadBalancerArn()));
}
/**
* The template's subnets: {@code Subnets}, or else the {@code SubnetId} of each
* {@code SubnetMappings} entry, the form a Network Load Balancer uses to give a zone an Elastic IP.
* Both non-empty fails the resource with the error CreateLoadBalancer gives, as the resource allows
* one or the other; an empty side, such as one an Fn::If drops, does not count. {@code SubnetId} is
* required in a mapping, so one without it fails the resource before anything is created or moved,
* with the error the resource schema gives. A blank one counts as a mapping and, with no
* {@code Subnets}, is passed on as given for ELBv2 to refuse as an unknown subnet; dropping it would
* create or move the balancer without it.
*/
private static List<String> templateSubnets(JsonNode props, ProvisionContext ctx) {
List<String> subnets = ctx.resolveStringList(props, "Subnets");
if (props == null || !props.has("SubnetMappings")) {
return subnets;
}
List<String> mapped = new ArrayList<>();
// A mapping, or its SubnetId, that an Fn::If drops with AWS::NoValue is left out, as on AWS.
JsonNode mappings = ctx.engine().resolveNodeOmittingNoValue(props.get("SubnetMappings"));
if (mappings != null && mappings.isArray()) {
for (int i = 0; i < mappings.size(); i++) {
String subnetId = ctx.resolveOptional(mappings.get(i), "SubnetId");
if (subnetId == null) {
throw new AwsException("InvalidRequest",
"Model validation failed (#/SubnetMappings/" + i + ": required key [SubnetId] not found)", 400);
}
mapped.add(subnetId);
}
}
if (!subnets.isEmpty() && !mapped.isEmpty()) {
throw new AwsException("ValidationError",
"You can specify either subnets or subnet mappings, not both", 400);
}
return subnets.isEmpty() ? mapped : subnets;
}
/**
* {@code Subnets} is not create-only, so an update that keeps the balancer moves it onto the
* template's subnets in place, as SetSubnets does, rather than leaving it on a subnet the template
* dropped or replaced and the update cleanup then deletes. The prior list is kept for a rollback.
*/
private void moveToTemplateSubnets(StackResource r, LoadBalancer lb, List<String> subnets,
ProvisionContext ctx) {
List<String> current = lb.getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList();
if (subnets.isEmpty() || Set.copyOf(subnets).equals(Set.copyOf(current))) {
return;
}
elbV2Service.setSubnets(ctx.region(), lb.getLoadBalancerArn(), subnets);
r.getAttributes().put(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR, String.join(",", current));
}
private void provisionTargetGroup(StackResource r, JsonNode props, ProvisionContext ctx) {
String name = nameOrPrior(ctx.resolveOptional(props, "Name"), r.getAttributes().get("TargetGroupName"),
ctx, r.getLogicalId());