fix(cloudformation): apply target group health updates (#8948)

Updating a target group's health check settings through CloudFormation now applies them to the existing group through ModifyTargetGroup. The prior settings are saved before the change and restored on a failed provision or a stack rollback, independently of the target registrations, and a failed undo keeps its snapshot for a retry. Properties the template omits keep their values.

A change to the health check interval replaces the group's probe timer, keeping target health, and a superseded timer's callback is ignored.

Closes #8900
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-23 20:09:29 -04:00
1 parent 39290aa8be
commit d4bbb1bf96
7 files changed
+373 -12

No files matched your search

@@ -54,6 +54,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
* update commits or when the replacement is rolled back.
*/
private static final String LISTENER_CERTIFICATES_ATTR = "__FlociListenerCertificates";
static final String HEALTH_UPDATE_SNAPSHOT_ATTR = "__FlociTargetGroupHealthUpdateSnapshot";
/** Prefixes one uses attribute per subnet a load balancer is on, numbered from 1. */
private static final String USED_SUBNET_ATTR_PREFIX = CfnRollback.USES_ATTR_PREFIX + "Subnet.";
/**
@@ -87,6 +88,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
r.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
r.getAttributes().remove(CfnRollback.TARGET_GROUP_TARGETS_UPDATE_SNAPSHOT_ATTR);
r.getAttributes().remove(HEALTH_UPDATE_SNAPSHOT_ATTR);
r.getAttributes().remove(CfnRollback.LISTENER_CERTIFICATES_UPDATE_SNAPSHOT_ATTR);
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
switch (r.getResourceType()) {
@@ -172,6 +174,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
private static void clearTargetUpdateRecords(StackResource resource) {
resource.getAttributes().remove(CfnRollback.TARGET_GROUP_TARGETS_UPDATE_SNAPSHOT_ATTR);
resource.getAttributes().remove(HEALTH_UPDATE_SNAPSHOT_ATTR);
resource.getAttributes().remove(CfnRollback.LISTENER_CERTIFICATES_UPDATE_SNAPSHOT_ATTR);
DECLARED_TARGETS.clearPrior(resource);
}
@@ -186,15 +189,14 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
CfnRollback.carryFailedUndoSnapshot(previous, attempted,
CfnRollback.TARGET_GROUP_TARGETS_UPDATE_SNAPSHOT_ATTR,
CfnRollback.LISTENER_CERTIFICATES_UPDATE_SNAPSHOT_ATTR);
CfnRollback.LISTENER_CERTIFICATES_UPDATE_SNAPSHOT_ATTR, HEALTH_UPDATE_SNAPSHOT_ATTR);
}
/**
* A replacement is undone through the cleanup record, and a target group's record of its declared
* targets goes back with it, even when deleting the replacement fails. Without a cleanup record, a
* load balancer is moved back to the subnets an update took it off, and a target group gets back
* the registrations the update changed; otherwise neither has anything to put back: an update that
* kept the name changed nothing else, the way the switch did. A listener certificate resource gets
* the registrations and health settings the update changed. A listener certificate resource gets
* back the certificates the update changed on its listener. 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.
@@ -215,7 +217,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
return restorePriorSubnets(resource);
}
if (TARGET_GROUP.equals(resource.getResourceType())) {
restorePriorRegistrations(resource);
restorePriorTargetGroup(resource);
return true;
}
if (LISTENER_CERTIFICATE.equals(resource.getResourceType())) {
@@ -225,6 +227,49 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
return false;
}
private void restorePriorTargetGroup(StackResource resource) {
RuntimeException failure = null;
try {
restorePriorRegistrations(resource);
} catch (RuntimeException e) {
failure = e;
}
try {
restorePriorHealth(resource);
} catch (RuntimeException e) {
if (failure == null) {
failure = e;
} else if (failure != e) {
failure.addSuppressed(e);
}
}
if (failure != null) {
throw failure;
}
}
private static void recordPriorHealth(StackResource resource, TargetGroup group) {
ObjectNode saved = MAPPER.valueToTree(group);
saved.retain("healthCheckProtocol", "healthCheckPort", "healthCheckEnabled", "healthCheckPath",
"healthCheckIntervalSeconds", "healthCheckTimeoutSeconds", "healthyThresholdCount",
"unhealthyThresholdCount", "matcher");
resource.getAttributes().put(HEALTH_UPDATE_SNAPSHOT_ATTR, saved.toString());
}
private void restorePriorHealth(StackResource resource) {
JsonNode saved = CfnRollback.updateSnapshot(resource, HEALTH_UPDATE_SNAPSHOT_ATTR);
if (saved == null) {
return;
}
TargetGroup prior = MAPPER.convertValue(saved, TargetGroup.class);
String arn = resource.getPhysicalId();
elbV2Service.modifyTargetGroup(AwsArnUtils.parse(arn).region(), arn, prior.getHealthCheckProtocol(),
prior.getHealthCheckPort(), prior.getHealthCheckEnabled(), prior.getHealthCheckPath(),
prior.getHealthCheckIntervalSeconds(), prior.getHealthCheckTimeoutSeconds(),
prior.getHealthyThresholdCount(), prior.getUnhealthyThresholdCount(), prior.getMatcher());
resource.getAttributes().remove(HEALTH_UPDATE_SNAPSHOT_ATTR);
}
/**
* Deregisters the targets the update registered and registers again the ones it deregistered or
* registered over, as they were registered before it: the undo of a provision that failed, and the
@@ -380,7 +425,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
* to the template's targets in place (see {@link #reconcileTargets}). When that fails, what it did
* is undone before the failure propagates ({@link CfnRollback#guarded}): a target group this
* provision created, on a create or a replacement, is deleted again, and a kept one gets back the
* registrations it had.
* registrations and health settings it had.
*/
private void provisionTargetGroup(StackResource r, JsonNode props, ProvisionContext ctx,
Map<String, String> attributesBefore) {
@@ -431,8 +476,19 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
TargetGroup group = tg;
CfnRollback.guarded(r, ctx, created,
() -> elbV2Service.deleteTargetGroup(ctx.region(), group.getTargetGroupArn()),
() -> restorePriorRegistrations(r),
() -> reconcileTargets(r, group, targets, ctx));
() -> restorePriorTargetGroup(r),
() -> {
if (ctx.reusesPriorEntity(group.getTargetGroupArn())
&& (hcProtocol != null || hcPort != null || hcEnabled != null || hcPath != null
|| hcInterval != null || hcTimeout != null || healthyThreshold != null
|| unhealthyThreshold != null || matcher != null)) {
recordPriorHealth(r, group);
elbV2Service.modifyTargetGroup(ctx.region(), group.getTargetGroupArn(),
hcProtocol, hcPort, hcEnabled, hcPath, hcInterval, hcTimeout,
healthyThreshold, unhealthyThreshold, matcher);
}
reconcileTargets(r, group, targets, ctx);
});
DECLARED_TARGETS.record(r, ctx, attributesBefore, declaredRecord);
}
@@ -84,7 +84,7 @@ public class ElbV2HealthChecker implements Resettable {
return Objects.equals(a.getId(), b.getId()) && effectivePort(a, tg) == effectivePort(b, tg);
}
public void startMonitoring(TargetGroup tg) {
public synchronized void startMonitoring(TargetGroup tg) {
if (config.services().elbv2().mock()) {
return;
}
@@ -95,11 +95,18 @@ public class ElbV2HealthChecker implements Resettable {
states.computeIfAbsent(tgArn, k -> new ConcurrentHashMap<>());
long intervalMs = (tg.getHealthCheckIntervalSeconds() != null ? tg.getHealthCheckIntervalSeconds() : 30) * 1000L;
long timerId = vertx.setPeriodic(intervalMs, id -> probeAll(tgArn, tg));
timers.put(tgArn, timerId);
long timerId = vertx.setPeriodic(intervalMs, id -> {
if (Objects.equals(timers.get(tgArn), id)) {
probeAll(tgArn, tg);
}
});
Long previous = timers.put(tgArn, timerId);
if (previous != null) {
vertx.cancelTimer(previous);
}
}
public void stopMonitoring(String tgArn) {
public synchronized void stopMonitoring(String tgArn) {
Long timerId = timers.remove(tgArn);
if (timerId != null) {
vertx.cancelTimer(timerId);
@@ -107,7 +114,7 @@ public class ElbV2HealthChecker implements Resettable {
states.remove(tgArn);
}
public void clear() {
public synchronized void clear() {
timers.values().forEach(vertx::cancelTimer);
timers.clear();
states.clear();
@@ -678,6 +678,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
validateHealthCheckTimeout(healthCheckTimeout);
TargetGroup tg = requireTargetGroup(region, arn);
validateHealthCheckPort(healthCheckPort);
Integer previousInterval = tg.getHealthCheckIntervalSeconds();
if (healthCheckProtocol != null) tg.setHealthCheckProtocol(healthCheckProtocol);
if (healthCheckPort != null) tg.setHealthCheckPort(healthCheckPort);
if (healthCheckEnabled != null) tg.setHealthCheckEnabled(healthCheckEnabled);
@@ -688,6 +689,9 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
if (unhealthyThreshold != null) tg.setUnhealthyThresholdCount(unhealthyThreshold);
if (matcher != null) tg.setMatcher(matcher);
persistRegion(targetGroups, region);
if (!Objects.equals(previousInterval, tg.getHealthCheckIntervalSeconds())) {
healthChecker.startMonitoring(tg);
}
}
private static void validateHealthCheckTimeout(Integer timeout) {