fix(autoscaling): reconcile groups in every account (#3824)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-20 08:55:39 -04:00
1 parent 51467dbc23
commit dfb294a79a
3 files changed
+117 -6

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.autoscaling;
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.LaunchConfiguration;
@@ -80,12 +81,30 @@ public class AutoScalingReconciler {
}
void reconcileAll() {
for (AutoScalingGroup asg : asgService.describeAutoScalingGroups(null, null)) {
try {
reconcile(asg);
} catch (Exception e) {
LOG.warnv("Reconcile failed for ASG {0}: {1}", asg.getAutoScalingGroupName(), e.getMessage());
Set<String> accountIds;
try {
accountIds = asgService.autoScalingGroupAccountIds();
} catch (Exception e) {
LOG.warnv("Could not enumerate Auto Scaling group accounts: {0}", e.getMessage());
return;
}
for (String accountId : accountIds) {
RequestScopes.runAs(accountId, () -> reconcileAccount(accountId));
}
}
private void reconcileAccount(String accountId) {
try {
for (AutoScalingGroup asg : asgService.describeAutoScalingGroups(null, null)) {
try {
reconcile(asg);
} catch (Exception e) {
LOG.warnv("Reconcile failed for ASG {0} in account {1}: {2}",
asg.getAutoScalingGroupName(), accountId, e.getMessage());
}
}
} catch (Exception e) {
LOG.warnv("Could not reconcile Auto Scaling groups in account {0}: {1}", accountId, e.getMessage());
}
}
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.autoscaling.model.*;
@@ -64,6 +65,7 @@ public class AutoScalingService {
private Map<String, LaunchConfiguration> launchConfigs = new ConcurrentHashMap<>();
private Map<String, AutoScalingGroup> groups = new ConcurrentHashMap<>();
private final Map<String, Object> groupLocks = new ConcurrentHashMap<>();
private AccountAwareStorageBackend<AutoScalingGroup> groupsStore;
private Map<String, LifecycleHook> hooks = new ConcurrentHashMap<>();
private Map<String, ScalingPolicy> policies = new ConcurrentHashMap<>();
private Map<String, ScalingActivity> activities = new ConcurrentHashMap<>();
@@ -78,7 +80,9 @@ public class AutoScalingService {
return;
}
this.launchConfigs = storageBacked("autoscaling-launch-configurations.json", new TypeReference<Map<String, LaunchConfiguration>>() {});
this.groups = storageBacked("autoscaling-groups.json", new TypeReference<Map<String, AutoScalingGroup>>() {});
this.groupsStore = storageFactory.create("autoscaling", "autoscaling-groups.json",
new TypeReference<Map<String, AutoScalingGroup>>() {});
this.groups = new StorageBackedMap<>(groupsStore);
this.hooks = storageBacked("autoscaling-lifecycle-hooks.json", new TypeReference<Map<String, LifecycleHook>>() {});
this.policies = storageBacked("autoscaling-policies.json", new TypeReference<Map<String, ScalingPolicy>>() {});
this.activities = storageBacked("autoscaling-activities.json", new TypeReference<Map<String, ScalingActivity>>() {});
@@ -392,6 +396,15 @@ public class AutoScalingService {
return groupLocks.computeIfAbsent(key, ignored -> new Object());
}
Set<String> autoScalingGroupAccountIds() {
if (groupsStore == null) {
return Set.of();
}
return groupsStore.scanAllAccountEntries(key -> true).stream()
.map(AccountAwareStorageBackend.AccountEntry::accountId)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
public void setDesiredCapacity(String region, String name, int desiredCapacity) {
AutoScalingGroup asg = requireGroup(region, name);
if (desiredCapacity < asg.getMinSize() || desiredCapacity > asg.getMaxSize()) {
@@ -0,0 +1,79 @@
package io.github.hectorvent.floci.services.autoscaling;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
@QuarkusTest
class AutoScalingMultiAccountIntegrationTest {
private static final String ACCOUNT_ID = "222222222222";
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT_ID + "/20260917/us-east-1/autoscaling/aws4_request";
@Inject
AutoScalingReconciler reconciler;
@Test
void reconcilesAGroupOwnedByANonDefaultAccount() {
String suffix = UUID.randomUUID().toString();
String launchConfigurationName = "multi-account-lc-" + suffix;
String groupName = "multi-account-asg-" + suffix;
given()
.formParam("Action", "CreateLaunchConfiguration")
.formParam("LaunchConfigurationName", launchConfigurationName)
.formParam("ImageId", "ami-amazonlinux2023")
.formParam("InstanceType", "t3.micro")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200);
given()
.formParam("Action", "CreateAutoScalingGroup")
.formParam("AutoScalingGroupName", groupName)
.formParam("LaunchConfigurationName", launchConfigurationName)
.formParam("MinSize", "1")
.formParam("MaxSize", "1")
.formParam("DesiredCapacity", "1")
.formParam("AvailabilityZones.member.1", "us-east-1a")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200);
reconciler.reconcileAll();
reconciler.reconcileAll();
given()
.formParam("Action", "DescribeAutoScalingGroups")
.formParam("AutoScalingGroupNames.member.1", groupName)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeAutoScalingGroupsResponse.DescribeAutoScalingGroupsResult"
+ ".AutoScalingGroups.member.Instances.member.size()", equalTo(1));
given()
.formParam("Action", "DescribeScalingActivities")
.formParam("AutoScalingGroupName", groupName)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeScalingActivitiesResponse.DescribeScalingActivitiesResult"
+ ".Activities.member.size()", equalTo(1));
}
}