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 ( import (
"context" "context"
"fmt" "fmt"
"io"
"log/slog" "log/slog"
"os" "os"
"os/exec" "os/exec"
@ -183,10 +182,10 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
} }
// start logstash container // start logstash container
logstashLog := newCmdLogger(logger.WithGroup("logstash"))
runLogstashArgs := []string{ runLogstashArgs := []string{
"run", "run",
"--rm", "-d",
"--restart=unless-stopped",
"--name=logstash", "--name=logstash",
"--pod=logcollection", "--pod=logcollection",
"--log-driver=journald", "--log-driver=journald",
@ -195,9 +194,8 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
} }
runLogstashCmd := podman(ctx, runLogstashArgs...) runLogstashCmd := podman(ctx, runLogstashArgs...)
logger.Info(fmt.Sprintf("Run logstash command: %v", runLogstashCmd.String())) logger.Info(fmt.Sprintf("Run logstash command: %v", runLogstashCmd.String()))
runLogstashCmd.Stdout = logstashLog if out, err := runLogstashCmd.CombinedOutput(); err != nil {
runLogstashCmd.Stderr = logstashLog logger.Error("Could not start logstash container", "err", err, "output", out)
if err := runLogstashCmd.Start(); err != nil {
return fmt.Errorf("failed to start logstash: %w", err) return fmt.Errorf("failed to start logstash: %w", err)
} }
if out, err := podman(ctx, "wait", "logstash", "--condition=running", "--interval=15s").CombinedOutput(); err != nil { 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 // start filebeat container
filebeatLog := newCmdLogger(logger.WithGroup("filebeat"))
runFilebeatArgs := []string{ runFilebeatArgs := []string{
"run", "run",
"--rm", "-d",
"--restart=unless-stopped",
"--name=filebeat", "--name=filebeat",
"--pod=logcollection", "--pod=logcollection",
"--privileged", "--privileged",
@ -224,9 +222,8 @@ func startPod(ctx context.Context, logger *slog.Logger) error {
} }
runFilebeatCmd := podman(ctx, runFilebeatArgs...) runFilebeatCmd := podman(ctx, runFilebeatArgs...)
logger.Info(fmt.Sprintf("Run filebeat command: %v", runFilebeatCmd.String())) logger.Info(fmt.Sprintf("Run filebeat command: %v", runFilebeatCmd.String()))
runFilebeatCmd.Stdout = filebeatLog if out, err := runFilebeatCmd.CombinedOutput(); err != nil {
runFilebeatCmd.Stderr = filebeatLog logger.Error("Could not start filebeat container", "err", err, "output", out)
if err := runFilebeatCmd.Start(); err != nil {
return fmt.Errorf("failed to run filebeat: %w", err) return fmt.Errorf("failed to run filebeat: %w", err)
} }
if out, err := podman(ctx, "wait", "filebeat", "--condition=running", "--interval=15s").CombinedOutput(); err != nil { 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 { func podman(ctx context.Context, args ...string) *exec.Cmd {
args = append([]string{"--runtime=runc"}, args...) args = append([]string{"--runtime=runc"}, args...)
return exec.CommandContext(ctx, "podman", args...) return exec.CommandContext(ctx, "podman", args...)