fix(docker): follow adopted sidecar containers from now instead of replaying their log history (#3612)

This commit is contained in:
Chris Jovanovski authored and GitHub committed 2026-09-17 10:46:22 -04:00
1 parent 49224917e1
commit ebb79ed3c2
5 files changed
+119 -15

No files matched your search

@@ -1,11 +1,16 @@
package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.io.Closeable;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -15,6 +20,13 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class ContainerLogStreamerTest {
@@ -325,4 +337,36 @@ class ContainerLogStreamerTest {
}
assertEquals(List.of("unterminated-tail"), emitted);
}
private static LogContainerCmd attachWith(java.util.function.BiFunction<ContainerLogStreamer, String, Closeable> attach) {
DockerClient dockerClient = mock(DockerClient.class);
LogContainerCmd command = mock(LogContainerCmd.class, RETURNS_SELF);
when(dockerClient.logContainerCmd("container-1")).thenReturn(command);
ContainerLogStreamer streamer = new ContainerLogStreamer(dockerClient, mock(CloudWatchLogsService.class));
attach.apply(streamer, "container-1");
return command;
}
@Test
void attachFollowsTheCompleteHistoryByDefault() {
LogContainerCmd command = attachWith((streamer, id) ->
streamer.attach(id, "/aws/lambda/fn", "stream", "us-east-1", "lambda:fn"));
verify(command).withFollowStream(true);
verify(command, never()).withSince(anyInt());
}
@Test
void attachFromNowOnlyFollowsLinesEmittedAfterAttachment() {
long before = Instant.now().getEpochSecond();
LogContainerCmd command = attachWith((streamer, id) ->
streamer.attachFromNow(id, "/aws/ecr/registry", "stream", "us-east-1", "ecr:registry"));
long after = Instant.now().getEpochSecond();
ArgumentCaptor<Integer> since = ArgumentCaptor.forClass(Integer.class);
verify(command).withSince(since.capture());
assertTrue(since.getValue() >= before && since.getValue() <= after,
"since must be the attach time so an adopted container's history is not replayed");
verify(command).withFollowStream(true);
}
}
@@ -42,6 +42,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -248,6 +249,36 @@ class EcrRegistryManagerTest {
assertEquals("http://" + REGISTRY_NAME + ":5000", manager.httpClient().baseUrl());
}
@Test
void freshlyCreatedRegistryStreamsItsCompleteLog() {
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
verify(logStreamer).attach(eq("container-id"), eq("/aws/ecr/registry"), any(), eq("us-east-1"), eq("ecr:registry"));
verify(logStreamer, never()).attachFromNow(any(), any(), any(), any(), any());
}
@Test
void adoptedRegistryStreamsOnlyNewLinesInsteadOfReplayingItsHistory() {
Container existing = Mockito.mock(Container.class);
when(existing.getId()).thenReturn("0123456789abcdef");
when(existing.getPorts()).thenReturn(new ContainerPort[] {
new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT + 1)
});
when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
when(lifecycleManager.adopt("0123456789abcdef", List.of(5000)))
.thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef",
Map.of(5000, new ContainerLifecycleManager.EndpointInfo("172.17.0.5", 5000)),
Map.of(5000, BASE_PORT + 1)));
manager.ensureStarted();
verify(logStreamer).attachFromNow(eq("0123456789abcdef"), eq("/aws/ecr/registry"), any(), eq("us-east-1"), eq("ecr:registry"));
verify(logStreamer, never()).attach(any(), any(), any(), any(), any());
}
@Test
void adoptTracksPrivateBackingPortWhenRunningInsideDocker() {
// In container mode httpClient() uses the registry's in-network port. The adopted