mirror of https://github.com/containers/podman.git
Return nil health when inspecting containers without healthchecks
When inspecting a container that does not define any health check, the health field should return nil. This matches docker behavior. Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
parent
4bef65240e
commit
a1c47f0299
|
@ -189,15 +189,20 @@ func (c *Container) getContainerInspectData(size bool, driverData *define.Driver
|
|||
data.OCIConfigPath = c.state.ConfigPath
|
||||
}
|
||||
|
||||
if c.config.HealthCheckConfig != nil {
|
||||
// Check if healthcheck is not nil and --no-healthcheck option is not set.
|
||||
// If --no-healthcheck is set Test will be always set to `[NONE]`, so the
|
||||
// inspect status should be set to nil.
|
||||
if c.config.HealthCheckConfig != nil && !(len(c.config.HealthCheckConfig.Test) == 1 && c.config.HealthCheckConfig.Test[0] == "NONE") {
|
||||
// This container has a healthcheck defined in it; we need to add its state
|
||||
healthCheckState, err := c.getHealthCheckLog()
|
||||
if err != nil {
|
||||
// An error here is not considered fatal; no health state will be displayed
|
||||
logrus.Error(err)
|
||||
} else {
|
||||
data.State.Health = healthCheckState
|
||||
data.State.Health = &healthCheckState
|
||||
}
|
||||
} else {
|
||||
data.State.Health = nil
|
||||
}
|
||||
|
||||
networkConfig, err := c.getContainerNetworkInfo()
|
||||
|
|
|
@ -219,7 +219,7 @@ type InspectContainerState struct {
|
|||
Error string `json:"Error"` // TODO
|
||||
StartedAt time.Time `json:"StartedAt"`
|
||||
FinishedAt time.Time `json:"FinishedAt"`
|
||||
Health HealthCheckResults `json:"Health,omitempty"`
|
||||
Health *HealthCheckResults `json:"Health,omitempty"`
|
||||
Checkpointed bool `json:"Checkpointed,omitempty"`
|
||||
CgroupPath string `json:"CgroupPath,omitempty"`
|
||||
CheckpointedAt time.Time `json:"CheckpointedAt,omitempty"`
|
||||
|
@ -233,7 +233,7 @@ type InspectContainerState struct {
|
|||
|
||||
// Healthcheck returns the HealthCheckResults. This is used for old podman compat
|
||||
// to make the "Healthcheck" key available in the go template.
|
||||
func (s *InspectContainerState) Healthcheck() HealthCheckResults {
|
||||
func (s *InspectContainerState) Healthcheck() *HealthCheckResults {
|
||||
return s.Health
|
||||
}
|
||||
|
||||
|
|
|
@ -442,17 +442,22 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,
|
|||
}
|
||||
|
||||
if l.HasHealthCheck() && state.Status != "created" {
|
||||
state.Health = &types.Health{
|
||||
Status: inspect.State.Health.Status,
|
||||
FailingStreak: inspect.State.Health.FailingStreak,
|
||||
}
|
||||
|
||||
state.Health = &types.Health{}
|
||||
if inspect.State.Health != nil {
|
||||
state.Health.Status = inspect.State.Health.Status
|
||||
state.Health.FailingStreak = inspect.State.Health.FailingStreak
|
||||
log := inspect.State.Health.Log
|
||||
|
||||
for _, item := range log {
|
||||
res := &types.HealthcheckResult{}
|
||||
s, _ := time.Parse(time.RFC3339Nano, item.Start)
|
||||
e, _ := time.Parse(time.RFC3339Nano, item.End)
|
||||
s, err := time.Parse(time.RFC3339Nano, item.Start)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e, err := time.Parse(time.RFC3339Nano, item.End)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.Start = s
|
||||
res.End = e
|
||||
res.ExitCode = item.ExitCode
|
||||
|
@ -460,6 +465,7 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,
|
|||
state.Health.Log = append(state.Health.Log, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
formatCapabilities(inspect.HostConfig.CapDrop)
|
||||
formatCapabilities(inspect.HostConfig.CapAdd)
|
||||
|
|
|
@ -34,10 +34,10 @@ var _ = Describe("Podman healthcheck run", func() {
|
|||
session := podmanTest.Podman([]string{"run", "-dt", "--no-healthcheck", "--name", "hc", HEALTHCHECK_IMAGE})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
hc := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.State.Health.Status}}", "hc"})
|
||||
hc := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.State.Health}}", "hc"})
|
||||
hc.WaitWithDefaultTimeout()
|
||||
Expect(hc).Should(ExitCleanly())
|
||||
Expect(hc.OutputToString()).To(Not(ContainSubstring("starting")))
|
||||
Expect(hc.OutputToString()).To(Equal("<nil>"))
|
||||
})
|
||||
|
||||
It("podman run healthcheck and logs should contain healthcheck output", func() {
|
||||
|
|
Loading…
Reference in New Issue