From 28774f18c5f49b86f768e93f4db2b53eeee077f0 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 5 Dec 2022 20:15:03 +0100 Subject: [PATCH] disable healthchecks automatically on non systemd systems The podman healthchecks are implemented using systemd timers, this works great but it will never work on non systemd distros. Currently the logic always assumes systemd is available and will fail with an error, so users are forced to always run with `--no-healthcheck` to disable healthchecks that are defined in an image for example. This is annoying and IMO unnecessary, we should just default to no healthcheck on these systems. First, use the systemd build tag to disable it at build time if this tag is not used. Second, use make sure systemd is used as init before trying to use healthchecks. This could be the case when we are run in a container. [NO NEW TESTS NEEDED] We do not have any non systemd VMs in CI AFAIK. Fixes #16644 Signed-off-by: Paul Holzinger --- libpod/healthcheck.go | 24 ----------------------- libpod/healthcheck_linux.go | 28 +++++++++++++++++++++++++++ libpod/healthcheck_nosystemd_linux.go | 24 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 libpod/healthcheck_nosystemd_linux.go diff --git a/libpod/healthcheck.go b/libpod/healthcheck.go index 165520ac49..d10ba8f3ba 100644 --- a/libpod/healthcheck.go +++ b/libpod/healthcheck.go @@ -432,27 +432,3 @@ func (c *Container) healthCheckStatus() (string, error) { return results.Status, nil } - -func (c *Container) disableHealthCheckSystemd(isStartup bool) bool { - if os.Getenv("DISABLE_HC_SYSTEMD") == "true" { - return true - } - if isStartup { - if c.config.StartupHealthCheckConfig.Interval == 0 { - return true - } - } - if c.config.HealthCheckConfig.Interval == 0 { - return true - } - return false -} - -// Systemd unit name for the healthcheck systemd unit -func (c *Container) hcUnitName(isStartup bool) string { - unitName := c.ID() - if isStartup { - unitName += "-startup" - } - return unitName -} diff --git a/libpod/healthcheck_linux.go b/libpod/healthcheck_linux.go index 5dda104107..1ebd58b7a5 100644 --- a/libpod/healthcheck_linux.go +++ b/libpod/healthcheck_linux.go @@ -1,3 +1,6 @@ +//go:build systemd +// +build systemd + package libpod import ( @@ -10,6 +13,7 @@ import ( "github.com/containers/podman/v4/pkg/errorhandling" "github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/systemd" + "github.com/containers/podman/v4/utils" "github.com/sirupsen/logrus" ) @@ -132,3 +136,27 @@ func (c *Container) removeTransientFiles(ctx context.Context, isStartup bool) er return errorhandling.JoinErrors(stopErrors) } + +func (c *Container) disableHealthCheckSystemd(isStartup bool) bool { + if !utils.RunsOnSystemd() || os.Getenv("DISABLE_HC_SYSTEMD") == "true" { + return true + } + if isStartup { + if c.config.StartupHealthCheckConfig.Interval == 0 { + return true + } + } + if c.config.HealthCheckConfig.Interval == 0 { + return true + } + return false +} + +// Systemd unit name for the healthcheck systemd unit +func (c *Container) hcUnitName(isStartup bool) string { + unitName := c.ID() + if isStartup { + unitName += "-startup" + } + return unitName +} diff --git a/libpod/healthcheck_nosystemd_linux.go b/libpod/healthcheck_nosystemd_linux.go new file mode 100644 index 0000000000..2e3b15dcff --- /dev/null +++ b/libpod/healthcheck_nosystemd_linux.go @@ -0,0 +1,24 @@ +//go:build !systemd +// +build !systemd + +package libpod + +import ( + "context" +) + +// createTimer systemd timers for healthchecks of a container +func (c *Container) createTimer(interval string, isStartup bool) error { + return nil +} + +// startTimer starts a systemd timer for the healthchecks +func (c *Container) startTimer(isStartup bool) error { + return nil +} + +// removeTransientFiles removes the systemd timer and unit files +// for the container +func (c *Container) removeTransientFiles(ctx context.Context, isStartup bool) error { + return nil +}