fix(opensearch): keep publishing the domain's host port when floci runs in a container (#2798)

* fix(opensearch): keep publishing the domain's host port when floci runs in a container

The container-IP endpoint fix dropped the Docker host-port binding in the
in-container topology, copying the Neptune/MemoryDB pattern - but those
services front their backends with floci-internal proxies, and OpenSearch
has none, so the binding was the only path to the domain from outside the
Docker network. Publish it in both topologies again; the stored endpoint
stays the container-IP one.

* fix(opensearch): release a domain's host-port reservation when its container is removed

Repeated create/delete cycles reserved a fresh port each time and never
handed one back, eventually exhausting the configured range. The domain
now records its host port, releases it when the container is removed
(teardown or restart), and keeps the reservation when keep-running
shutdown leaves the container holding the binding.

(cherry picked from commit 91507b191abda6f30ee42022add797bff1584487)
This commit is contained in:
lex00 authored and GitHub committed 2026-09-02 13:53:57 -04:00
1 parent b85dd2b7f7
commit 96c55ec2e2
3 files changed
+169 -16

No files matched your search

@@ -15,11 +15,13 @@ import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -78,4 +80,131 @@ class OpenSearchDomainManagerTest {
"io.floci.account", "000000000000",
"io.floci.region", "us-east-1"));
}
/**
* OpenSearch has no floci-internal proxy fronting the backend, so the Docker
* host-port binding is the only path to the domain for a client outside the
* Docker network. It must be published even when floci itself runs inside a
* container, while the stored endpoint stays the container-IP one floci and
* same-network clients use.
*/
@Test
void startDomainPublishesAHostPortEvenWhenFlociRunsInAContainer() {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.OpenSearchServiceConfig opensearch = mock(EmulatorConfig.OpenSearchServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.opensearch()).thenReturn(opensearch);
when(services.dockerNetwork()).thenReturn(Optional.empty());
when(opensearch.proxyBasePort()).thenReturn(9400);
when(opensearch.proxyMaxPort()).thenReturn(9499);
when(opensearch.defaultImage()).thenReturn(Optional.of("opensearchproject/opensearch:2.11.0"));
EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
when(config.docker()).thenReturn(docker);
lenient().when(docker.logMaxSize()).thenReturn("10m");
lenient().when(docker.logMaxFile()).thenReturn("3");
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
when(config.storage()).thenReturn(storage);
when(storage.hostPersistentPath()).thenReturn("floci-data");
when(storage.persistentPath()).thenReturn("/data");
PortAllocator portAllocator = mock(PortAllocator.class);
when(portAllocator.allocate(9400, 9499)).thenReturn(9401);
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of(
9200, new ContainerLifecycleManager.EndpointInfo("172.17.0.5", 9200))));
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
ContainerBuilder.Builder builder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
when(builder.build()).thenReturn(mock(ContainerSpec.class));
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn("000000000000");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
ContainerDetector containerDetector = mock(ContainerDetector.class);
lenient().when(containerDetector.isRunningInContainer()).thenReturn(true);
OpenSearchDomainManager manager = new OpenSearchDomainManager(containerBuilder, lifecycleManager,
containerDetector, portAllocator, config, regionResolver);
Domain domain = new Domain();
domain.setDomainName("host-reach");
domain.setEngineVersion("OpenSearch_2.11");
manager.startDomain(domain);
verify(portAllocator).allocate(9400, 9499);
verify(builder).withPortBinding(9200, 9401);
assertEquals("http://172.17.0.5:9200", domain.getEndpoint());
assertEquals(Integer.valueOf(9401), domain.getHostPort());
}
/**
* The reservation must die with the container: without the release, repeated
* create/delete cycles exhaust the configured port range and later domains
* can never start.
*/
@Test
void stopDomainReleasesTheHostPort() {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.OpenSearchServiceConfig opensearch = mock(EmulatorConfig.OpenSearchServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.opensearch()).thenReturn(opensearch);
when(opensearch.keepRunningOnShutdown()).thenReturn(false);
PortAllocator portAllocator = mock(PortAllocator.class);
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
OpenSearchDomainManager manager = new OpenSearchDomainManager(mock(ContainerBuilder.class),
lifecycleManager, mock(ContainerDetector.class), portAllocator, config,
mock(RegionResolver.class));
Domain domain = new Domain();
domain.setDomainName("teardown");
domain.setContainerId("container-id");
domain.setHostPort(9401);
manager.stopDomain(domain);
verify(lifecycleManager).stopAndRemove("container-id", null);
verify(portAllocator).release(9401);
assertNull(domain.getHostPort());
}
/**
* Keep-running shutdown leaves the container holding its Docker binding, so
* the reservation has to survive with it.
*/
@Test
void stopDomainKeepsTheReservationWhenTheContainerKeepsRunning() {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.OpenSearchServiceConfig opensearch = mock(EmulatorConfig.OpenSearchServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.opensearch()).thenReturn(opensearch);
when(opensearch.keepRunningOnShutdown()).thenReturn(true);
PortAllocator portAllocator = mock(PortAllocator.class);
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
OpenSearchDomainManager manager = new OpenSearchDomainManager(mock(ContainerBuilder.class),
lifecycleManager, mock(ContainerDetector.class), portAllocator, config,
mock(RegionResolver.class));
Domain domain = new Domain();
domain.setDomainName("keep-running");
domain.setContainerId("container-id");
domain.setHostPort(9401);
manager.stopDomain(domain);
verify(lifecycleManager, never()).stopAndRemove(anyString(), any());
verify(portAllocator, never()).release(9401);
assertEquals(Integer.valueOf(9401), domain.getHostPort());
}
}