fix(ec2): restore and stop the instances of every account (#5103)

Startup restore runs from @PostConstruct and shutdown from a lifecycle
hook, both outside any request, so an account-aware keys() sees only
the default account. Every other account's EC2 state was skipped:

- after a restart its VPCs and subnets were not re-declared, so paths
  that do not declare lazily, such as an EKS node joining its VPC,
  found no network; reconcileOrphans also judged its VPC networks
  against the default account only;
- its instances' private addresses were not re-reserved and their
  IMDS registrations were not restored, so IMDS stopped answering;
- shutdown, and a state reset, which wipes every account, did not stop
  its running instances.

Restore and shutdown now walk every account's VPCs, subnets and
instances. Restore runs each instance as its own account, so the
security groups and prefix lists it resolves come from that account, in
the instance's own region, and both paths write to the owning partition.
VPCs are declared default account first, then in account and key order,
because overlapping CIDRs go to whichever VPC is declared first. The
port-forward persister, which also runs on container executor threads
with no request, writes to the account that holds the instance instead
of the current one, and writes nothing when no account holds it any more
(terminated meanwhile). A pre-multi-account key is migrated once, or
dropped when a scoped copy already exists.

Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
Robin BaeckmanandRobin Baeckman authored and GitHub committed 2026-10-15 16:52:54 -04:00
1 parent 9cd9002e73
commit 8eddae4ed6
3 files changed
+488 -33

No files matched your search

@@ -811,7 +811,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
if (portForwardManager != null) {
portForwardManager.setPersister(inst -> {
if (inst != null && inst.getRegion() != null && inst.getInstanceId() != null) {
instances.put(key(inst.getRegion(), inst.getInstanceId()), inst);
putInstanceForOwner(key(inst.getRegion(), inst.getInstanceId()), inst);
}
});
}
@@ -822,27 +822,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
restoreVpcNetworks();
int restored = 0;
for (String key : instances.keys()) {
Instance instance = instances.get(key).orElse(null);
if (!needsMetadataRegistration(instance)) {
continue;
}
try {
restoreInstanceFirewall(instance);
} catch (Exception e) {
LOG.warnv("Could not restore EC2 firewall for {0}: {1}", instance.getInstanceId(), e.getMessage());
containerManager.stopForShutdown(instance);
instance.setState(InstanceState.stopped());
instances.put(key, instance);
continue;
}
if (containerManager.restoreMetadataRegistration(instance)) {
instances.put(key, instance);
for (AccountAwareStorageBackend.AccountEntry<Instance> entry : entriesOfEveryAccount(instances)) {
if (needsMetadataRegistration(entry.value())
&& RequestScopes.callAs(entry.accountId(), entry.value().getRegion(), () -> restoreInstance(entry))) {
restored++;
// Container is running: re-reserve host ports and recreate any missing socat sidecars.
if (portForwardManager != null) {
portForwardManager.restore(instance);
}
}
}
if (restored > 0) {
@@ -857,6 +840,32 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
containerManager.reconcileOrphanedContainers(this::instanceContainerStillWanted);
}
/**
* Runs as the instance's own account, so the security groups, prefix lists and port-forward
* writes it resolves along the way come from that account rather than the default one.
*/
private boolean restoreInstance(AccountAwareStorageBackend.AccountEntry<Instance> entry) {
Instance instance = entry.value();
try {
restoreInstanceFirewall(instance);
} catch (Exception e) {
LOG.warnv("Could not restore EC2 firewall for {0}: {1}", instance.getInstanceId(), e.getMessage());
containerManager.stopForShutdown(instance);
instance.setState(InstanceState.stopped());
putForOwner(instances, entry, instance);
return false;
}
if (!containerManager.restoreMetadataRegistration(instance)) {
return false;
}
putForOwner(instances, entry, instance);
// Container is running: re-reserve host ports and recreate any missing socat sidecars.
if (portForwardManager != null) {
portForwardManager.restore(instance);
}
return true;
}
private void restoreAttachedVolumesOnStartup() {
Map<String, Volume> allVolumes;
if (volumes instanceof AccountAwareStorageBackend<?> rawAccountAware) {
@@ -952,14 +961,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
if (vpcNetworkManager == null || !vpcNetworkManager.enabled()) {
return;
}
vpcNetworkManager.reconcileOrphans((region, vpcId) -> vpcs.get(key(region, vpcId)).isPresent());
for (String storageKey : vpcs.keys()) {
vpcs.get(storageKey).ifPresent(vpc ->
vpcNetworkManager.declareVpc(vpc.getRegion(), vpc.getVpcId(), vpc.getCidrBlock()));
vpcNetworkManager.reconcileOrphans((region, vpcId) -> findAnyVpcEntry(region, vpcId).isPresent());
// declareVpc is first-wins on overlapping CIDRs, so declare in a stable order, default account first.
List<AccountAwareStorageBackend.AccountEntry<Vpc>> restoredVpcs = new ArrayList<>(entriesOfEveryAccount(vpcs));
restoredVpcs.sort(Comparator
.comparing((AccountAwareStorageBackend.AccountEntry<Vpc> entry) -> !defaultAccountId.equals(entry.accountId()))
.thenComparing(entry -> String.valueOf(entry.accountId()))
.thenComparing(AccountAwareStorageBackend.AccountEntry::key));
for (AccountAwareStorageBackend.AccountEntry<Vpc> entry : restoredVpcs) {
Vpc vpc = entry.value();
vpcNetworkManager.declareVpc(vpc.getRegion(), vpc.getVpcId(), vpc.getCidrBlock());
}
for (String storageKey : subnets.keys()) {
subnets.get(storageKey).ifPresent(subnet -> vpcNetworkManager.declareSubnet(
subnet.getRegion(), subnet.getVpcId(), subnet.getSubnetId(), subnet.getCidrBlock()));
for (AccountAwareStorageBackend.AccountEntry<Subnet> entry : entriesOfEveryAccount(subnets)) {
Subnet subnet = entry.value();
vpcNetworkManager.declareSubnet(
subnet.getRegion(), subnet.getVpcId(), subnet.getSubnetId(), subnet.getCidrBlock());
}
reserveRestoredPrivateIps();
}
@@ -979,8 +995,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
private void reserveRestoredPrivateIps() {
int reserved = 0;
int skipped = 0;
for (String storageKey : instances.keys()) {
Instance instance = instances.get(storageKey).orElse(null);
for (AccountAwareStorageBackend.AccountEntry<Instance> entry : entriesOfEveryAccount(instances)) {
Instance instance = entry.value();
if (instance == null || instance.getSubnetId() == null
|| instance.getPrivateIpAddress() == null
|| instance.getState() == null
@@ -1001,6 +1017,59 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
/**
* Every stored entry, whichever account owns it. Startup restore and shutdown run outside
* any request, where an account-aware {@code keys()} sees only the default account.
* A pre-multi-account key is migrated to the default account it is reported under, so it is
* listed once and later writes and deletes reach the only copy. The migration is a write on
* read, on purpose: it is idempotent, so the restore and shutdown paths can each call this.
*/
private static <T> List<AccountAwareStorageBackend.AccountEntry<T>> entriesOfEveryAccount(
StorageBackend<String, T> store) {
if (store instanceof AccountAwareStorageBackend<?> rawAccountAware) {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<T> accountAware = (AccountAwareStorageBackend<T>) rawAccountAware;
Map<String, AccountAwareStorageBackend.AccountEntry<T>> unique = new LinkedHashMap<>();
Set<String> legacy = new HashSet<>();
for (AccountAwareStorageBackend.AccountEntry<T> scanned : accountAware.scanAllAccountEntries(key -> true)) {
String owned = scanned.accountId() + "/" + scanned.key();
if (unique.putIfAbsent(owned, scanned) != null
|| accountAware.getForAccount(scanned.accountId(), scanned.key()).isEmpty()) {
legacy.add(owned);
}
}
List<AccountAwareStorageBackend.AccountEntry<T>> entries = new ArrayList<>();
for (Map.Entry<String, AccountAwareStorageBackend.AccountEntry<T>> listed : unique.entrySet()) {
AccountAwareStorageBackend.AccountEntry<T> scanned = listed.getValue();
if (!legacy.contains(listed.getKey())) {
entries.add(scanned);
continue;
}
// Moves the legacy copy under its account, or drops it when a scoped twin already exists.
accountAware.getForAccountMigratingLegacyKeys(scanned.accountId(), scanned.key(), List.of(), value -> true)
.ifPresent(value -> entries.add(
new AccountAwareStorageBackend.AccountEntry<>(scanned.accountId(), scanned.key(), value)));
}
return entries;
}
List<AccountAwareStorageBackend.AccountEntry<T>> entries = new ArrayList<>();
for (String key : store.keys()) {
store.get(key).ifPresent(value -> entries.add(new AccountAwareStorageBackend.AccountEntry<>(null, key, value)));
}
return entries;
}
private static <T> void putForOwner(StorageBackend<String, T> store,
AccountAwareStorageBackend.AccountEntry<T> entry, T value) {
if (store instanceof AccountAwareStorageBackend<?> rawAccountAware) {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<T> accountAware = (AccountAwareStorageBackend<T>) rawAccountAware;
accountAware.putForAccount(entry.accountId(), entry.key(), value);
return;
}
store.put(entry.key(), value);
}
private void declareVpcNetwork(String region, String vpcId, String cidrBlock) {
if (vpcNetworkManager != null) {
vpcNetworkManager.declareVpc(region, vpcId, cidrBlock);
@@ -4014,8 +4083,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
if (config.services().ec2().mock()) {
return;
}
for (String storeKey : Set.copyOf(instances.keys())) {
Instance inst = instances.get(storeKey).orElse(null);
for (AccountAwareStorageBackend.AccountEntry<Instance> entry : entriesOfEveryAccount(instances)) {
Instance inst = entry.value();
if (inst == null || inst.getDockerContainerId() == null
|| inst.getState() == null || !"running".equals(inst.getState().getName())) {
continue;
@@ -4023,7 +4092,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
try {
containerManager.stopForShutdown(inst);
inst.setState(InstanceState.stopped());
instances.put(storeKey, inst);
putForOwner(instances, entry, inst);
} catch (Exception e) {
LOG.warnv("Failed to stop EC2 instance container {0} on shutdown: {1}",
inst.getDockerContainerId(), e.getMessage());
@@ -9150,6 +9219,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return instances.get(storageKey);
}
/**
* The port-forward persister also runs on container executor threads, outside any request,
* where a plain put would copy another account's instance into the default account.
*/
private void putInstanceForOwner(String storageKey, Instance instance) {
if (instances instanceof AccountAwareStorageBackend<?> rawAccountAware) {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<Instance> accountAware = (AccountAwareStorageBackend<Instance>) rawAccountAware;
Optional<AccountAwareStorageBackend.OwnedEntry<Instance>> owner = accountAware.findAnyAccountEntry(storageKey);
// No owner means the instance was terminated and deleted meanwhile; writing it would
// leave a copy in the default account.
owner.ifPresent(found -> accountAware.putForAccount(found.account(), storageKey, instance));
return;
}
instances.put(storageKey, instance);
}
private Optional<AccountAwareStorageBackend.OwnedEntry<Vpc>> findAnyVpcEntry(String region, String vpcId) {
if (vpcs instanceof AccountAwareStorageBackend<?> rawAccountAware) {
@SuppressWarnings("unchecked")