mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(autoscaling): retain indexed instance mutation members (#7217)
* fix(autoscaling): retain indexed instance mutation members Use the existing indexed member parser for AttachInstances, DetachInstances and SetInstanceProtection. Keep all members for service validation before mutation, including after gaps. Preserve existing validation and protocol responses. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(autoscaling): count actual detached instances Keep every collected instance ID available to validation, but decrement desired capacity only for members actually detached. Cover repeated query values and ignored unknown IDs without changing the false decrement flag. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com> * test: consolidate Auto Scaling indexed instance fixtures Cover all five instance-list actions with one sparse, large, repeated and ignored-key parameterized map. Preserve validation-before-mutation and repeated-detach capacity regressions. Addresses upstream review 5729806284. Signed-off-by: Andre Kurait <akurait@amazon.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
87936c19b3
commit
3c5e8936f2
4 files changed
+104
-27
No files matched your search
+3
-3
@@ -638,7 +638,7 @@ public class AutoScalingQueryHandler {
|
||||
|
||||
private Response handleSetInstanceProtection(MultivaluedMap<String, String> p, String region) {
|
||||
String groupName = p.getFirst("AutoScalingGroupName");
|
||||
List<String> instanceIds = memberList(p, "InstanceIds");
|
||||
List<String> instanceIds = indexedMembers(p, "InstanceIds");
|
||||
boolean protectedFromScaleIn = requiredBoolParam(p, "ProtectedFromScaleIn");
|
||||
service.setInstanceProtection(region, groupName, instanceIds, protectedFromScaleIn);
|
||||
return ok(new XmlBuilder()
|
||||
@@ -893,7 +893,7 @@ public class AutoScalingQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleAttachInstances(MultivaluedMap<String, String> p, String region) {
|
||||
service.attachInstances(region, p.getFirst("AutoScalingGroupName"), memberList(p, "InstanceIds"));
|
||||
service.attachInstances(region, p.getFirst("AutoScalingGroupName"), indexedMembers(p, "InstanceIds"));
|
||||
return ok(new XmlBuilder()
|
||||
.start("AttachInstancesResponse", NS)
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
@@ -902,7 +902,7 @@ public class AutoScalingQueryHandler {
|
||||
|
||||
private Response handleDetachInstances(MultivaluedMap<String, String> p, String region) {
|
||||
service.detachInstances(region, p.getFirst("AutoScalingGroupName"),
|
||||
memberList(p, "InstanceIds"),
|
||||
indexedMembers(p, "InstanceIds"),
|
||||
"true".equalsIgnoreCase(p.getFirst("ShouldDecrementDesiredCapacity")));
|
||||
return ok(new XmlBuilder()
|
||||
.start("DetachInstancesResponse", NS)
|
||||
|
||||
+1
-1
@@ -1016,7 +1016,7 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
releaseRefreshOwnership(asg, detached);
|
||||
asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId()));
|
||||
if (decrementDesiredCapacity) {
|
||||
int newDesired = Math.max(asg.getMinSize(), asg.getDesiredCapacity() - instanceIds.size());
|
||||
int newDesired = Math.max(asg.getMinSize(), asg.getDesiredCapacity() - detached.size());
|
||||
asg.setDesiredCapacity(newDesired);
|
||||
}
|
||||
groups.put(key, asg);
|
||||
|
||||
+92
-20
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -13,7 +14,6 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -29,37 +29,109 @@ class AutoScalingQueryHandlerTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"EnterStandby", "ExitStandby"})
|
||||
void standbyMembersRetainNumericOrderAndRepeatedParameterValues(String action) {
|
||||
@ValueSource(booleans = {false, true})
|
||||
void repeatedDetachMembersCountEachDetachedInstanceOnce(boolean decrementDesiredCapacity) {
|
||||
AutoScalingService service = new AutoScalingService();
|
||||
service.regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
service.createAutoScalingGroup(REGION, "repeated-detach", null, "lt", null, "1", null,
|
||||
0, 3, 3, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
|
||||
"EC2", 0, List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none());
|
||||
service.ec2Service = mock(Ec2Service.class);
|
||||
AutoScalingGroup group = service.describeAutoScalingGroups(REGION, List.of("repeated-detach")).getFirst();
|
||||
for (String id : List.of("i-detached", "i-retained-one", "i-retained-two")) {
|
||||
AsgInstance instance = new AsgInstance();
|
||||
instance.setInstanceId(id);
|
||||
instance.setLifecycleState("InService");
|
||||
group.getInstances().add(instance);
|
||||
}
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "repeated-detach");
|
||||
params.add("ShouldDecrementDesiredCapacity", Boolean.toString(decrementDesiredCapacity));
|
||||
params.add("InstanceIds.member.1", "i-detached");
|
||||
params.add("InstanceIds.member.1", "i-detached");
|
||||
params.add("InstanceIds.member.3", "i-detached");
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle("DetachInstances", params, REGION);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(decrementDesiredCapacity ? 2 : 3, group.getDesiredCapacity());
|
||||
assertEquals(List.of("i-retained-one", "i-retained-two"),
|
||||
group.getInstances().stream().map(AsgInstance::getInstanceId).toList());
|
||||
verify(service.ec2Service).deleteSystemTag(
|
||||
REGION, List.of("i-detached"), "aws:autoscaling:groupName", "repeated-detach");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"AttachInstances", "DetachInstances", "SetInstanceProtection", "EnterStandby", "ExitStandby"})
|
||||
void instanceMutationsRetainSparseOrderedAndRepeatedMembers(String action) {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "indexed-standby");
|
||||
params.add("AutoScalingGroupName", "indexed-instances");
|
||||
params.add("ShouldDecrementDesiredCapacity", "true");
|
||||
for (int index = 12; index >= 1; index--) {
|
||||
params.add("InstanceIds.member." + index, "i-" + index);
|
||||
}
|
||||
params.add("ProtectedFromScaleIn", "true");
|
||||
params.add("InstanceIds.member.999999999999999999999999", "i-large");
|
||||
params.add("InstanceIds.member.10", "i-ten");
|
||||
params.add("InstanceIds.member.2", "i-two");
|
||||
params.add("InstanceIds.member.2", "i-repeated");
|
||||
List<String> expected = new ArrayList<>();
|
||||
for (int index = 1; index <= 12; index++) {
|
||||
expected.add("i-" + index);
|
||||
if (index == 2) {
|
||||
expected.add("i-repeated");
|
||||
}
|
||||
}
|
||||
when(service.enterStandby(eq(REGION), eq("indexed-standby"), anyList(), eq(true)))
|
||||
.thenReturn(List.of());
|
||||
when(service.exitStandby(eq(REGION), eq("indexed-standby"), anyList())).thenReturn(List.of());
|
||||
params.add("InstanceIds.member.01", "ignored");
|
||||
params.add("InstanceIds.member.3.Other", "ignored");
|
||||
List<String> expected = List.of("i-two", "i-repeated", "i-ten", "i-large");
|
||||
|
||||
assertEquals(200, handler.handle(action, params, REGION).getStatus());
|
||||
|
||||
if ("EnterStandby".equals(action)) {
|
||||
verify(service).enterStandby(REGION, "indexed-standby", expected, true);
|
||||
if ("AttachInstances".equals(action)) {
|
||||
verify(service).attachInstances(REGION, "indexed-instances", expected);
|
||||
} else if ("DetachInstances".equals(action)) {
|
||||
verify(service).detachInstances(REGION, "indexed-instances", expected, true);
|
||||
} else if ("SetInstanceProtection".equals(action)) {
|
||||
verify(service).setInstanceProtection(REGION, "indexed-instances", expected, true);
|
||||
} else if ("EnterStandby".equals(action)) {
|
||||
verify(service).enterStandby(REGION, "indexed-instances", expected, true);
|
||||
} else {
|
||||
verify(service).exitStandby(REGION, "indexed-standby", expected);
|
||||
verify(service).exitStandby(REGION, "indexed-instances", expected);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"AttachInstances", "DetachInstances", "SetInstanceProtection"})
|
||||
void instanceMutationsValidateMembersAfterGapBeforeChangingGroup(String action) {
|
||||
AutoScalingService service = new AutoScalingService();
|
||||
service.regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
service.createAutoScalingGroup(REGION, "indexed-validation", null, "lt", null, "1", null,
|
||||
0, 3, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
|
||||
"EC2", 0, List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none());
|
||||
AutoScalingGroup group = service.describeAutoScalingGroups(REGION, List.of("indexed-validation")).getFirst();
|
||||
service.ec2Service = mock(Ec2Service.class);
|
||||
AsgInstance original = new AsgInstance();
|
||||
original.setInstanceId("i-original");
|
||||
original.setLifecycleState("InService");
|
||||
group.getInstances().add(original);
|
||||
AsgInstance moving = new AsgInstance();
|
||||
moving.setInstanceId("i-moving");
|
||||
moving.setLifecycleState("EnteringStandby");
|
||||
group.getInstances().add(moving);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "indexed-validation");
|
||||
params.add("ProtectedFromScaleIn", "true");
|
||||
params.add("ShouldDecrementDesiredCapacity", "true");
|
||||
params.add("InstanceIds.member.1", "AttachInstances".equals(action) ? "i-new" : "i-original");
|
||||
params.add("InstanceIds.member.3", "SetInstanceProtection".equals(action) ? "i-missing" : "i-moving");
|
||||
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
|
||||
|
||||
Response response = handler.handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(response.getEntity().toString().contains("<Code>ValidationError</Code>"));
|
||||
assertEquals(List.of("i-original", "i-moving"),
|
||||
group.getInstances().stream().map(AsgInstance::getInstanceId).toList());
|
||||
assertEquals(1, group.getDesiredCapacity());
|
||||
assertEquals("InService", original.getLifecycleState());
|
||||
assertEquals("EnteringStandby", moving.getLifecycleState());
|
||||
assertFalse(original.isProtectedFromScaleIn());
|
||||
verifyNoInteractions(service.ec2Service);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"CapacityReservationIds,cr-0123456789abcdef0",
|
||||
|
||||
+8
-3
@@ -228,15 +228,20 @@ class AutoScalingServiceTest {
|
||||
assertEquals(decrementDesiredCapacity ? 1 : 2, group.getDesiredCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detachingAnInstanceTheGroupDoesNotHoldChangesNoTags() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void detachingAnInstanceTheGroupDoesNotHoldChangesNoTags(boolean decrementDesiredCapacity) {
|
||||
service.ec2Service = mock(Ec2Service.class);
|
||||
service.attachInstances(REGION, "test-asg", List.of("i-member"));
|
||||
int desiredCapacity = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst()
|
||||
.getDesiredCapacity();
|
||||
clearInvocations(service.ec2Service);
|
||||
|
||||
service.detachInstances(REGION, "test-asg", List.of("i-stranger"), false);
|
||||
service.detachInstances(REGION, "test-asg", List.of("i-stranger"), decrementDesiredCapacity);
|
||||
|
||||
verify(service.ec2Service, never()).deleteSystemTag(anyString(), anyList(), anyString(), anyString());
|
||||
assertEquals(desiredCapacity, service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst()
|
||||
.getDesiredCapacity());
|
||||
assertEquals(List.of("i-member"), service.describeAutoScalingGroups(REGION, List.of("test-asg"))
|
||||
.getFirst().getInstances().stream().map(AsgInstance::getInstanceId).toList());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user