mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): keep a cluster's API server port when its container is recreated (#8343)
* fix(eks): keep a cluster's API server port when its container is recreated * fix(eks): leave a network-mode endpoint's port out of the restored port * fix(eks): free an unadoptable survivor's port first, and tell a taken port from an error
This commit is contained in:
1 parent
c0747f9c07
commit
acd884a739
5 files changed
+220
-9
No files matched your search
@@ -4,8 +4,10 @@ import io.floci.aws.testing.StartGate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -75,6 +77,23 @@ class PortAllocatorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void allocateIfFreeHandsOutThatPortOnlyWhileNothingHoldsIt() throws Exception {
|
||||
PortAllocator allocator = new PortAllocator();
|
||||
int port = allocator.allocate(19900, 19999);
|
||||
allocator.release(port);
|
||||
|
||||
assertEquals(OptionalInt.of(port), allocator.allocateIfFree(port));
|
||||
// Reserved now, as allocate leaves a port it hands out.
|
||||
assertEquals(OptionalInt.empty(), allocator.allocateIfFree(port));
|
||||
allocator.release(port);
|
||||
try (ServerSocket bound = new ServerSocket(port)) {
|
||||
assertEquals(OptionalInt.empty(), allocator.allocateIfFree(port));
|
||||
}
|
||||
assertEquals(OptionalInt.of(port), allocator.allocateIfFree(port));
|
||||
allocator.release(port);
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsWhenRangeExhausted() {
|
||||
PortAllocator allocator = new PortAllocator();
|
||||
|
||||
@@ -83,6 +83,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.text.MessageFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -92,6 +93,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -115,6 +117,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
@@ -597,6 +600,7 @@ class EksClusterManagerTest {
|
||||
private PortAllocator portAllocator;
|
||||
private DockerClient dockerClient;
|
||||
private ContainerBuilder containerBuilder;
|
||||
private ContainerBuilder.Builder builder;
|
||||
private ContainerDetector containerDetector;
|
||||
private EksClusterManager manager;
|
||||
|
||||
@@ -620,7 +624,7 @@ class EksClusterManagerTest {
|
||||
|
||||
lifecycleManager = Mockito.mock(ContainerLifecycleManager.class);
|
||||
containerBuilder = Mockito.mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(new ContainerSpec("test-image"));
|
||||
portAllocator = Mockito.mock(PortAllocator.class);
|
||||
@@ -1269,6 +1273,126 @@ class EksClusterManagerTest {
|
||||
verify(lifecycleManager).removeIfExists("floci-eks-demo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatingAMissingContainerKeepsTheApiServerPortTheEndpointNames() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
|
||||
stubFreshStart("cid-new", 6440);
|
||||
when(portAllocator.allocateIfFree(6453)).thenReturn(OptionalInt.of(6453));
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://localhost:6453");
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals(6453, cluster.getHostPort());
|
||||
assertEquals("https://localhost:6453", cluster.getEndpoint());
|
||||
verify(builder).withPortBinding(6443, 6453);
|
||||
verify(portAllocator, never()).allocate(6440, 6499);
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatingASurvivorThatPublishesNoPortKeepsTheApiServerPortTheEndpointNames() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
.thenReturn(Optional.of(survivingContainer("cid-1")));
|
||||
when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.empty()))
|
||||
.thenReturn(new ContainerInfo("cid-1", Map.of()));
|
||||
stubFreshStart("cid-new", 6440);
|
||||
when(portAllocator.allocateIfFree(6457)).thenReturn(OptionalInt.of(6457));
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://localhost:6457");
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("https://localhost:6457", cluster.getEndpoint());
|
||||
verify(builder).withPortBinding(6443, 6457);
|
||||
verify(portAllocator, never()).allocate(6440, 6499);
|
||||
}
|
||||
|
||||
/**
|
||||
* A survivor that can't be adopted may still publish the port the endpoint names. It is removed before the
|
||||
* port is asked for, so the new container takes that port and the endpoint stays.
|
||||
*/
|
||||
@Test
|
||||
void recreatingASurvivorThatCannotBeAdoptedRemovesItBeforeTakingTheEndpointsPort() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
.thenReturn(Optional.of(survivingContainer("cid-1")));
|
||||
when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.empty()))
|
||||
.thenThrow(new RuntimeException("the survivor did not start"));
|
||||
stubFreshStart("cid-new", 6440);
|
||||
when(portAllocator.allocateIfFree(6457)).thenReturn(OptionalInt.of(6457));
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://localhost:6457");
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
InOrder order = inOrder(lifecycleManager, portAllocator);
|
||||
order.verify(lifecycleManager).removeIfExistsStrict("floci-eks-demo");
|
||||
order.verify(portAllocator).allocateIfFree(6457);
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("https://localhost:6457", cluster.getEndpoint());
|
||||
verify(portAllocator, never()).allocate(6440, 6499);
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatingAMissingContainerMovesToAFreePortWhenTheEndpointPortIsTaken() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
|
||||
stubFreshStart("cid-new", 6440);
|
||||
when(portAllocator.allocateIfFree(6457)).thenReturn(OptionalInt.empty());
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://localhost:6457");
|
||||
|
||||
List<LogRecord> logs = LogCapture.capture(EksClusterManager.class, () -> manager.restoreCluster(cluster));
|
||||
|
||||
assertEquals(6440, cluster.getHostPort());
|
||||
assertEquals("https://localhost:6440", cluster.getEndpoint());
|
||||
verify(builder).withPortBinding(6443, 6440);
|
||||
verify(portAllocator).allocate(6440, 6499);
|
||||
String warning = "API server port 6457 of EKS cluster demo is taken; moving the cluster to another port "
|
||||
+ "changes its endpoint";
|
||||
// Compared by severity, not identity: JBoss LogManager has its own Level constants.
|
||||
List<String> warnings = logs.stream()
|
||||
.filter(record -> record.getLevel().intValue() >= Level.WARNING.intValue())
|
||||
.map(record -> MessageFormat.format(record.getMessage(), record.getParameters()))
|
||||
.toList();
|
||||
assertTrue(warnings.contains(warning), String.valueOf(warnings));
|
||||
}
|
||||
|
||||
/**
|
||||
* A network-mode endpoint names the container's own API server port, 6443, which says nothing
|
||||
* about the host port the container had, so it is not asked for even inside the range.
|
||||
*/
|
||||
@Test
|
||||
void recreatingAMissingContainerInNetworkModeDoesNotAskForTheEndpointsContainerPort() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().eks().endpointMode()).thenReturn("network");
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
|
||||
stubFreshStart("cid-new", 6440);
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://floci-eks-demo:6443");
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals(6440, cluster.getHostPort());
|
||||
assertEquals("https://" + cluster.getDockerName() + ":6443", cluster.getEndpoint());
|
||||
verify(builder).withPortBinding(6443, 6440);
|
||||
verify(portAllocator, never()).allocateIfFree(6443);
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatingAMissingContainerIgnoresAnEndpointPortOutsideTheConfiguredRange() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
|
||||
stubFreshStart("cid-new", 6440);
|
||||
Cluster cluster = cluster();
|
||||
cluster.setEndpoint("https://localhost:6512");
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals(6440, cluster.getHostPort());
|
||||
assertEquals("https://localhost:6440", cluster.getEndpoint());
|
||||
verify(portAllocator, never()).allocateIfFree(6512);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopClusterRetainsTheDataVolumeInPersistentStorageMode() {
|
||||
when(storage.mode()).thenReturn("hybrid");
|
||||
|
||||
Reference in new issue
Block a user