podman-healthcheck-run: add --ignore-result flag

This is useful for triggering the execution of a healthcheck
without caring about it's result as long as no fatal error occured.

Signed-off-by: Patrick Wicki <patrick.wicki@siemens.com>
This commit is contained in:
Patrick Wicki
2026-01-19 17:03:14 +01:00
parent c18a9ea4a3
commit 3856389fc9
4 changed files with 56 additions and 3 deletions

View File

@@ -12,7 +12,7 @@ import (
)
var runCmd = &cobra.Command{
Use: "run CONTAINER",
Use: "run [options] CONTAINER",
Short: "Run the health check of a container",
Long: "Run the health check of a container",
Example: `podman healthcheck run mywebapp`,
@@ -21,11 +21,17 @@ var runCmd = &cobra.Command{
ValidArgsFunction: common.AutocompleteContainersRunning,
}
var ignoreResult bool
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: runCmd,
Parent: healthCmd,
})
flags := runCmd.Flags()
flags.BoolVar(&ignoreResult, "ignore-result", false,
"Exit with code 0 regardless of healthcheck result or if the container is still in startup period")
}
func run(_ *cobra.Command, args []string) error {
@@ -35,7 +41,11 @@ func run(_ *cobra.Command, args []string) error {
}
switch response.Status {
case define.HealthCheckUnhealthy, define.HealthCheckStarting, define.HealthCheckStopped:
registry.SetExitCode(1)
if ignoreResult {
registry.SetExitCode(0)
} else {
registry.SetExitCode(1)
}
fmt.Println(response.Status)
}
return err

View File

@@ -4,7 +4,7 @@
podman\-healthcheck\-run - Run a container healthcheck
## SYNOPSIS
**podman healthcheck run** *container*
**podman healthcheck run** [*options*] *container*
## DESCRIPTION
@@ -25,6 +25,10 @@ Possible errors that can occur during the healthcheck are:
Print usage statement
#### **--ignore-result**
Exit with code 0 regardless of the healthcheck result and if the container is
still in the startup period. Other errors will not be ignored.
## EXAMPLES

View File

@@ -161,6 +161,16 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(inspect[0].State.Health).To(HaveField("Status", "starting"))
})
It("podman healthcheck --ignore-result exits 0 on failing healthcheck", func() {
session := podmanTest.Podman([]string{"run", "-q", "-dt", "--name", "hc", "quay.io/libpod/badhealthcheck:latest"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
hc := podmanTest.Podman([]string{"healthcheck", "run", "--ignore-result", "hc"})
hc.WaitWithDefaultTimeout()
Expect(hc).Should(ExitWithError(0, ""))
})
It("podman healthcheck failed checks in start-period should not change status", func() {
session := podmanTest.Podman([]string{"run", "-dt", "--name", "hc", "--health-start-period", "2m", "--health-retries", "2", "--health-cmd", "ls /foo || exit 1", ALPINE, "top"})
session.WaitWithDefaultTimeout()

View File

@@ -457,6 +457,35 @@ function _check_health_log {
run_podman rm -f -t0 $ctr
}
@test "podman healthcheck --ignore-result" {
ctr="c-h-$(safename)"
run_podman run -d --name $ctr \
--health-cmd /home/podman/healthcheck \
--health-retries=1 \
--health-interval=disable \
$IMAGE /home/podman/pause
# Healthcheck should succeed normally
run_podman healthcheck run $ctr
is "$output" "" "output from 'podman healthcheck run'"
# Now make the healthcheck fail
run_podman exec $ctr touch /uh-oh
run_podman 1 healthcheck run $ctr
is "$output" "unhealthy" "output from 'podman healthcheck run' without --ignore-result"
# With --ignore-result, we should still get "unhealthy" but exit 0
run_podman healthcheck run --ignore-result $ctr
is "$output" "unhealthy" "output from 'podman healthcheck run --ignore-result'"
# It should NOT suppress fatal errors however (e.g., nonexistent container)
run_podman 125 healthcheck run --ignore-result "nonexistent-$(safename)"
assert "$output" =~ "no such container"
run_podman rm -f -t0 $ctr
}
# https://github.com/containers/podman/issues/25034
@test "podman healthcheck - start errors" {
skip_if_remote '$PATH overwrite not working via remote'