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:
Andre Kurait authored and GitHub committed 2026-10-23 18:23:50 -04:00
1 parent f7c9d9a993
commit ceaad92e06
7 files changed
+311 -23

No files matched your search

@@ -1666,12 +1666,21 @@ public class AutoScalingQueryHandler {
CapacityReservationSpecification specification = new CapacityReservationSpecification();
specification.setCapacityReservationPreference(p.getFirst(prefix + "CapacityReservationPreference"));
specification.setCapacityReservationIds(
memberList(p, prefix + "CapacityReservationTarget.CapacityReservationIds"));
capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationIds"));
specification.setCapacityReservationResourceGroupArns(
memberList(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns"));
capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns"));
return specification;
}
private List<String> capacityTargetMembers(MultivaluedMap<String, String> params, String prefix) {
String memberPrefix = prefix + ".member.";
return params.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(memberPrefix))
.filter(entry -> entry.getKey().substring(memberPrefix.length()).matches("[1-9][0-9]*"))
.flatMap(entry -> entry.getValue().stream())
.toList();
}
private ParsedTags parseTags(MultivaluedMap<String, String> p) {
Map<String, String> tags = new LinkedHashMap<>();
Map<String, Boolean> propagateAtLaunch = new LinkedHashMap<>();
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
import io.github.hectorvent.floci.services.autoscaling.model.CapacityReservationSpecification;
import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefresh;
import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshLaunch;
import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshReplacement;
@@ -538,6 +539,71 @@ public class AutoScalingReconciler {
return launchedInstanceIds;
}
private Reservation launchWithGroupCapacity(AutoScalingGroup asg, LaunchSource launchSource,
LaunchPlacement placement, String clientToken) {
CapacityReservationSpecification groupSpecification = asg.getCapacityReservationSpecification();
AutoScalingService.validateCapacityReservationSpecification(groupSpecification);
String preference = groupSpecification == null ? null
: groupSpecification.getCapacityReservationPreference();
if (preference == null || "default".equals(preference)) {
return launchWithCapacity(asg, launchSource, placement, clientToken,
launchSource.capacityReservationSpecification());
}
LaunchTemplateData.CapacityReservationSpecification specification =
new LaunchTemplateData.CapacityReservationSpecification();
if (!"none".equals(preference)) {
specification.setCapacityReservationTarget(capacityTarget(groupSpecification,
launchSource.capacityReservationSpecification()));
}
boolean fallback = "capacity-reservations-first".equals(preference);
specification.setCapacityReservationPreference(fallback
? specification.getCapacityReservationTarget() == null ? "open" : "capacity-reservations-only"
: preference);
try {
return launchWithCapacity(asg, launchSource, placement, clientToken, specification);
} catch (AwsException failure) {
if (!fallback || !Set.of("InsufficientInstanceCapacity", "ReservationCapacityExceeded")
.contains(failure.getErrorCode())) {
throw failure;
}
specification.setCapacityReservationPreference("none");
specification.setCapacityReservationTarget(null);
return launchWithCapacity(asg, launchSource, placement, clientToken, specification);
}
}
private static LaunchTemplateData.CapacityReservationTarget capacityTarget(
CapacityReservationSpecification groupSpecification,
LaunchTemplateData.CapacityReservationSpecification templateSpecification) {
if (!groupSpecification.hasTarget()) {
return templateSpecification == null ? null : templateSpecification.getCapacityReservationTarget();
}
LaunchTemplateData.CapacityReservationTarget target = new LaunchTemplateData.CapacityReservationTarget();
if (!groupSpecification.getCapacityReservationIds().isEmpty()) {
target.setCapacityReservationId(groupSpecification.getCapacityReservationIds().getFirst());
} else {
target.setCapacityReservationResourceGroupArn(
groupSpecification.getCapacityReservationResourceGroupArns().getFirst());
}
return target;
}
private Reservation launchWithCapacity(AutoScalingGroup asg, LaunchSource launchSource,
LaunchPlacement placement, String clientToken,
LaunchTemplateData.CapacityReservationSpecification capacitySpecification) {
return ec2Service.runInstances(
asg.getRegion(), launchSource.imageId(), launchSource.instanceType(), 1, 1,
launchSource.keyName(), launchSource.securityGroupIds(), placement.subnetId(), clientToken,
propagatedInstanceTags(asg, launchSource), launchSource.userData(),
launchSource.launchConfigurationName() == null ? launchSource.iamInstanceProfile()
: asgService.resolveLaunchConfigurationProfile(launchSource.iamInstanceProfile()),
launchSource.associatePublicIpAddress(), null, 0, null, launchSource.metadataOptions(), null,
launchSource.encodedUserData() != null ? launchSource.encodedUserData()
: encodeLegacyUserData(launchSource.userData()),
false, null, null, launchSource.blockDeviceMappings(), false, null,
capacitySpecification);
}
private List<Instance> launchInstance(AutoScalingGroup asg, LaunchSource launchSource,
LaunchPlacement placement, String clientToken,
InstanceRefreshLaunch metadata, List<Instance> existing,
@@ -546,17 +612,7 @@ public class AutoScalingReconciler {
if (instances.isEmpty()) {
Reservation reservation;
try {
reservation = ec2Service.runInstances(
asg.getRegion(), launchSource.imageId(), launchSource.instanceType(), 1, 1,
launchSource.keyName(), launchSource.securityGroupIds(), placement.subnetId(), clientToken,
propagatedInstanceTags(asg, launchSource), launchSource.userData(),
launchSource.launchConfigurationName() == null ? launchSource.iamInstanceProfile()
: asgService.resolveLaunchConfigurationProfile(launchSource.iamInstanceProfile()),
launchSource.associatePublicIpAddress(), null, 0, null, launchSource.metadataOptions(), null,
launchSource.encodedUserData() != null ? launchSource.encodedUserData()
: encodeLegacyUserData(launchSource.userData()),
false, null, null, launchSource.blockDeviceMappings(), false, null,
launchSource.capacityReservationSpecification());
reservation = launchWithGroupCapacity(asg, launchSource, placement, clientToken);
} catch (RuntimeException launchFailure) {
if (!refreshLaunch) {
throw launchFailure;
@@ -1627,7 +1627,7 @@ public class AutoScalingService implements PassedRoleLookup {
}
}
private static void validateCapacityReservationSpecification(CapacityReservationSpecification specification) {
static void validateCapacityReservationSpecification(CapacityReservationSpecification specification) {
if (specification == null) {
return;
}
@@ -1638,6 +1638,10 @@ public class AutoScalingService implements PassedRoleLookup {
if ("none".equals(preference) && specification.hasTarget()) {
throw new AwsException("ValidationError", CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE, 400);
}
if (specification.getCapacityReservationIds().size()
+ specification.getCapacityReservationResourceGroupArns().size() > 1) {
throw new AwsException("ValidationError", "Specify only one Capacity Reservation target.", 400);
}
for (String id : specification.getCapacityReservationIds()) {
if (id == null || id.isEmpty() || id.length() > 255
|| !CAPACITY_RESERVATION_ID_PATTERN.matcher(id).matches()) {