From 7e6e2673296346c05a7694cc1b058c3f90f7b6b6 Mon Sep 17 00:00:00 2001 From: Chetan Giradkar Date: Mon, 25 Sep 2023 12:46:34 +0100 Subject: [PATCH] Filter health_check and exec events for logging in console Podman server logs are mostly full of healthcheck output, making them hard to navigate. Hence, made healthcheck service to run with LogLevelMax=notice, this would remove the normal output, inclusive the started/stopped messages from systemd itself. Fixes #17856 Signed-off-by: Chetan Giradkar --- libpod/container_exec.go | 12 +++++++----- libpod/healthcheck_linux.go | 2 +- test/e2e/healthcheck_run_test.go | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libpod/container_exec.go b/libpod/container_exec.go index 661be832aa..9dea0216f5 100644 --- a/libpod/container_exec.go +++ b/libpod/container_exec.go @@ -401,7 +401,7 @@ func (c *Container) execStartAndAttach(sessionID string, streams *define.AttachS logrus.Debugf("Container %s exec session %s completed with exit code %d", c.ID(), session.ID(), exitCode) - if err := justWriteExecExitCode(c, session.ID(), exitCode); err != nil { + if err := justWriteExecExitCode(c, session.ID(), exitCode, !isHealthcheck); err != nil { if lastErr != nil { logrus.Errorf("Container %s exec session %s error: %v", c.ID(), session.ID(), lastErr) } @@ -1114,7 +1114,7 @@ func writeExecExitCode(c *Container, sessionID string, exitCode int) error { return fmt.Errorf("syncing container %s state to remove exec session %s: %w", c.ID(), sessionID, err) } - return justWriteExecExitCode(c, sessionID, exitCode) + return justWriteExecExitCode(c, sessionID, exitCode, true) } func retrieveAndWriteExecExitCode(c *Container, sessionID string) error { @@ -1123,12 +1123,14 @@ func retrieveAndWriteExecExitCode(c *Container, sessionID string) error { return err } - return justWriteExecExitCode(c, sessionID, exitCode) + return justWriteExecExitCode(c, sessionID, exitCode, true) } -func justWriteExecExitCode(c *Container, sessionID string, exitCode int) error { +func justWriteExecExitCode(c *Container, sessionID string, exitCode int, emitEvent bool) error { // Write an event first - c.newExecDiedEvent(sessionID, exitCode) + if emitEvent { + c.newExecDiedEvent(sessionID, exitCode) + } session, ok := c.state.ExecSessions[sessionID] if !ok { diff --git a/libpod/healthcheck_linux.go b/libpod/healthcheck_linux.go index a0502e5775..04268e5eec 100644 --- a/libpod/healthcheck_linux.go +++ b/libpod/healthcheck_linux.go @@ -27,7 +27,7 @@ func (c *Container) createTimer(interval string, isStartup bool) error { return fmt.Errorf("failed to get path for podman for a health check timer: %w", err) } - var cmd = []string{} + var cmd = []string{"--property", "LogLevelMax=notice"} if rootless.IsRootless() { cmd = append(cmd, "--user") } diff --git a/test/e2e/healthcheck_run_test.go b/test/e2e/healthcheck_run_test.go index 52ce570285..8aafd78625 100644 --- a/test/e2e/healthcheck_run_test.go +++ b/test/e2e/healthcheck_run_test.go @@ -263,6 +263,20 @@ var _ = Describe("Podman healthcheck run", func() { Expect(ps.OutputToString()).To(ContainSubstring("hc")) }) + It("hc logs do not include exec events", func() { + session := podmanTest.Podman([]string{"run", "-dt", "--name", "hc", "--health-cmd", "true", "--health-interval", "5s", "alpine", "sleep", "60"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + hc := podmanTest.Podman([]string{"healthcheck", "run", "hc"}) + hc.WaitWithDefaultTimeout() + Expect(hc).Should(ExitCleanly()) + hcLogs := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "container=hc", "--filter", "event=exec_died", "--filter", "event=exec", "--since", "1m"}) + hcLogs.WaitWithDefaultTimeout() + Expect(hcLogs).Should(ExitCleanly()) + hcLogsArray := hcLogs.OutputToStringArray() + Expect(hcLogsArray).To(BeEmpty()) + }) + It("stopping and then starting a container with healthcheck cmd", func() { session := podmanTest.Podman([]string{"run", "-dt", "--name", "hc", "--health-cmd", "[\"ls\", \"/foo\"]", ALPINE, "top"}) session.WaitWithDefaultTimeout()