From d42de1cc7cd2b045a985d50528fd62d4bcf7c469 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 3 Apr 2024 23:38:12 +0300 Subject: [PATCH] account for exited but not because it was fatal --- check_health.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/check_health.go b/check_health.go index 29525ab0b7..df043da1fb 100644 --- a/check_health.go +++ b/check_health.go @@ -19,14 +19,15 @@ import ( ) type Result struct { - Name string - Fatal bool - Healthy bool - HasCheck bool - Logs string - ProbeLogs string + Name string // Name of the container + Fatal bool // True if the container is exited with a non-zero exit code. + Healthy bool // True if the container is healthy, false if probe is failing + HasCheck bool // True if the container has a health check + Logs string // Logs of the container + ProbeLogs string // Logs of the probe } +// map[container name]result type Results map[string]Result var flag_name string @@ -140,10 +141,15 @@ func checkContainer(c d_types.Container, checksCh chan Result) { res.HasCheck, _ = hasHealthCheck(c.ID) res.Name = getContainerName(c.Names) running, _ := isRunning(c.ID) + exitCode, _ := getExitCode(c.ID) if !running { - res.Fatal = true + if exitCode != 0 { + res.Fatal = true + res.ProbeLogs, _ = getFailedProbeLogs(c.ID) + } else { + res.Healthy = false + } res.Logs, _ = getLogs(c.ID) - res.ProbeLogs, _ = getFailedProbeLogs(c.ID) checksCh <- res return } @@ -201,6 +207,14 @@ func isRunning(cID string) (bool, error) { return container.State.Running, nil } +func getExitCode(cID string) (int, error) { + container, err := apiClient.ContainerInspect(context.Background(), cID) + if err != nil { + return 0, fmt.Errorf("failed to inspect container: %w", err) + } + + return container.State.ExitCode, nil +} // getHealth returns the health status of the container func getHealth(cID string) (string, error) {