mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ecs): add FireLens task logging support (#3768)
* feat(ecs): add FireLens task logging support * fix(docker): omit extra hosts for container network mode * fix(ecs): bind FireLens router port to loopback
This commit is contained in:
1 parent
172bfb32b0
commit
6f87973649
11 files changed
+527
-17
No files matched your search
+16
@@ -165,6 +165,22 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
verify(createCmd, never()).withPlatform(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOmitsExtraHostsForContainerNetworkMode() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
ContainerSpec spec = new ContainerSpec(
|
||||
"busybox:stable", null, List.of(), null, null, null, Map.of(), List.of(),
|
||||
"container:router-id", List.of(), List.of(), List.of("host.docker.internal:host-gateway"),
|
||||
Map.of(), null, false, null, List.of(), null, null, List.of());
|
||||
|
||||
manager().create(spec);
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
assertTrue(hostConfig.getValue().getExtraHosts() == null
|
||||
|| hostConfig.getValue().getExtraHosts().length == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createIncludesNamespaceLabelWhenConfigured() {
|
||||
when(dockerConfig.resourceNamespace()).thenReturn(Optional.of("run-one"));
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EcsJsonHandlerFirelensTest {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
private EcsJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
objectMapper = new ObjectMapper();
|
||||
EcsService service = mock(EcsService.class);
|
||||
when(service.registerTaskDefinition(anyString(), any(), any(), any(), any(), any(), any(), any(), any(), anyString()))
|
||||
.thenAnswer(invocation -> {
|
||||
TaskDefinition taskDefinition = new TaskDefinition();
|
||||
taskDefinition.setFamily(invocation.getArgument(0));
|
||||
taskDefinition.setRevision(1);
|
||||
taskDefinition.setStatus("ACTIVE");
|
||||
taskDefinition.setContainerDefinitions(invocation.getArgument(1, List.class));
|
||||
return taskDefinition;
|
||||
});
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRoundTripsFirelensConfiguration() throws Exception {
|
||||
JsonNode request = objectMapper.readTree("""
|
||||
{
|
||||
"family": "firelens-family",
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "log_router",
|
||||
"image": "public.ecr.aws/aws-observability/aws-for-fluent-bit:3",
|
||||
"firelensConfiguration": {
|
||||
"type": "fluentbit",
|
||||
"options": {"enable-ecs-log-metadata": "false"}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "app",
|
||||
"image": "alpine:latest",
|
||||
"logConfiguration": {
|
||||
"logDriver": "awsfirelens",
|
||||
"options": {"Name": "cloudwatch_logs", "log_group_name": "/ecs/app"}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
|
||||
JsonNode taskDefinition = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
|
||||
JsonNode firelens = taskDefinition.path("containerDefinitions").get(0).path("firelensConfiguration");
|
||||
assertEquals("fluentbit", firelens.path("type").asText());
|
||||
assertEquals("false", firelens.path("options").path("enable-ecs-log-metadata").asText());
|
||||
JsonNode logConfiguration = taskDefinition.path("containerDefinitions").get(1).path("logConfiguration");
|
||||
assertEquals("awsfirelens", logConfiguration.path("logDriver").asText());
|
||||
assertEquals("cloudwatch_logs", logConfiguration.path("options").path("Name").asText());
|
||||
assertTrue(logConfiguration.path("options").path("log_group_name").isTextual());
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.core.command.ExecStartResultCallback;
|
||||
import com.github.dockerjava.core.command.WaitContainerResultCallback;
|
||||
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.FirelensConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LogConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class EcsContainerManagerFirelensDockerIntegrationTest {
|
||||
|
||||
private static final String IMAGE = "public.ecr.aws/docker/library/busybox:latest";
|
||||
|
||||
@Inject
|
||||
EcsContainerManager containerManager;
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
private EcsTaskHandle taskHandle;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
Assumptions.assumeTrue(isDockerAvailable(),
|
||||
"Docker daemon must be available for FireLens integration tests");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUpTask() {
|
||||
if (taskHandle != null) {
|
||||
containerManager.stopTask(taskHandle);
|
||||
taskHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationStdoutReachesFirelensRouterThroughPublishedHostPort() throws Exception {
|
||||
String marker = "firelens-marker-" + UUID.randomUUID();
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
app.setImage(IMAGE);
|
||||
app.setCommand(List.of("sh", "-c", "echo " + marker + "; sleep 2"));
|
||||
app.setLogConfiguration(new LogConfiguration("awsfirelens", Map.of(), null));
|
||||
|
||||
ContainerDefinition router = new ContainerDefinition();
|
||||
router.setName("router");
|
||||
router.setImage(IMAGE);
|
||||
router.setCommand(List.of("sh", "-c", "while true; do nc -l -p 24224 >> /tmp/received; done"));
|
||||
router.setFirelensConfiguration(new FirelensConfiguration("fluentbit", Map.of()));
|
||||
|
||||
TaskDefinition taskDefinition = new TaskDefinition();
|
||||
taskDefinition.setFamily("firelens-integration");
|
||||
taskDefinition.setContainerDefinitions(List.of(app, router));
|
||||
|
||||
EcsTask task = new EcsTask();
|
||||
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/firelens/" + marker);
|
||||
|
||||
taskHandle = containerManager.startTask(task, taskDefinition, List.of(), "us-east-1");
|
||||
|
||||
assertEquals(List.of("router", "app"), taskHandle.getContainerIds().keySet().stream().toList());
|
||||
String appId = taskHandle.getContainerIds().get("app");
|
||||
String routerId = taskHandle.getContainerIds().get("router");
|
||||
dockerClient.waitContainerCmd(appId)
|
||||
.exec(new WaitContainerResultCallback())
|
||||
.awaitStatusCode(30, TimeUnit.SECONDS);
|
||||
|
||||
String received = awaitRouterBuffer(routerId, marker);
|
||||
assertTrue(received.contains(marker), "FireLens router did not receive the app stdout record");
|
||||
}
|
||||
|
||||
private String awaitRouterBuffer(String routerId, String marker) throws Exception {
|
||||
for (int attempt = 0; attempt < 30; attempt++) {
|
||||
ExecResult result = readRouterBuffer(routerId, marker);
|
||||
if (result.exitCode() == 0 && result.output().contains(marker)) {
|
||||
return result.output();
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
return readRouterBuffer(routerId, marker).output();
|
||||
}
|
||||
|
||||
private ExecResult readRouterBuffer(String routerId, String marker) throws Exception {
|
||||
String execId = dockerClient.execCreateCmd(routerId)
|
||||
.withCmd("sh", "-c", "grep -a " + marker + " /tmp/received")
|
||||
.withAttachStdout(true)
|
||||
.withAttachStderr(true)
|
||||
.exec()
|
||||
.getId();
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
CountDownLatch complete = new CountDownLatch(1);
|
||||
Closeable callback = dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
try {
|
||||
if (frame.getStreamType() == StreamType.STDOUT) {
|
||||
output.write(frame.getPayload());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to capture FireLens output", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
complete.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
complete.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
assertTrue(complete.await(10, TimeUnit.SECONDS), "Timed out reading FireLens router buffer");
|
||||
long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
|
||||
return new ExecResult(exitCode, output.toString(StandardCharsets.UTF_8));
|
||||
} finally {
|
||||
callback.close();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDockerAvailable() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private record ExecResult(long exitCode, String output) {
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import com.github.dockerjava.api.model.LogConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.FirelensConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LogConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class EcsContainerManagerFirelensTest {
|
||||
|
||||
@Test
|
||||
void createsFluentdLogConfigForAwsFirelensApplication() {
|
||||
LogConfig logConfig = EcsContainerManager.firelensLogConfig("task-123", "app", 32768);
|
||||
|
||||
assertEquals(LogConfig.LoggingType.FLUENTD, logConfig.getType());
|
||||
assertEquals("127.0.0.1:32768", logConfig.getConfig().get("fluentd-address"));
|
||||
assertEquals("task-123.app", logConfig.getConfig().get("tag"));
|
||||
assertEquals("true", logConfig.getConfig().get("fluentd-async"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordersRouterBeforeAwsFirelensApplications() {
|
||||
ContainerDefinition app = container("app", "awsfirelens", null);
|
||||
ContainerDefinition router = container("router", null,
|
||||
new FirelensConfiguration("fluentbit", Map.of()));
|
||||
|
||||
List<ContainerDefinition> ordered = EcsContainerManager.orderFirelensContainers(List.of(app, router));
|
||||
|
||||
assertEquals("router", ordered.get(0).getName());
|
||||
assertEquals("app", ordered.get(1).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAwsFirelensApplicationWithoutRouter() {
|
||||
ContainerDefinition app = container("app", "awsfirelens", null);
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> EcsContainerManager.orderFirelensContainers(List.of(app)));
|
||||
}
|
||||
|
||||
private static ContainerDefinition container(String name, String driver,
|
||||
FirelensConfiguration firelensConfiguration) {
|
||||
ContainerDefinition definition = new ContainerDefinition();
|
||||
definition.setName(name);
|
||||
definition.setImage("alpine:latest");
|
||||
definition.setFirelensConfiguration(firelensConfiguration);
|
||||
if (driver != null) {
|
||||
definition.setLogConfiguration(new LogConfiguration(driver, Map.of(), null));
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
+33
@@ -12,6 +12,8 @@ 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.FirelensConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LogConfiguration;
|
||||
import io.github.hectorvent.floci.services.ecs.model.NetworkMode;
|
||||
import io.github.hectorvent.floci.services.ecs.model.PortMapping;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
@@ -221,4 +223,35 @@ class EcsContainerManagerPortMappingsTest {
|
||||
verify(builder, never()).withDynamicPort(9090);
|
||||
verify(builder, never()).withPortBinding(eq(9090), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void firelensRouterUsesLoopbackOnlyDynamicPortBinding() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
|
||||
ContainerDefinition router = new ContainerDefinition();
|
||||
router.setName("router");
|
||||
router.setImage("fluent/fluent-bit:latest");
|
||||
router.setFirelensConfiguration(new FirelensConfiguration("fluentbit", Map.of()));
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
app.setImage("app:latest");
|
||||
app.setLogConfiguration(new LogConfiguration("awsfirelens", Map.of(), null));
|
||||
|
||||
TaskDefinition taskDef = new TaskDefinition();
|
||||
taskDef.setFamily("test-family");
|
||||
taskDef.setContainerDefinitions(List.of(app, router));
|
||||
|
||||
EcsTask task = new EcsTask();
|
||||
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123");
|
||||
|
||||
when(lifecycleManager.createAndStart(any()))
|
||||
.thenReturn(new ContainerInfo("router-id", Map.of(), Map.of(24224, 32768)))
|
||||
.thenReturn(new ContainerInfo("app-id", Map.of()));
|
||||
|
||||
manager.startTask(task, taskDef, List.of(), "us-east-1");
|
||||
|
||||
verify(builder).withLoopbackPortBinding(24224, 0);
|
||||
verify(builder, never()).withDynamicPort(24224);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user