fix(autoscaling): register instances with a Classic Load Balancer on attach and deregister them on detach (#8496)

* fix(autoscaling): register instances with a Classic Load Balancer on attach and deregister them on detach
* fix(autoscaling): put members back in what they left when a detach is refused
* test(autoscaling): share the attachment tests' groups in one fixture
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-15 18:29:22 -04:00
1 parent 065d7194b0
commit e1c3242978
9 files changed
+480 -149

No files matched your search

@@ -302,7 +302,11 @@ public class AutoScalingReconciler {
AutoScalingService.registerWithTargetGroups(elbV2Service, asg,
AutoScalingService.attachedTargetGroups(asg),
asgInst.getInstanceId(), returningFromStandby);
registerWithClassicLoadBalancers(asg, asgInst, returningFromStandby);
// Without this a Classic Load Balancer would stay empty, and a min_elb_capacity wait would never
// be satisfied.
AutoScalingService.registerWithClassicLoadBalancers(elbClassicService, asg,
AutoScalingService.attachedClassicLoadBalancers(asg), List.of(asgInst.getInstanceId()),
returningFromStandby);
asgInst.setLifecycleState("InService");
changed = true;
if (standbyActivityId != null) {
@@ -881,36 +885,6 @@ public class AutoScalingReconciler {
}
/**
* Registers a newly launched or returning instance with every Classic load balancer the group names.
*
* <p>An ASG attached through {@code LoadBalancerNames} feeds a Classic load balancer, not a
* target group, and the two lists are independent; a group can carry either or both. Without
* this the Classic load balancer would stay empty, and a {@code min_elb_capacity} wait would
* never be satisfied.
*/
private void registerWithClassicLoadBalancers(AutoScalingGroup asg, AsgInstance asgInst,
boolean returningFromStandby) {
if (elbClassicService == null) {
return;
}
for (String lbName : asg.getLoadBalancerNames()) {
try {
elbClassicService.registerInstances(asg.getRegion(), lbName,
List.of(asgInst.getInstanceId()));
LOG.debugv("ASG {0}: registered {1} with Classic ELB {2}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), lbName);
} catch (Exception e) {
LOG.warnv(e, "ASG {0}: could not register {1} with Classic ELB {2}: {3}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), lbName, e.getMessage());
if (returningFromStandby && !(e instanceof AwsException aws
&& "LoadBalancerNotFound".equals(aws.getErrorCode()))) {
throw e;
}
}
}
}
private LaunchSource resolveLaunchSource(AutoScalingGroup asg) {
LaunchConfiguration lc = resolveLaunchConfiguration(asg);
if (lc != null) {
@@ -33,7 +33,9 @@ import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.Base64;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -631,8 +633,9 @@ public class AutoScalingService implements PassedRoleLookup {
}
if (asg.isEksManaged()) {
// The instance is the cluster's node, which EKS owns and the cluster's other node groups share: it
// is not terminated, and stays in the target groups another of their groups has attached.
// is not terminated, and stays in the load balancers another of their groups has attached.
deregisterFromUnsharedTargetGroups(asg, memberIds(asg), attachedTargetGroups(asg));
deregisterFromUnsharedClassicLoadBalancers(asg, memberIds(asg), attachedClassicLoadBalancers(asg));
} else if (forceDelete && ec2Service != null && !active.isEmpty()) {
// The instances leave the group's load balancers before they are terminated, as on a scale-in.
List<String> leaving = active.stream().map(AsgInstance::getInstanceId).filter(Objects::nonNull).toList();
@@ -1077,6 +1080,34 @@ public class AutoScalingService implements PassedRoleLookup {
}
}
/**
* Registers {@code instanceIds} with each of {@code loadBalancerNames}, Classic Load Balancers, in one call to
* each. A load balancer that refuses them is logged and skipped, unless {@code strict}: then only a load balancer
* that is gone is skipped, and any other failure is thrown.
*/
static void registerWithClassicLoadBalancers(ElbClassicService elb, AutoScalingGroup asg,
Collection<String> loadBalancerNames, List<String> instanceIds,
boolean strict) {
if (elb == null || instanceIds.isEmpty()) {
return;
}
String instances = String.join(", ", instanceIds);
for (String loadBalancer : loadBalancerNames) {
try {
elb.registerInstances(asg.getRegion(), loadBalancer, instanceIds);
LOG.debugv("ASG {0}: registered {1} with Classic ELB {2}",
asg.getAutoScalingGroupName(), instances, loadBalancer);
} catch (Exception e) {
LOG.warnv(e, "ASG {0}: could not register {1} with Classic ELB {2}: {3}",
asg.getAutoScalingGroupName(), instances, loadBalancer, e.getMessage());
if (strict && !(e instanceof AwsException aws
&& "LoadBalancerNotFound".equals(aws.getErrorCode()))) {
throw e;
}
}
}
}
/**
* Runs {@code action} on each group in {@code region}, under the group's own lock, which guards its member list
* against the reconciler. Callers hold the Region's lock, so no other change to which group holds an instance
@@ -1118,9 +1149,21 @@ public class AutoScalingService implements PassedRoleLookup {
}
}
/**
* Deregisters {@code instanceIds} from every Classic Load Balancer attached to {@code asg}, elb sources included.
*/
static void deregisterFromClassicLoadBalancers(ElbClassicService elb, AutoScalingGroup asg,
List<String> instanceIds) {
for (String loadBalancer : asg.getLoadBalancerNames()) {
deregisterFromClassicLoadBalancers(elb, asg, attachedClassicLoadBalancers(asg), instanceIds);
}
/** As above, from {@code loadBalancerNames} rather than from every Classic Load Balancer the group has attached. */
static void deregisterFromClassicLoadBalancers(ElbClassicService elb, AutoScalingGroup asg,
Collection<String> loadBalancerNames, List<String> instanceIds) {
if (elb == null || instanceIds.isEmpty()) {
return;
}
for (String loadBalancer : loadBalancerNames) {
try {
elb.deregisterInstances(asg.getRegion(), loadBalancer, instanceIds);
} catch (AwsException e) {
@@ -1160,12 +1203,13 @@ public class AutoScalingService implements PassedRoleLookup {
InstanceRefresh refresh = activeInstanceRefresh(asg);
if (!detached.isEmpty()) {
if (asg.isEksManaged()) {
// The node stays in the target groups another group holding it has attached.
// The node stays in the load balancers another group holding it has attached.
deregisterFromUnsharedTargetGroups(asg, detached, attachedTargetGroups(asg));
deregisterFromUnsharedClassicLoadBalancers(asg, detached, attachedClassicLoadBalancers(asg));
} else {
deregisterFromTargetGroups(elbV2Service, asg, detached);
deregisterFromClassicLoadBalancers(elbClassicService, asg, detached);
}
deregisterFromClassicLoadBalancers(elbClassicService, asg, detached);
// Detaching removes only the group-name tag, and only where it still names this group.
ec2Service.deleteSystemTag(region, detached, GROUP_NAME_TAG, name);
completeInstanceRefreshIfSettled(region, name, asg);
@@ -1498,19 +1542,17 @@ public class AutoScalingService implements PassedRoleLookup {
}
public void attachLoadBalancers(String region, String name, List<String> lbNames) {
AutoScalingGroup asg = requireGroup(region, name);
for (String lb : lbNames) {
if (!asg.getLoadBalancerNames().contains(lb)) {
asg.getLoadBalancerNames().add(lb);
changeAttachments(region, name, asg -> {
for (String lb : lbNames) {
if (!asg.getLoadBalancerNames().contains(lb)) {
asg.getLoadBalancerNames().add(lb);
}
}
}
groups.put(asgKey(region, name), asg);
});
}
public void detachLoadBalancers(String region, String name, List<String> lbNames) {
AutoScalingGroup asg = requireGroup(region, name);
asg.getLoadBalancerNames().removeAll(lbNames);
groups.put(asgKey(region, name), asg);
changeAttachments(region, name, asg -> asg.getLoadBalancerNames().removeAll(lbNames));
}
// ── Traffic sources ─────────────────────────────────────────────────────────────
@@ -1557,9 +1599,18 @@ public class AutoScalingService implements PassedRoleLookup {
/** The target groups attached to {@code asg}: as target groups, and as {@code elbv2} traffic sources. */
static Set<String> attachedTargetGroups(AutoScalingGroup asg) {
Set<String> attached = new LinkedHashSet<>(asg.getTargetGroupARNs());
return attached(asg.getTargetGroupARNs(), asg, "elbv2");
}
/** The Classic Load Balancers attached to {@code asg}: by name, and as {@code elb} traffic sources. */
static Set<String> attachedClassicLoadBalancers(AutoScalingGroup asg) {
return attached(asg.getLoadBalancerNames(), asg, "elb");
}
private static Set<String> attached(List<String> listed, AutoScalingGroup asg, String sourceType) {
Set<String> attached = new LinkedHashSet<>(listed);
asg.getTrafficSourceTypeByIdentifier().forEach((identifier, type) -> {
if ("elbv2".equals(type)) {
if (sourceType.equals(type)) {
attached.add(identifier);
}
});
@@ -1569,9 +1620,9 @@ public class AutoScalingService implements PassedRoleLookup {
// ── EKS managed node groups ────────────────────────────────────────────────
// EKS keeps a group for each managed node group, under the name DescribeNodegroup reports. Floci runs one node
// per cluster, which all of the cluster's node groups share, so their groups all hold that node, and the
// reconciler leaves them alone. The reconciler registers other groups' members with their target groups as
// they come into service; these groups' members are registered and deregistered here, as target groups are
// attached and detached.
// reconciler leaves them alone. The reconciler registers other groups' members with their target groups and
// Classic Load Balancers as they come into service; these groups' members are registered and deregistered here,
// as those are attached and detached.
/**
* Creates the group EKS keeps for a managed node group, unless a group of that name exists. It has no launch
@@ -1597,7 +1648,8 @@ public class AutoScalingService implements PassedRoleLookup {
/**
* Puts {@code instance} in service in the EKS group {@code name}, unless it is a member already, and registers
* it with the group's target groups. Does nothing when there is no EKS group of that name.
* it with the group's target groups and Classic Load Balancers. Does nothing when there is no EKS group of that
* name.
*/
public void addEksManagedInstance(String region, String name, Instance instance) {
String key = asgKey(region, name);
@@ -1618,14 +1670,16 @@ public class AutoScalingService implements PassedRoleLookup {
groups.put(key, asg);
registerWithTargetGroups(elbV2Service, asg, attachedTargetGroups(asg), member.getInstanceId(),
false);
registerWithClassicLoadBalancers(elbClassicService, asg, attachedClassicLoadBalancers(asg),
List.of(member.getInstanceId()), false);
}
}
}
/**
* Deletes the EKS group {@code name}, if there is one, as {@link #deleteAutoScalingGroup} deletes it: its
* instance, which EKS owns, is not terminated, and leaves the group's target groups except those another group
* holding it has attached. Does nothing when there is no EKS group of that name.
* instance, which EKS owns, is not terminated, and leaves the group's target groups and Classic Load Balancers
* except those another group holding it has attached. Does nothing when there is no EKS group of that name.
*/
public void deleteEksManagedGroup(String region, String name) {
synchronized (lockFor(region)) {
@@ -1637,11 +1691,12 @@ public class AutoScalingService implements PassedRoleLookup {
}
/**
* Applies {@code change} to the target groups and traffic sources attached to the group {@code name}, and
* stores it. The group's members are deregistered from the target groups the change detached, and its in-service
* members registered with those it attached, as AWS does, all under the Region's lock and then the group's, in
* the order {@code attachInstances} and the changes to an EKS group's members take them. An instance an EKS group
* shares with another group stays in a target group that other group still has attached.
* Applies {@code change} to the target groups, Classic Load Balancers and traffic sources attached to the group
* {@code name}, and stores it. The group's members are deregistered from the target groups and Classic Load
* Balancers the change detached, and its in-service members registered with those it attached, as AWS does, all
* under the Region's lock and then the group's, in the order {@code attachInstances} and the changes to an EKS
* group's members take them. An instance an EKS group shares with another group stays in a target group or
* Classic Load Balancer that other group still has attached.
*/
private void changeAttachments(String region, String name, Consumer<AutoScalingGroup> change) {
String key = asgKey(region, name);
@@ -1649,32 +1704,58 @@ public class AutoScalingService implements PassedRoleLookup {
synchronized (lockFor(key)) {
AutoScalingGroup current = requireGroup(region, name);
List<String> targetGroupsBefore = new ArrayList<>(current.getTargetGroupARNs());
List<String> loadBalancersBefore = new ArrayList<>(current.getLoadBalancerNames());
Map<String, String> trafficSourcesBefore = new LinkedHashMap<>(current.getTrafficSourceTypeByIdentifier());
Set<String> attachedBefore = attachedTargetGroups(current);
Set<String> classicBefore = attachedClassicLoadBalancers(current);
change.accept(current);
Set<String> attached = attachedTargetGroups(current);
Set<String> detached = new LinkedHashSet<>(attachedBefore);
detached.removeAll(attached);
attached.removeAll(attachedBefore);
// The members leave the target groups the change detached before the change is stored: when ELBv2
// refuses, the group keeps them attached, so the detach can be sent again.
Set<String> classicAttached = attachedClassicLoadBalancers(current);
Set<String> classicDetached = new LinkedHashSet<>(classicBefore);
classicDetached.removeAll(classicAttached);
classicAttached.removeAll(classicBefore);
List<String> members = memberIds(current);
List<String> inService = current.getInstances().stream()
.filter(member -> "InService".equals(member.getLifecycleState()))
.map(AsgInstance::getInstanceId).filter(Objects::nonNull).toList();
// The members leave the target groups and Classic Load Balancers the change detached before the change
// is stored, one load balancer at a time. When one refuses, the in-service members rejoin those they
// had already left and the group keeps them all attached, as it was, so the detach can be sent again.
// An ordinary group's instances are its own, so they leave every one it detached.
List<Runnable> rejoin = new ArrayList<>();
try {
if (current.isEksManaged()) {
deregisterFromUnsharedTargetGroups(current, memberIds(current), detached);
} else if (!detached.isEmpty() && !memberIds(current).isEmpty()) {
// An ordinary group's instances are its own, so they leave every target group it detached.
deregisterFromTargetGroups(elbV2Service, current, detached, memberIds(current));
for (String targetGroup : detached) {
if (current.isEksManaged()) {
deregisterFromUnsharedTargetGroups(current, members, Set.of(targetGroup));
} else if (!members.isEmpty()) {
deregisterFromTargetGroups(elbV2Service, current, List.of(targetGroup), members);
}
rejoin.add(() -> registerWithTargetGroups(elbV2Service, current, List.of(targetGroup),
inService, false));
}
for (String loadBalancer : classicDetached) {
if (current.isEksManaged()) {
deregisterFromUnsharedClassicLoadBalancers(current, members, Set.of(loadBalancer));
} else {
deregisterFromClassicLoadBalancers(elbClassicService, current, List.of(loadBalancer),
members);
}
rejoin.add(() -> registerWithClassicLoadBalancers(elbClassicService, current,
List.of(loadBalancer), inService, false));
}
} catch (RuntimeException e) {
rejoin.forEach(Runnable::run);
current.setTargetGroupARNs(targetGroupsBefore);
current.setLoadBalancerNames(loadBalancersBefore);
current.setTrafficSourceTypeByIdentifier(trafficSourcesBefore);
throw e;
}
groups.put(key, current);
List<String> inService = current.getInstances().stream()
.filter(member -> "InService".equals(member.getLifecycleState()))
.map(AsgInstance::getInstanceId).filter(Objects::nonNull).toList();
registerWithTargetGroups(elbV2Service, current, attached, inService, false);
registerWithClassicLoadBalancers(elbClassicService, current, classicAttached, inService, false);
}
}
}
@@ -1706,19 +1787,38 @@ public class AutoScalingService implements PassedRoleLookup {
*/
private void deregisterFromUnsharedTargetGroups(AutoScalingGroup asg, List<String> instanceIds,
Set<String> targetGroupArns) {
if (targetGroupArns.isEmpty()) {
forEachUnshared(asg, instanceIds, targetGroupArns, AutoScalingService::attachedTargetGroups,
(instanceId, leaving) -> deregisterFromTargetGroups(elbV2Service, asg, leaving, List.of(instanceId)));
}
/** As {@link #deregisterFromUnsharedTargetGroups}, from Classic Load Balancers. */
private void deregisterFromUnsharedClassicLoadBalancers(AutoScalingGroup asg, List<String> instanceIds,
Set<String> loadBalancerNames) {
forEachUnshared(asg, instanceIds, loadBalancerNames, AutoScalingService::attachedClassicLoadBalancers,
(instanceId, leaving) -> deregisterFromClassicLoadBalancers(elbClassicService, asg, leaving,
List.of(instanceId)));
}
/**
* Calls {@code deregister} for each of {@code instanceIds} with those of {@code loadBalancers} that no other group
* holding the instance has attached, as {@code attachedOf} reads a group's.
*/
private void forEachUnshared(AutoScalingGroup asg, List<String> instanceIds, Set<String> loadBalancers,
Function<AutoScalingGroup, Set<String>> attachedOf,
BiConsumer<String, Set<String>> deregister) {
if (loadBalancers.isEmpty()) {
return;
}
for (String instanceId : instanceIds) {
Set<String> leaving = new LinkedHashSet<>(targetGroupArns);
Set<String> leaving = new LinkedHashSet<>(loadBalancers);
forEachGroupInRegion(asg.getRegion(), other -> {
if (!asg.getAutoScalingGroupName().equals(other.getAutoScalingGroupName())
&& containsInstance(other, instanceId)) {
leaving.removeAll(attachedTargetGroups(other));
leaving.removeAll(attachedOf.apply(other));
}
});
if (!leaving.isEmpty()) {
deregisterFromTargetGroups(elbV2Service, asg, leaving, List.of(instanceId));
deregister.accept(instanceId, leaving);
}
}
}
@@ -2011,8 +2011,8 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
/**
* Deletes the Auto Scaling group {@code nodeGroup} reports, which takes the cluster's node out of its target
* groups, except those another node group's group has attached. A failure is logged, so the node group, and
* the other node groups of a cluster being deleted, are deleted all the same.
* groups and Classic Load Balancers, except those another node group's group has attached. A failure is logged,
* so the node group, and the other node groups of a cluster being deleted, are deleted all the same.
*/
private void deleteNodeGroupAutoScalingGroup(String accountId, String region, Nodegroup nodeGroup) {
String groupName = autoScalingGroupName(nodeGroup);