mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(autoscaling): scope target cleanup to group-owned instances (#7038)
* fix(autoscaling): preserve group-owned target cleanup across lifecycle paths Preserve the reviewed strict shared cleanup, Classic load-balancer detach behavior, and failure ownership for retries. Reconcile current main test SDK dependencies without changing the reviewed Java sources. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> * fix(autoscaling): share Classic cleanup and retain failed members Use the same Classic load-balancer deregistration helper for detach and every reconciler cleanup path. Preserve members for retry on actual failures, tolerate deleted balancers, and move detach cases to the existing complete fixture. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
9f0856c691
commit
e714fd9594
8 files changed
+646
-92
No files matched your search
+10
-72
@@ -22,7 +22,6 @@ import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.elb.ElbClassicService;
|
||||
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmCommandService;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
@@ -144,7 +143,6 @@ public class AutoScalingReconciler {
|
||||
settleStandbyMoves(asg);
|
||||
removeTerminatingInstances(asg);
|
||||
removeStaleInstances(asg);
|
||||
removeOrphanedTargetRegistrations(asg);
|
||||
promoteReadyInstances(asg);
|
||||
reconcileRefreshReplacements(asg);
|
||||
|
||||
@@ -255,8 +253,8 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
if (!entering.isEmpty()) {
|
||||
List<String> instanceIds = entering.stream().map(AsgInstance::getInstanceId).toList();
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
AutoScalingService.deregisterFromTargetGroups(elbV2Service, asg, instanceIds);
|
||||
AutoScalingService.deregisterFromClassicLoadBalancers(elbClassicService, asg, instanceIds);
|
||||
for (AsgInstance instance : entering) {
|
||||
instance.setLifecycleState("Standby");
|
||||
if (instance.getStandbyActivityId() != null) {
|
||||
@@ -343,8 +341,8 @@ public class AutoScalingReconciler {
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.collect(Collectors.toList());
|
||||
failActiveSsmInvocations(asg, instanceIds);
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
AutoScalingService.deregisterFromTargetGroups(elbV2Service, asg, instanceIds);
|
||||
AutoScalingService.deregisterFromClassicLoadBalancers(elbClassicService, asg, instanceIds);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
@@ -376,8 +374,8 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
}
|
||||
failActiveSsmInvocations(asg, instanceIds);
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
AutoScalingService.deregisterFromTargetGroups(elbV2Service, asg, instanceIds);
|
||||
AutoScalingService.deregisterFromClassicLoadBalancers(elbClassicService, asg, instanceIds);
|
||||
asgService.cancelStandbyMoves(returning);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
@@ -455,39 +453,6 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
}
|
||||
|
||||
private void removeOrphanedTargetRegistrations(AutoScalingGroup asg) {
|
||||
if (asg.getTargetGroupARNs().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> activeInstanceIds = asg.getInstances().stream()
|
||||
.filter(instance -> isActiveLifecycleState(instance.getLifecycleState())
|
||||
|| pausedRefreshOwns(asg, instance.getInstanceId()))
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
for (String tgArn : asg.getTargetGroupARNs()) {
|
||||
try {
|
||||
List<TargetDescription> orphanedTargets = elbV2Service.describeTargetHealth(
|
||||
asg.getRegion(), tgArn, List.of()).stream()
|
||||
.map(TargetHealth::getTarget)
|
||||
.filter(target -> target != null && target.getId() != null)
|
||||
.filter(target -> !activeInstanceIds.contains(target.getId()))
|
||||
.collect(Collectors.toList());
|
||||
if (!orphanedTargets.isEmpty()) {
|
||||
elbV2Service.deregisterTargets(asg.getRegion(), tgArn, orphanedTargets);
|
||||
LOG.infov("ASG {0}: deregistered orphaned target(s) {1} from TG {2}",
|
||||
asg.getAutoScalingGroupName(),
|
||||
orphanedTargets.stream().map(TargetDescription::getId).collect(Collectors.toList()),
|
||||
tgArn);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("ASG {0}: could not reconcile TG {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), tgArn, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeTerminatingInstances(AutoScalingGroup asg) {
|
||||
List<AsgInstance> terminatingInstances = asg.getInstances().stream()
|
||||
.filter(instance -> "Terminating".equals(instance.getLifecycleState()))
|
||||
@@ -522,8 +487,8 @@ public class AutoScalingReconciler {
|
||||
return;
|
||||
}
|
||||
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
AutoScalingService.deregisterFromTargetGroups(elbV2Service, asg, instanceIds);
|
||||
AutoScalingService.deregisterFromClassicLoadBalancers(elbClassicService, asg, instanceIds);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
@@ -856,8 +821,8 @@ public class AutoScalingReconciler {
|
||||
return;
|
||||
}
|
||||
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
AutoScalingService.deregisterFromTargetGroups(elbV2Service, asg, instanceIds);
|
||||
AutoScalingService.deregisterFromClassicLoadBalancers(elbClassicService, asg, instanceIds);
|
||||
|
||||
asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
@@ -907,19 +872,6 @@ public class AutoScalingReconciler {
|
||||
return message.substring(0, end) + suffix;
|
||||
}
|
||||
|
||||
private void deregisterFromTargetGroups(AutoScalingGroup asg, List<String> instanceIds) {
|
||||
for (String tgArn : asg.getTargetGroupARNs()) {
|
||||
try {
|
||||
List<TargetDescription> targets = instanceIds.stream()
|
||||
.map(id -> { TargetDescription td = new TargetDescription(); td.setId(id); return td; })
|
||||
.collect(Collectors.toList());
|
||||
elbV2Service.deregisterTargets(asg.getRegion(), tgArn, targets);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("ASG {0}: could not deregister from TG {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), tgArn, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a newly launched instance with every Classic load balancer the group names.
|
||||
@@ -946,20 +898,6 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
}
|
||||
|
||||
private void deregisterFromClassicLoadBalancers(AutoScalingGroup asg, List<String> instanceIds) {
|
||||
if (elbClassicService == null) {
|
||||
return;
|
||||
}
|
||||
for (String lbName : asg.getLoadBalancerNames()) {
|
||||
try {
|
||||
elbClassicService.deregisterInstances(asg.getRegion(), lbName, instanceIds);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("ASG {0}: could not deregister from Classic ELB {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), lbName, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerWithTargetGroups(AutoScalingGroup asg, AsgInstance asgInst) {
|
||||
for (String tgArn : asg.getTargetGroupARNs()) {
|
||||
try {
|
||||
|
||||
@@ -17,6 +17,9 @@ import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.elb.ElbClassicService;
|
||||
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
|
||||
import io.github.hectorvent.floci.services.iam.PassedRoleLookup;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -126,6 +129,19 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
@Inject
|
||||
Ec2Service ec2Service;
|
||||
|
||||
private final ElbV2Service elbV2Service;
|
||||
private final ElbClassicService elbClassicService;
|
||||
|
||||
@Inject
|
||||
public AutoScalingService(ElbV2Service elbV2Service, ElbClassicService elbClassicService) {
|
||||
this.elbV2Service = elbV2Service;
|
||||
this.elbClassicService = elbClassicService;
|
||||
}
|
||||
|
||||
AutoScalingService() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
// region :: name → resource
|
||||
private Map<String, LaunchConfiguration> launchConfigs = new ConcurrentHashMap<>();
|
||||
Map<String, AutoScalingGroup> groups = new ConcurrentHashMap<>();
|
||||
@@ -940,6 +956,39 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
}
|
||||
}
|
||||
|
||||
static void deregisterFromTargetGroups(ElbV2Service elb, AutoScalingGroup asg, List<String> instanceIds) {
|
||||
List<TargetDescription> targets = instanceIds.stream().map(id -> {
|
||||
TargetDescription target = new TargetDescription();
|
||||
target.setId(id);
|
||||
return target;
|
||||
}).toList();
|
||||
for (String targetGroup : asg.getTargetGroupARNs()) {
|
||||
try {
|
||||
elb.deregisterTargets(asg.getRegion(), targetGroup, targets);
|
||||
} catch (AwsException e) {
|
||||
if (!"TargetGroupNotFound".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
LOG.debugv("ASG {0}: target group {1} is already deleted", asg.getAutoScalingGroupName(), targetGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void deregisterFromClassicLoadBalancers(ElbClassicService elb, AutoScalingGroup asg,
|
||||
List<String> instanceIds) {
|
||||
for (String loadBalancer : asg.getLoadBalancerNames()) {
|
||||
try {
|
||||
elb.deregisterInstances(asg.getRegion(), loadBalancer, instanceIds);
|
||||
} catch (AwsException e) {
|
||||
if (!"LoadBalancerNotFound".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
LOG.debugv("ASG {0}: Classic load balancer {1} is already deleted",
|
||||
asg.getAutoScalingGroupName(), loadBalancer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void detachInstances(String region, String name, List<String> instanceIds,
|
||||
boolean decrementDesiredCapacity) {
|
||||
String key = asgKey(region, name);
|
||||
@@ -959,6 +1008,8 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
List<String> detached = asg.getInstances().stream().map(AsgInstance::getInstanceId)
|
||||
.filter(instanceIds::contains).toList();
|
||||
if (!detached.isEmpty()) {
|
||||
deregisterFromTargetGroups(elbV2Service, 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user