mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(docker): follow adopted sidecar containers from now instead of replaying their log history (#3612)
This commit is contained in:
1 parent
49224917e1
commit
ebb79ed3c2
5 files changed
+119
-15
No files matched your search
+26
-3
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.LogContainerCmd;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -14,6 +15,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayDeque;
|
||||
@@ -69,9 +71,27 @@ public class ContainerLogStreamer {
|
||||
null, containerId, logGroup, logStream, region, logPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #attach}, but only forwards lines the container emits from now on. Use it for a
|
||||
* container that already existed before this process started: following from the beginning
|
||||
* replays its whole history (hours of registry access logs after a busy day) into the console
|
||||
* and CloudWatch Logs, where it evicts the current Lambda logs.
|
||||
*/
|
||||
public Closeable attachFromNow(String containerId, String logGroup, String logStream,
|
||||
String region, String logPrefix) {
|
||||
return attachForAccount(null, containerId, logGroup, logStream, region, logPrefix, Instant.now());
|
||||
}
|
||||
|
||||
public Closeable attachForAccount(
|
||||
String accountId, String containerId, String logGroup, String logStream,
|
||||
String region, String logPrefix) {
|
||||
return attachForAccount(accountId, containerId, logGroup, logStream, region, logPrefix, null);
|
||||
}
|
||||
|
||||
/** {@code since} of null follows the container's complete log history. */
|
||||
Closeable attachForAccount(
|
||||
String accountId, String containerId, String logGroup, String logStream,
|
||||
String region, String logPrefix, Instant since) {
|
||||
ensureLogGroupAndStreamForAccount(accountId, logGroup, logStream, region);
|
||||
|
||||
// Trim trailing whitespace and drop blank lines, then fan each reassembled line out to the
|
||||
@@ -86,12 +106,15 @@ public class ContainerLogStreamer {
|
||||
|
||||
try {
|
||||
LogReassemblyCallback callback = new LogReassemblyCallback(emitter);
|
||||
dockerClient.logContainerCmd(containerId)
|
||||
LogContainerCmd command = dockerClient.logContainerCmd(containerId)
|
||||
.withStdOut(true)
|
||||
.withStdErr(true)
|
||||
.withFollowStream(true)
|
||||
.withTimestamps(false)
|
||||
.exec(callback);
|
||||
.withTimestamps(false);
|
||||
if (since != null) {
|
||||
command = command.withSince((int) since.getEpochSecond());
|
||||
}
|
||||
command.exec(callback);
|
||||
return new ContainerLogHandle(callback);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not attach log stream for container {0}: {1}", containerId, e.getMessage());
|
||||
|
||||
+11
-8
@@ -32,6 +32,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -292,7 +293,7 @@ public class EcrRegistryManager {
|
||||
LOG.infov("Started ECR backing registry {0} on host port {1}", name, String.valueOf(chosenPort));
|
||||
|
||||
// Attach log streaming (new feature)
|
||||
attachLogStream();
|
||||
attachLogStream(false);
|
||||
} catch (Exception e) {
|
||||
// Release the reserved port unless the container actually started, so a
|
||||
// failed start (e.g. Docker unreachable) does not permanently exhaust the
|
||||
@@ -330,15 +331,17 @@ public class EcrRegistryManager {
|
||||
specBuilder.withBind(hostDataPath, "/var/lib/registry");
|
||||
}
|
||||
|
||||
private void attachLogStream() {
|
||||
// An adopted container carries history from before this process; only its new lines are wanted.
|
||||
private void attachLogStream(boolean adopted) {
|
||||
closeLogStream();
|
||||
String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId;
|
||||
String logGroup = "/aws/ecr/registry";
|
||||
String logStreamName = logStreamer.generateLogStreamName(shortId);
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
|
||||
this.logStream = logStreamer.attach(
|
||||
containerId, logGroup, logStreamName, region, "ecr:registry");
|
||||
this.logStream = adopted
|
||||
? logStreamer.attachFromNow(containerId, logGroup, logStreamName, region, "ecr:registry")
|
||||
: logStreamer.attach(containerId, logGroup, logStreamName, region, "ecr:registry");
|
||||
}
|
||||
|
||||
private void closeLogStream() {
|
||||
@@ -354,15 +357,15 @@ public class EcrRegistryManager {
|
||||
}
|
||||
}
|
||||
|
||||
private java.util.Optional<String> resolveRegistryDockerNetwork() {
|
||||
java.util.Optional<String> configured = config.services().ecr().dockerNetwork();
|
||||
private Optional<String> resolveRegistryDockerNetwork() {
|
||||
Optional<String> configured = config.services().ecr().dockerNetwork();
|
||||
if (configured.isPresent() && !configured.get().isBlank()) {
|
||||
return configured;
|
||||
}
|
||||
if (containerDetector.isRunningInContainer()) {
|
||||
return currentContainerNetworkResolver.resolveNetworkName();
|
||||
}
|
||||
return java.util.Optional.empty();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private void runReconcileOnce() {
|
||||
@@ -575,7 +578,7 @@ public class EcrRegistryManager {
|
||||
containerId, String.valueOf(hostPort));
|
||||
|
||||
// Attach log streaming to adopted container
|
||||
attachLogStream();
|
||||
attachLogStream(true);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to adopt existing ECR registry container: {0}", e.getMessage());
|
||||
this.containerId = null;
|
||||
|
||||
@@ -207,7 +207,7 @@ public class FlociUiManager {
|
||||
this.lastError = null;
|
||||
LOG.infov("Started web console sidecar {0} from {1} on host port {2}",
|
||||
name, image, String.valueOf(hostPort));
|
||||
attachLogStream();
|
||||
attachLogStream(false);
|
||||
} catch (IllegalStateException e) {
|
||||
// replaceIfEndpointDrifted() records its own specific message for a container with an
|
||||
// existing sidecar to adopt, but a fresh start (no existing container) reaches this
|
||||
@@ -746,20 +746,23 @@ public class FlociUiManager {
|
||||
this.lastError = null;
|
||||
LOG.infov("Adopted existing web console sidecar {0} on host port {1}",
|
||||
containerId, String.valueOf(hostPort));
|
||||
attachLogStream();
|
||||
attachLogStream(true);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to adopt the existing web console sidecar: {0}", e.getMessage());
|
||||
this.containerId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void attachLogStream() {
|
||||
// An adopted container carries history from before this process; only its new lines are wanted.
|
||||
private void attachLogStream(boolean adopted) {
|
||||
closeLogStream();
|
||||
String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId;
|
||||
String logGroup = "/floci/ui";
|
||||
String logStreamName = logStreamer.generateLogStreamName(shortId);
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
this.logStream = logStreamer.attach(containerId, logGroup, logStreamName, region, "floci:ui");
|
||||
this.logStream = adopted
|
||||
? logStreamer.attachFromNow(containerId, logGroup, logStreamName, region, "floci:ui")
|
||||
: logStreamer.attach(containerId, logGroup, logStreamName, region, "floci:ui");
|
||||
}
|
||||
|
||||
/** Releases the previous follower, so a restarted sidecar does not leave one behind. */
|
||||
|
||||
Reference in new issue
Block a user