Skip to content

Commit 1698fa0

Browse files
Merge pull request #21388 from ashley-cui/healthcheck
Return nil health when inspecting containers without healthchecks
2 parents 0655bf3 + a1c47f0 commit 1698fa0

File tree

4 files changed

+55
-44
lines changed

4 files changed

+55
-44
lines changed

libpod/container_inspect.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,20 @@ func (c *Container) getContainerInspectData(size bool, driverData *define.Driver
189189
data.OCIConfigPath = c.state.ConfigPath
190190
}
191191

192-
if c.config.HealthCheckConfig != nil {
192+
// Check if healthcheck is not nil and --no-healthcheck option is not set.
193+
// If --no-healthcheck is set Test will be always set to `[NONE]`, so the
194+
// inspect status should be set to nil.
195+
if c.config.HealthCheckConfig != nil && !(len(c.config.HealthCheckConfig.Test) == 1 && c.config.HealthCheckConfig.Test[0] == "NONE") {
193196
// This container has a healthcheck defined in it; we need to add its state
194197
healthCheckState, err := c.getHealthCheckLog()
195198
if err != nil {
196199
// An error here is not considered fatal; no health state will be displayed
197200
logrus.Error(err)
198201
} else {
199-
data.State.Health = healthCheckState
202+
data.State.Health = &healthCheckState
200203
}
204+
} else {
205+
data.State.Health = nil
201206
}
202207

203208
networkConfig, err := c.getContainerNetworkInfo()

libpod/define/container_inspect.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,34 +206,34 @@ type InspectMount struct {
206206
// Docker, but here we see more fields that are unused (nonsensical in the
207207
// context of Libpod).
208208
type InspectContainerState struct {
209-
OciVersion string `json:"OciVersion"`
210-
Status string `json:"Status"`
211-
Running bool `json:"Running"`
212-
Paused bool `json:"Paused"`
213-
Restarting bool `json:"Restarting"` // TODO
214-
OOMKilled bool `json:"OOMKilled"`
215-
Dead bool `json:"Dead"`
216-
Pid int `json:"Pid"`
217-
ConmonPid int `json:"ConmonPid,omitempty"`
218-
ExitCode int32 `json:"ExitCode"`
219-
Error string `json:"Error"` // TODO
220-
StartedAt time.Time `json:"StartedAt"`
221-
FinishedAt time.Time `json:"FinishedAt"`
222-
Health HealthCheckResults `json:"Health,omitempty"`
223-
Checkpointed bool `json:"Checkpointed,omitempty"`
224-
CgroupPath string `json:"CgroupPath,omitempty"`
225-
CheckpointedAt time.Time `json:"CheckpointedAt,omitempty"`
226-
RestoredAt time.Time `json:"RestoredAt,omitempty"`
227-
CheckpointLog string `json:"CheckpointLog,omitempty"`
228-
CheckpointPath string `json:"CheckpointPath,omitempty"`
229-
RestoreLog string `json:"RestoreLog,omitempty"`
230-
Restored bool `json:"Restored,omitempty"`
231-
StoppedByUser bool `json:"StoppedByUser,omitempty"`
209+
OciVersion string `json:"OciVersion"`
210+
Status string `json:"Status"`
211+
Running bool `json:"Running"`
212+
Paused bool `json:"Paused"`
213+
Restarting bool `json:"Restarting"` // TODO
214+
OOMKilled bool `json:"OOMKilled"`
215+
Dead bool `json:"Dead"`
216+
Pid int `json:"Pid"`
217+
ConmonPid int `json:"ConmonPid,omitempty"`
218+
ExitCode int32 `json:"ExitCode"`
219+
Error string `json:"Error"` // TODO
220+
StartedAt time.Time `json:"StartedAt"`
221+
FinishedAt time.Time `json:"FinishedAt"`
222+
Health *HealthCheckResults `json:"Health,omitempty"`
223+
Checkpointed bool `json:"Checkpointed,omitempty"`
224+
CgroupPath string `json:"CgroupPath,omitempty"`
225+
CheckpointedAt time.Time `json:"CheckpointedAt,omitempty"`
226+
RestoredAt time.Time `json:"RestoredAt,omitempty"`
227+
CheckpointLog string `json:"CheckpointLog,omitempty"`
228+
CheckpointPath string `json:"CheckpointPath,omitempty"`
229+
RestoreLog string `json:"RestoreLog,omitempty"`
230+
Restored bool `json:"Restored,omitempty"`
231+
StoppedByUser bool `json:"StoppedByUser,omitempty"`
232232
}
233233

234234
// Healthcheck returns the HealthCheckResults. This is used for old podman compat
235235
// to make the "Healthcheck" key available in the go template.
236-
func (s *InspectContainerState) Healthcheck() HealthCheckResults {
236+
func (s *InspectContainerState) Healthcheck() *HealthCheckResults {
237237
return s.Health
238238
}
239239

pkg/api/handlers/compat/containers.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,22 +442,28 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,
442442
}
443443

444444
if l.HasHealthCheck() && state.Status != "created" {
445-
state.Health = &types.Health{
446-
Status: inspect.State.Health.Status,
447-
FailingStreak: inspect.State.Health.FailingStreak,
448-
}
449-
450-
log := inspect.State.Health.Log
451-
452-
for _, item := range log {
453-
res := &types.HealthcheckResult{}
454-
s, _ := time.Parse(time.RFC3339Nano, item.Start)
455-
e, _ := time.Parse(time.RFC3339Nano, item.End)
456-
res.Start = s
457-
res.End = e
458-
res.ExitCode = item.ExitCode
459-
res.Output = item.Output
460-
state.Health.Log = append(state.Health.Log, res)
445+
state.Health = &types.Health{}
446+
if inspect.State.Health != nil {
447+
state.Health.Status = inspect.State.Health.Status
448+
state.Health.FailingStreak = inspect.State.Health.FailingStreak
449+
log := inspect.State.Health.Log
450+
451+
for _, item := range log {
452+
res := &types.HealthcheckResult{}
453+
s, err := time.Parse(time.RFC3339Nano, item.Start)
454+
if err != nil {
455+
return nil, err
456+
}
457+
e, err := time.Parse(time.RFC3339Nano, item.End)
458+
if err != nil {
459+
return nil, err
460+
}
461+
res.Start = s
462+
res.End = e
463+
res.ExitCode = item.ExitCode
464+
res.Output = item.Output
465+
state.Health.Log = append(state.Health.Log, res)
466+
}
461467
}
462468
}
463469

test/e2e/healthcheck_run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ var _ = Describe("Podman healthcheck run", func() {
3434
session := podmanTest.Podman([]string{"run", "-dt", "--no-healthcheck", "--name", "hc", HEALTHCHECK_IMAGE})
3535
session.WaitWithDefaultTimeout()
3636
Expect(session).Should(ExitCleanly())
37-
hc := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.State.Health.Status}}", "hc"})
37+
hc := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.State.Health}}", "hc"})
3838
hc.WaitWithDefaultTimeout()
3939
Expect(hc).Should(ExitCleanly())
40-
Expect(hc.OutputToString()).To(Not(ContainSubstring("starting")))
40+
Expect(hc.OutputToString()).To(Equal("<nil>"))
4141
})
4242

4343
It("podman run healthcheck and logs should contain healthcheck output", func() {

0 commit comments

Comments
 (0)