mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): keep each instance in the reservation of its RunInstances call (#8257)
RunInstances now stores its reservation id on each instance it launches, and DescribeInstances groups instances by it, so the ReservationId RunInstances answers stays the same on every call and instances launched together are listed together. An instance Floci did not launch keeps a stable reservation of its own, and a provider's reservation id is kept. Closes #8255
This commit is contained in:
1 parent
5cf3c780d4
commit
486f0e4604
4 files changed
+96
-5
No files matched your search
@@ -4184,6 +4184,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
Instance inst = new Instance();
|
||||
inst.setInstanceId(instanceId);
|
||||
inst.setOwnerId(callerAccountId());
|
||||
inst.setReservationId(reservationId);
|
||||
inst.setImageId(imageId);
|
||||
inst.setState(InstanceState.pending());
|
||||
inst.setInstanceType(effectiveInstanceType);
|
||||
@@ -4933,15 +4934,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
.filter(i -> matchesFilters(i, filters, region))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Group into reservations (one instance per reservation for simplicity)
|
||||
// An instance without a stored reservation (one Floci did not launch) keeps a stable id of its own
|
||||
Map<String, Reservation> reservationMap = new LinkedHashMap<>();
|
||||
String ownerAccountId = callerAccountId();
|
||||
for (Instance inst : matched) {
|
||||
Reservation res = new Reservation();
|
||||
res.setReservationId("r-" + randomHex(17));
|
||||
res.setOwnerId(ownerAccountId);
|
||||
String reservationId = inst.getReservationId() != null
|
||||
? inst.getReservationId() : "r-" + inst.getInstanceId().substring("i-".length());
|
||||
Reservation res = reservationMap.computeIfAbsent(reservationId, id -> {
|
||||
Reservation created = new Reservation();
|
||||
created.setReservationId(id);
|
||||
created.setOwnerId(ownerAccountId);
|
||||
return created;
|
||||
});
|
||||
res.getInstances().add(inst);
|
||||
reservationMap.put(inst.getInstanceId(), res);
|
||||
}
|
||||
return new ArrayList<>(reservationMap.values());
|
||||
}
|
||||
@@ -6063,6 +6068,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
copy.setLaunchTime(source.getLaunchTime());
|
||||
copy.setAmiLaunchIndex(source.getAmiLaunchIndex());
|
||||
copy.setClientToken(source.getClientToken());
|
||||
copy.setReservationId(source.getReservationId());
|
||||
copy.setMonitoring(source.getMonitoring());
|
||||
copy.setSourceDestCheck(source.isSourceDestCheck());
|
||||
copy.setEbsOptimized(source.isEbsOptimized());
|
||||
|
||||
@@ -57,6 +57,7 @@ public class Instance {
|
||||
private String stateReasonMessage;
|
||||
private String region;
|
||||
private String ownerId;
|
||||
private String reservationId;
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
|
||||
private String rootVolumeId;
|
||||
@@ -231,6 +232,9 @@ public class Instance {
|
||||
public String getRegion() { return region; }
|
||||
public void setRegion(String region) { this.region = region; }
|
||||
|
||||
public String getReservationId() { return reservationId; }
|
||||
public void setReservationId(String reservationId) { this.reservationId = reservationId; }
|
||||
|
||||
public String getOwnerId() { return ownerId; }
|
||||
public void setOwnerId(String ownerId) { this.ownerId = ownerId; }
|
||||
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Instance;
|
||||
import software.amazon.awssdk.services.ec2.model.Reservation;
|
||||
import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Every instance keeps the reservation of the RunInstances call that launched it. */
|
||||
@QuarkusTest
|
||||
class Ec2DescribeInstancesReservationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void instancesLaunchedTogetherShareTheReservationRunInstancesAnswered() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(), "test", "test", Region.US_EAST_1)) {
|
||||
RunInstancesResponse run = client.runInstances(request -> request.imageId("ami-0abcdef1234567890")
|
||||
.instanceType("t3.micro").minCount(3).maxCount(3));
|
||||
List<String> ids = run.instances().stream().map(Instance::instanceId).toList();
|
||||
try {
|
||||
Reservation first = reservationOf(client, ids);
|
||||
Reservation second = reservationOf(client, ids);
|
||||
|
||||
assertEquals(run.reservationId(), first.reservationId());
|
||||
assertEquals(first.reservationId(), second.reservationId());
|
||||
assertEquals(3, first.instances().size());
|
||||
assertTrue(first.instances().stream().map(Instance::instanceId).toList().containsAll(ids));
|
||||
} finally {
|
||||
client.terminateInstances(request -> request.instanceIds(ids));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Reservation reservationOf(Ec2Client client, List<String> ids) {
|
||||
List<Reservation> reservations = client.describeInstances(request -> request.instanceIds(ids)).reservations();
|
||||
assertEquals(1, reservations.size());
|
||||
return reservations.getFirst();
|
||||
}
|
||||
}
|
||||
@@ -10105,6 +10105,41 @@ class Ec2ServiceTest {
|
||||
assertEquals("OperationNotPermitted", terminateEx.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void providerInstancesSharingAReservationAreDescribedTogether() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
List<Instance> nodes = new ArrayList<>();
|
||||
for (String id : List.of("i-node1111111111111", "i-node2222222222222")) {
|
||||
Instance node = new Instance();
|
||||
node.setInstanceId(id);
|
||||
node.setRegion("us-east-1");
|
||||
node.setPlacement(new Placement("us-east-1a"));
|
||||
node.setState(InstanceState.running());
|
||||
node.setInstanceType("m5.large");
|
||||
node.setReservationId("r-sharedprovider01");
|
||||
nodes.add(node);
|
||||
}
|
||||
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
|
||||
@Override
|
||||
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
|
||||
return nodes.stream().filter(node -> node.getInstanceId().equals(instanceId)).findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Instance> listInstances(String accountId, String region) {
|
||||
return nodes;
|
||||
}
|
||||
});
|
||||
|
||||
List<Reservation> reservations = service.describeInstances("us-east-1", List.of(), Map.of());
|
||||
|
||||
assertEquals(1, reservations.size());
|
||||
assertEquals("r-sharedprovider01", reservations.getFirst().getReservationId());
|
||||
assertEquals(2, reservations.getFirst().getInstances().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterNodeStaysRefusedAfterAnotherActionStoredACopyOfIt() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
Reference in new issue
Block a user