fix: add missing service container health check (#2354)

* fix: Implement missing health ceck for Services

* Add test case

* linter doesn't support min builtin and fix check

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
ChristopherHX
2024-07-10 17:33:54 +02:00
committed by GitHub
parent 1ac4b60a06
commit 2ad5ff74f8
6 changed files with 93 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ type Container interface {
Remove() common.Executor
Close() common.Executor
ReplaceLogWriter(io.Writer, io.Writer) (io.Writer, io.Writer)
GetHealth(ctx context.Context) ContainerHealth
}
// NewDockerBuildExecutorInput the input for the NewDockerBuildExecutor function
@@ -73,3 +74,11 @@ type NewDockerPullExecutorInput struct {
Username string
Password string
}
type ContainerHealth int
const (
ContainerHealthStarting ContainerHealth = iota
ContainerHealthHealthy
ContainerHealthUnHealthy
)

View File

@@ -169,6 +169,30 @@ func (cr *containerReference) Remove() common.Executor {
).IfNot(common.Dryrun)
}
func (cr *containerReference) GetHealth(ctx context.Context) ContainerHealth {
resp, err := cr.cli.ContainerInspect(ctx, cr.id)
logger := common.Logger(ctx)
if err != nil {
logger.Errorf("failed to query container health %s", err)
return ContainerHealthUnHealthy
}
if resp.Config == nil || resp.Config.Healthcheck == nil || resp.State == nil || resp.State.Health == nil || len(resp.Config.Healthcheck.Test) == 1 && strings.EqualFold(resp.Config.Healthcheck.Test[0], "NONE") {
logger.Debugf("no container health check defined")
return ContainerHealthHealthy
}
logger.Infof("container health of %s (%s) is %s", cr.id, resp.Config.Image, resp.State.Health.Status)
switch resp.State.Health.Status {
case "starting":
return ContainerHealthStarting
case "healthy":
return ContainerHealthHealthy
case "unhealthy":
return ContainerHealthUnHealthy
}
return ContainerHealthUnHealthy
}
func (cr *containerReference) ReplaceLogWriter(stdout io.Writer, stderr io.Writer) (io.Writer, io.Writer) {
out := cr.input.Stdout
err := cr.input.Stderr

View File

@@ -452,6 +452,10 @@ func (e *HostEnvironment) GetRunnerContext(_ context.Context) map[string]interfa
}
}
func (e *HostEnvironment) GetHealth(ctx context.Context) ContainerHealth {
return ContainerHealthHealthy
}
func (e *HostEnvironment) ReplaceLogWriter(stdout io.Writer, _ io.Writer) (io.Writer, io.Writer) {
org := e.StdOut
e.StdOut = stdout