mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(autoscaling): apply group capacity reservation preferences (#6378)
Honor group preferences and inherited targets in the shared launch path. Preserve failed launch state, limit fallback to capacity shortages, and validate all supplied numeric Query target members. Reuse existing SDK coverage. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
f7c9d9a993
commit
ceaad92e06
7 files changed
+311
-23
No files matched your search
+99
@@ -0,0 +1,99 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class AutoScalingCapacityTargetQueryIntegrationTest {
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261023/us-east-1/autoscaling/aws4_request";
|
||||
private static final String MEMBER =
|
||||
"DescribeAutoScalingGroupsResponse.DescribeAutoScalingGroupsResult.AutoScalingGroups.member";
|
||||
private static final String TARGET = "CapacityReservationSpecification.CapacityReservationTarget.";
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CapacityReservationIds,false", "CapacityReservationIds,true",
|
||||
"CapacityReservationResourceGroupArns,false", "CapacityReservationResourceGroupArns,true"})
|
||||
void sparseMultipleTargetsCannotMutateGroup(String target, boolean update) {
|
||||
String name = "sparse-target-" + UUID.randomUUID();
|
||||
createConfiguration(name);
|
||||
try {
|
||||
if (update) {
|
||||
create(name).post("/").then().statusCode(200);
|
||||
}
|
||||
RequestSpecification request = update ? request("UpdateAutoScalingGroup", name)
|
||||
.formParam("DesiredCapacity", "1") : create(name);
|
||||
request.formParam("CapacityReservationSpecification.CapacityReservationPreference",
|
||||
"capacity-reservations-only")
|
||||
.formParam(TARGET + target + ".member.1", "target-one")
|
||||
.formParam(TARGET + target + ".member.3", "target-three")
|
||||
.post("/").then().statusCode(400).body(containsString("ValidationError"));
|
||||
describe(name).body(MEMBER + ".size()", equalTo(update ? 1 : 0));
|
||||
if (update) {
|
||||
describe(name).body(MEMBER + ".DesiredCapacity", equalTo("0"))
|
||||
.body(MEMBER + ".CapacityReservationSpecification.size()", equalTo(0));
|
||||
}
|
||||
} finally {
|
||||
cleanUp(name);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CapacityReservationIds", "CapacityReservationResourceGroupArns"})
|
||||
void sparseSingletonTargetIsRetained(String target) {
|
||||
String name = "sparse-single-" + UUID.randomUUID();
|
||||
createConfiguration(name);
|
||||
try {
|
||||
create(name).formParam("CapacityReservationSpecification.CapacityReservationPreference",
|
||||
"capacity-reservations-only")
|
||||
.formParam(TARGET + target + ".member.3", "target-three")
|
||||
.post("/").then().statusCode(200);
|
||||
describe(name).body(MEMBER + ".CapacityReservationSpecification.CapacityReservationTarget."
|
||||
+ target + ".member", equalTo("target-three"));
|
||||
} finally {
|
||||
cleanUp(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static RequestSpecification request(String action, String name) {
|
||||
return given().header("Authorization", AUTH).formParam("Action", action)
|
||||
.formParam("AutoScalingGroupName", name);
|
||||
}
|
||||
|
||||
private static RequestSpecification create(String name) {
|
||||
return request("CreateAutoScalingGroup", name).formParam("LaunchConfigurationName", name)
|
||||
.formParam("MinSize", "0").formParam("MaxSize", "1").formParam("DesiredCapacity", "0")
|
||||
.formParam("AvailabilityZones.member.1", "us-east-1a");
|
||||
}
|
||||
|
||||
private static ValidatableResponse describe(String name) {
|
||||
return given().header("Authorization", AUTH).formParam("Action", "DescribeAutoScalingGroups")
|
||||
.formParam("AutoScalingGroupNames.member.1", name).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void createConfiguration(String name) {
|
||||
given().header("Authorization", AUTH).formParam("Action", "CreateLaunchConfiguration")
|
||||
.formParam("LaunchConfigurationName", name).formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t3.small").post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void cleanUp(String name) {
|
||||
int count = describe(name).extract().xmlPath().getInt(MEMBER + ".size()");
|
||||
if (count > 0) {
|
||||
request("DeleteAutoScalingGroup", name).formParam("ForceDelete", "true")
|
||||
.post("/").then().statusCode(200);
|
||||
}
|
||||
given().header("Authorization", AUTH).formParam("Action", "DeleteLaunchConfiguration")
|
||||
.formParam("LaunchConfigurationName", name).post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
+90
-6
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.autoscaling;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.CapacityReservationSpecification;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.CapacityReservation;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
@@ -10,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
@@ -35,10 +37,56 @@ class AutoScalingReservationOnlyIntegrationTest {
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void templateReservationOnlyDoesNotFallBack(boolean available) {
|
||||
checkReservationPreference(available, "capacity-reservations-only", null, false, available ? 1 : 0,
|
||||
available, null);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"false,none,capacity-reservations-only,false,0,false",
|
||||
"true,none,capacity-reservations-only,false,1,true",
|
||||
"false,none,capacity-reservations-only,true,0,false",
|
||||
"true,none,capacity-reservations-only,true,1,true",
|
||||
"true,capacity-reservations-only,none,false,1,false",
|
||||
"true,capacity-reservations-only,none,true,1,false",
|
||||
"false,capacity-reservations-only,capacity-reservations-first,false,1,false",
|
||||
"true,none,capacity-reservations-first,false,1,true",
|
||||
"false,capacity-reservations-only,default,false,0,false",
|
||||
"true,none,default,false,1,false"
|
||||
})
|
||||
void groupPreferenceControlsLaunch(boolean available, String templatePreference, String groupPreference,
|
||||
boolean update, int expectedInstances, boolean expectedReserved) {
|
||||
checkReservationPreference(available, templatePreference, groupPreference, update, expectedInstances,
|
||||
expectedReserved, null);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"capacity-reservations-only,matching,1,true",
|
||||
"capacity-reservations-first,matching,1,true",
|
||||
"capacity-reservations-only,inherited-mismatch,0,false",
|
||||
"capacity-reservations-first,inherited-mismatch,1,false",
|
||||
"capacity-reservations-only,override-template,1,true",
|
||||
"none,inherited-mismatch,1,false",
|
||||
"capacity-reservations-only,mismatch,0,false",
|
||||
"capacity-reservations-first,mismatch,1,false",
|
||||
"capacity-reservations-only,missing,0,false",
|
||||
"capacity-reservations-first,missing,0,false",
|
||||
"capacity-reservations-only,resource-group,0,false",
|
||||
"capacity-reservations-first,resource-group,0,false"
|
||||
})
|
||||
void groupTargetsDoNotConsumeUnrelatedOpenCapacity(String preference, String targets,
|
||||
int expectedInstances, boolean expectedReserved) {
|
||||
checkReservationPreference(true, "open", preference, false, expectedInstances, expectedReserved, targets);
|
||||
}
|
||||
|
||||
private void checkReservationPreference(boolean available, String templatePreference, String groupPreference,
|
||||
boolean update, int expectedInstances, boolean expectedReserved, String targets) {
|
||||
RequestScopes.runAs(ACCOUNT, REGION, () -> {
|
||||
CapacityReservation capacity = ec2.createCapacityReservation(REGION, "t3.small", "Linux/UNIX",
|
||||
REGION + "a", null, 1, null, null, null, null, null, null, null, null);
|
||||
String groupName = "reservation-only-" + UUID.randomUUID();
|
||||
CapacityReservation mismatch = null;
|
||||
LaunchTemplate template = null;
|
||||
boolean groupCreated = false;
|
||||
try {
|
||||
@@ -50,25 +98,58 @@ class AutoScalingReservationOnlyIntegrationTest {
|
||||
data.setInstanceType("t3.small");
|
||||
LaunchTemplateData.CapacityReservationSpecification specification =
|
||||
new LaunchTemplateData.CapacityReservationSpecification();
|
||||
specification.setCapacityReservationPreference("capacity-reservations-only");
|
||||
specification.setCapacityReservationPreference(templatePreference);
|
||||
data.setCapacityReservationSpecification(specification);
|
||||
template = ec2.createLaunchTemplate(REGION, groupName, data, List.of());
|
||||
String subnet = ec2.describeSubnets(REGION, List.of(), Map.of()).stream()
|
||||
.filter(value -> (REGION + "a").equals(value.getAvailabilityZone()))
|
||||
.findFirst().orElseThrow().getSubnetId();
|
||||
CapacityReservationSpecification groupSpec =
|
||||
new CapacityReservationSpecification();
|
||||
groupSpec.setCapacityReservationPreference(groupPreference);
|
||||
if (targets != null) {
|
||||
if ("resource-group".equals(targets)) {
|
||||
groupSpec.setCapacityReservationResourceGroupArns(
|
||||
List.of("arn:aws:resource-groups:us-east-1:" + ACCOUNT + ":group/test"));
|
||||
} else if ("missing".equals(targets)) {
|
||||
groupSpec.setCapacityReservationIds(List.of("cr-00000000000000000"));
|
||||
} else {
|
||||
mismatch = ec2.createCapacityReservation(REGION, "t3.micro", "Linux/UNIX",
|
||||
REGION + "a", null, 1, null, null, null, null, null, null, null, null);
|
||||
if ("inherited-mismatch".equals(targets) || "override-template".equals(targets)) {
|
||||
LaunchTemplateData.CapacityReservationTarget templateTarget =
|
||||
new LaunchTemplateData.CapacityReservationTarget();
|
||||
templateTarget.setCapacityReservationId(mismatch.getCapacityReservationId());
|
||||
specification.setCapacityReservationTarget(templateTarget);
|
||||
ec2.createLaunchTemplateVersion(REGION, template.getLaunchTemplateId(), null,
|
||||
"1", data, null);
|
||||
}
|
||||
if (!"inherited-mismatch".equals(targets)) {
|
||||
groupSpec.setCapacityReservationIds(List.of(
|
||||
"matching".equals(targets) || "override-template".equals(targets)
|
||||
? capacity.getCapacityReservationId() : mismatch.getCapacityReservationId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
AsgOptionalFields options = groupPreference == null ? AsgOptionalFields.none()
|
||||
: new AsgOptionalFields(null, null, null, null, null, null, null, groupSpec);
|
||||
AutoScalingGroup group = service.createAutoScalingGroup(REGION, groupName, null,
|
||||
template.getLaunchTemplateId(), null, "1", null, 0, 1, 1, 300,
|
||||
template.getLaunchTemplateId(), null, "$Latest", null, 0, 1, update ? 0 : 1, 300,
|
||||
List.of(REGION + "a"), List.of(subnet), List.of(), List.of(), "EC2", 0,
|
||||
List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none());
|
||||
List.of("Default"), Map.of(), Map.of(), update ? AsgOptionalFields.none() : options);
|
||||
groupCreated = true;
|
||||
if (update) {
|
||||
service.updateAutoScalingGroup(REGION, groupName, null, null, null, null, null,
|
||||
null, null, 1, null, null, null, null, null, null, options);
|
||||
}
|
||||
reconciler.reconcile(group);
|
||||
AutoScalingGroup current = service.describeAutoScalingGroups(REGION, List.of(groupName)).getFirst();
|
||||
assertEquals(available ? 1 : 0, current.getInstances().size());
|
||||
assertEquals(available ? 1 : 0, ec2.describeInstances(REGION, List.of(), Map.of()).stream()
|
||||
assertEquals(expectedInstances, current.getInstances().size());
|
||||
assertEquals(expectedInstances, ec2.describeInstances(REGION, List.of(), Map.of()).stream()
|
||||
.flatMap(value -> value.getInstances().stream())
|
||||
.filter(value -> !"terminated".equals(value.getState().getName())).count());
|
||||
if (available) {
|
||||
assertEquals(List.of(current.getInstances().getFirst().getInstanceId()),
|
||||
assertEquals(expectedReserved ? List.of(current.getInstances().getFirst().getInstanceId()) : List.of(),
|
||||
ec2.describeCapacityReservations(REGION, List.of(capacity.getCapacityReservationId()),
|
||||
Map.of()).getFirst().getInstanceIds());
|
||||
}
|
||||
@@ -79,6 +160,9 @@ class AutoScalingReservationOnlyIntegrationTest {
|
||||
if (template != null) {
|
||||
ec2.deleteLaunchTemplate(REGION, template.getLaunchTemplateId(), null);
|
||||
}
|
||||
if (mismatch != null) {
|
||||
ec2.cancelCapacityReservation(REGION, mismatch.getCapacityReservationId());
|
||||
}
|
||||
if (available) {
|
||||
ec2.cancelCapacityReservation(REGION, capacity.getCapacityReservationId());
|
||||
}
|
||||
|
||||
+28
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -1449,6 +1450,33 @@ class AutoScalingServiceTest {
|
||||
assertEquals(AutoScalingService.CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE, error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"ids", "groups", "mixed"})
|
||||
void capacityReservationRejectsMultipleTargetsBeforeCreateOrUpdate(String kind) {
|
||||
CapacityReservationSpecification specification = new CapacityReservationSpecification();
|
||||
specification.setCapacityReservationPreference("capacity-reservations-only");
|
||||
if ("ids".equals(kind)) {
|
||||
specification.setCapacityReservationIds(List.of("cr-one", "cr-two"));
|
||||
} else if ("groups".equals(kind)) {
|
||||
specification.setCapacityReservationResourceGroupArns(List.of("group-one", "group-two"));
|
||||
} else {
|
||||
specification.setCapacityReservationIds(List.of("cr-one"));
|
||||
specification.setCapacityReservationResourceGroupArns(List.of("group-one"));
|
||||
}
|
||||
AsgOptionalFields options = new AsgOptionalFields(null, null, null, null, null, null, null, specification);
|
||||
AwsException create = assertThrows(AwsException.class, () -> createWithStructures("multi-target", options));
|
||||
assertEquals("ValidationError", create.getErrorCode());
|
||||
assertTrue(service.describeAutoScalingGroups(REGION, List.of("multi-target")).isEmpty());
|
||||
AutoScalingGroup before = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst();
|
||||
int desired = before.getDesiredCapacity();
|
||||
AwsException update = assertThrows(AwsException.class, () -> service.updateAutoScalingGroup(REGION,
|
||||
"test-asg", null, null, null, null, null, null, null, 2, null, null, null, null, null, null, options));
|
||||
assertEquals("ValidationError", update.getErrorCode());
|
||||
AutoScalingGroup after = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst();
|
||||
assertEquals(desired, after.getDesiredCapacity());
|
||||
assertNull(after.getCapacityReservationSpecification());
|
||||
}
|
||||
|
||||
@Test
|
||||
void capacityReservationTargetMembersFollowTheirModelShapes() {
|
||||
CapacityReservationSpecification badId = new CapacityReservationSpecification();
|
||||
|
||||
Reference in new issue
Block a user