mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): send container logs to the awslogs group and stream from the task definition (#4808)
* fix(ecs): send container logs to the awslogs group and stream from the task definition * fix(ecs): keep console output for containers without awslogs
This commit is contained in:
1 parent
8f904afa5a
commit
bc2592e300
8 files changed
+240
-7
No files matched your search
+21
@@ -106,6 +106,27 @@ class ContainerLogStreamerTest {
|
||||
eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void consoleOnlyStreamReachesTheConsoleButNotCloudWatch() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
LogContainerCmd command = mock(LogContainerCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.logContainerCmd("container-1")).thenReturn(command);
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(dockerClient, cloudWatchLogsService);
|
||||
|
||||
streamer.attachConsoleOnly("container-1", "ecs:family:main");
|
||||
ArgumentCaptor<ContainerLogStreamer.LogReassemblyCallback> callback =
|
||||
ArgumentCaptor.forClass(ContainerLogStreamer.LogReassemblyCallback.class);
|
||||
verify(command).exec(callback.capture());
|
||||
|
||||
List<LogRecord> records = LogCapture.capture(ContainerLogStreamer.class, () ->
|
||||
callback.getValue().onNext(new Frame(StreamType.STDOUT, utf8("container output\n"))));
|
||||
|
||||
assertEquals(1, records.size());
|
||||
verify(cloudWatchLogsService, never()).createLogGroupForAccount(any(), any(), any(), any(), any());
|
||||
verify(cloudWatchLogsService, never()).putLogEventsForAccount(any(), any(), any(), anyList(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void execStreamAlwaysReachesCloudWatchAndConsoleIsOptional(boolean logToConsole) {
|
||||
|
||||
+3
@@ -114,6 +114,8 @@ class EcsContainerManagerFirelensTest {
|
||||
router.setName("log_router");
|
||||
router.setImage("amazon/aws-for-fluent-bit:stable");
|
||||
router.setFirelensConfiguration(new FirelensConfiguration("fluentbit", Map.of()));
|
||||
router.setLogConfiguration(new LogConfiguration("awslogs",
|
||||
Map.of("awslogs-group", "/ecs/firelens-family", "awslogs-stream-prefix", "firelens"), null));
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
@@ -150,6 +152,7 @@ class EcsContainerManagerFirelensTest {
|
||||
|
||||
verify(logStreamer).attach(eq("router-id"), anyString(), anyString(), anyString(), anyString());
|
||||
verify(logStreamer, never()).attach(eq("app-id"), anyString(), anyString(), anyString(), anyString());
|
||||
verify(logStreamer, never()).attachConsoleOnly(eq("app-id"), anyString());
|
||||
assertTrue(handle.getFirelensVolumeName().contains("firelens"));
|
||||
|
||||
verify(copyCmd).withRemotePath("/fluent-bit/etc");
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LogConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService.LaunchImage;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
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;
|
||||
|
||||
/**
|
||||
* A container's output reaches CloudWatch Logs only through the {@code awslogs} driver, in the
|
||||
* group and stream its log configuration names. Other non-FireLens containers stay on the console.
|
||||
* Docker is mocked, so no daemon is needed.
|
||||
*/
|
||||
class EcsContainerManagerLogConfigurationTest {
|
||||
|
||||
private static final String TASK_ARN = "arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123";
|
||||
|
||||
private ContainerLogStreamer logStreamer;
|
||||
private EcsContainerManager manager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ContainerBuilder.Builder builder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(containerBuilder.resolveImage(anyString())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.resolveImageForLaunch(any(), any()))
|
||||
.thenAnswer(invocation -> new LaunchImage(invocation.getArgument(0), null));
|
||||
when(lifecycleManager.createAndStart(any()))
|
||||
.thenReturn(new ContainerInfo("docker-id", Map.of()));
|
||||
|
||||
logStreamer = mock(ContainerLogStreamer.class);
|
||||
LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);
|
||||
when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());
|
||||
EcrRegistryManager ecrRegistryManager = mock(EcrRegistryManager.class);
|
||||
when(ecrRegistryManager.rewriteImageUri(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
mock(ContainerDetector.class), mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),
|
||||
mock(RegionResolver.class), awsEnv, mock(SsmService.class), mock(SecretsManagerService.class),
|
||||
mock(S3Service.class), ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void awslogsSendsToTheConfiguredGroupAndPrefixedStream() {
|
||||
startTask(new LogConfiguration("awslogs", Map.of(
|
||||
"awslogs-group", "my-app",
|
||||
"awslogs-region", "us-east-1",
|
||||
"awslogs-stream-prefix", "web"), null));
|
||||
|
||||
verify(logStreamer).attach(eq("docker-id"), eq("my-app"), eq("web/main/abc123"), eq("us-east-1"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awslogsWithoutStreamPrefixNamesTheStreamAfterTheDockerContainer() {
|
||||
startTask(new LogConfiguration("awslogs", Map.of("awslogs-group", "my-app"), null));
|
||||
|
||||
verify(logStreamer).attach(eq("docker-id"), eq("my-app"), eq("docker-id"), eq("us-east-1"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awslogsRegionOptionChoosesTheLogsRegion() {
|
||||
startTask(new LogConfiguration("awslogs", Map.of(
|
||||
"awslogs-group", "my-app",
|
||||
"awslogs-region", "eu-west-1",
|
||||
"awslogs-stream-prefix", "web"), null));
|
||||
|
||||
verify(logStreamer).attach(eq("docker-id"), eq("my-app"), eq("web/main/abc123"), eq("eu-west-1"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerWithoutLogConfigurationOnlyStreamsToTheConsole() {
|
||||
startTask(null);
|
||||
|
||||
verify(logStreamer).attachConsoleOnly("docker-id", "ecs:logs-family:main");
|
||||
verify(logStreamer, never()).attach(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherLogDriverOnlyStreamsToTheConsole() {
|
||||
startTask(new LogConfiguration("splunk", Map.of("splunk-url", "https://splunk.example.com"), null));
|
||||
|
||||
verify(logStreamer).attachConsoleOnly("docker-id", "ecs:logs-family:main");
|
||||
verify(logStreamer, never()).attach(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awslogsWithoutGroupOnlyStreamsToTheConsole() {
|
||||
startTask(new LogConfiguration("awslogs", Map.of("awslogs-stream-prefix", "web"), null));
|
||||
|
||||
verify(logStreamer).attachConsoleOnly("docker-id", "ecs:logs-family:main");
|
||||
verify(logStreamer, never()).attach(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
private void startTask(LogConfiguration logConfiguration) {
|
||||
ContainerDefinition main = new ContainerDefinition();
|
||||
main.setName("main");
|
||||
main.setImage("busybox:latest");
|
||||
main.setLogConfiguration(logConfiguration);
|
||||
|
||||
TaskDefinition taskDef = new TaskDefinition();
|
||||
taskDef.setFamily("logs-family");
|
||||
taskDef.setContainerDefinitions(List.of(main));
|
||||
|
||||
EcsTask task = new EcsTask();
|
||||
task.setTaskArn(TASK_ARN);
|
||||
|
||||
manager.startTask(task, taskDef, List.of(), "us-east-1");
|
||||
}
|
||||
}
|
||||
+6
@@ -146,7 +146,9 @@ class EcsContainerManagerVolumesFromTest {
|
||||
ContainerDefinition router = definition("router", "router:latest");
|
||||
router.setFirelensConfiguration(new FirelensConfiguration("fluentbit", Map.of()));
|
||||
router.setVolumesFrom(List.of(new VolumeFrom("source", false)));
|
||||
router.setLogConfiguration(awslogs());
|
||||
ContainerDefinition source = definition("source", "sidecar:latest");
|
||||
source.setLogConfiguration(awslogs());
|
||||
|
||||
EcsTask ecsTask = task();
|
||||
EcsTaskHandle handle = manager.startTask(
|
||||
@@ -267,6 +269,10 @@ class EcsContainerManagerVolumesFromTest {
|
||||
return definition;
|
||||
}
|
||||
|
||||
private static LogConfiguration awslogs() {
|
||||
return new LogConfiguration("awslogs", Map.of("awslogs-group", "/ecs/volumesfrom-family"), null);
|
||||
}
|
||||
|
||||
private static TaskDefinition taskDefinition(List<ContainerDefinition> definitions) {
|
||||
TaskDefinition taskDefinition = new TaskDefinition();
|
||||
taskDefinition.setFamily("volumes-from-family");
|
||||
|
||||
Reference in new issue
Block a user