debugd: daemonize logstash containers (#3216)

* debugd: daemonize logstash containers

* fixup! debugd: daemonize logstash containers

remove in-process logging
This commit is contained in:
Markus Rudy 2024-06-28 14:56:46 +02:00 committed by GitHub
parent 782752080f
commit ab94456455
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,6 @@ package logcollector
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
@ -183,10 +182,10 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
}
// start logstash container
logstashLog := newCmdLogger(logger.WithGroup("logstash"))
runLogstashArgs := []string{
"run",
"--rm",
"-d",
"--restart=unless-stopped",
"--name=logstash",
"--pod=logcollection",
"--log-driver=journald",
@ -195,9 +194,8 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
}
runLogstashCmd := podman(ctx, runLogstashArgs...)
logger.Info(fmt.Sprintf("Run logstash command: %v", runLogstashCmd.String()))
runLogstashCmd.Stdout = logstashLog
runLogstashCmd.Stderr = logstashLog
if err := runLogstashCmd.Start(); err != nil {
if out, err := runLogstashCmd.CombinedOutput(); err != nil {
logger.Error("Could not start logstash container", "err", err, "output", out)
return fmt.Errorf("failed to start logstash: %w", err)
}
if out, err := podman(ctx, "wait", "logstash", "--condition=running", "--interval=15s").CombinedOutput(); err != nil {
@ -206,10 +204,10 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
}
// start filebeat container
filebeatLog := newCmdLogger(logger.WithGroup("filebeat"))
runFilebeatArgs := []string{
"run",
"--rm",
"-d",
"--restart=unless-stopped",
"--name=filebeat",
"--pod=logcollection",
"--privileged",
@ -224,9 +222,8 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
}
runFilebeatCmd := podman(ctx, runFilebeatArgs...)
logger.Info(fmt.Sprintf("Run filebeat command: %v", runFilebeatCmd.String()))
runFilebeatCmd.Stdout = filebeatLog
runFilebeatCmd.Stderr = filebeatLog
if err := runFilebeatCmd.Start(); err != nil {
if out, err := runFilebeatCmd.CombinedOutput(); err != nil {
logger.Error("Could not start filebeat container", "err", err, "output", out)
return fmt.Errorf("failed to run filebeat: %w", err)
}
if out, err := podman(ctx, "wait", "filebeat", "--condition=running", "--interval=15s").CombinedOutput(); err != nil {
@ -303,19 +300,6 @@ func setCloudMetadata(ctx context.Context, m map[string]string, provider cloudpr
}
}
func newCmdLogger(logger *slog.Logger) io.Writer {
return &cmdLogger{logger: logger}
}
type cmdLogger struct {
logger *slog.Logger
}
func (c *cmdLogger) Write(p []byte) (n int, err error) {
c.logger.Info(string(p))
return len(p), nil
}
func podman(ctx context.Context, args ...string) *exec.Cmd {
args = append([]string{"--runtime=runc"}, args...)
return exec.CommandContext(ctx, "podman", args...)