Skip to content

Commit

Permalink
podman inspect add State.Health field for docker compat
Browse files Browse the repository at this point in the history
podman inspect shows the healthcheck status in `.State.Healthcheck`,
docker uses `.State.Health`. To make sure docker scripts work we
should add the `Health` key. Because we do not want to display both keys
by default we only use the new `Health` key. This is a breaking change
for podman users but matches what docker does. To provide some form of
compatibility users can still use `--format {{.State.Healthcheck}}`. IT
is just not shown by default.

Fixes containers#11645

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
  • Loading branch information
Luap99 committed Sep 22, 2021
1 parent 8e2d25e commit 214592c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (c *Container) getContainerInspectData(size bool, driverData *define.Driver
// An error here is not considered fatal; no health state will be displayed
logrus.Error(err)
} else {
data.State.Healthcheck = healthCheckState
data.State.Health = healthCheckState
}
}

Expand Down
8 changes: 7 additions & 1 deletion libpod/define/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,16 @@ type InspectContainerState struct {
Error string `json:"Error"` // TODO
StartedAt time.Time `json:"StartedAt"`
FinishedAt time.Time `json:"FinishedAt"`
Healthcheck HealthCheckResults `json:"Healthcheck,omitempty"`
Health HealthCheckResults `json:"Health,omitempty"`
Checkpointed bool `json:"Checkpointed,omitempty"`
}

// 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 {
return s.Health
}

// HealthCheckResults describes the results/logs from a healthcheck
type HealthCheckResults struct {
// Status healthy or unhealthy
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/handlers/compat/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,11 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,

if l.HasHealthCheck() && state.Status != "created" {
state.Health = &types.Health{
Status: inspect.State.Healthcheck.Status,
FailingStreak: inspect.State.Healthcheck.FailingStreak,
Status: inspect.State.Health.Status,
FailingStreak: inspect.State.Health.FailingStreak,
}

log := inspect.State.Healthcheck.Log
log := inspect.State.Health.Log

for _, item := range log {
res := &types.HealthcheckResult{}
Expand Down
21 changes: 12 additions & 9 deletions test/e2e/healthcheck_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("Podman healthcheck run", func() {
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
inspect := podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal("starting"))
Expect(inspect[0].State.Health.Status).To(Equal("starting"))
})

It("podman healthcheck failed checks in start-period should not change status", func() {
Expand All @@ -138,7 +138,9 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(hc).Should(Exit(1))

inspect := podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal("starting"))
Expect(inspect[0].State.Health.Status).To(Equal("starting"))
// test old podman compat (see #11645)
Expect(inspect[0].State.Healthcheck().Status).To(Equal("starting"))
})

It("podman healthcheck failed checks must reach retries before unhealthy ", func() {
Expand All @@ -151,15 +153,16 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(hc).Should(Exit(1))

inspect := podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal("starting"))
Expect(inspect[0].State.Health.Status).To(Equal("starting"))

hc = podmanTest.Podman([]string{"healthcheck", "run", "hc"})
hc.WaitWithDefaultTimeout()
Expect(hc).Should(Exit(1))

inspect = podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal(define.HealthCheckUnhealthy))

Expect(inspect[0].State.Health.Status).To(Equal(define.HealthCheckUnhealthy))
// test old podman compat (see #11645)
Expect(inspect[0].State.Healthcheck().Status).To(Equal(define.HealthCheckUnhealthy))
})

It("podman healthcheck good check results in healthy even in start-period", func() {
Expand All @@ -172,7 +175,7 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(hc).Should(Exit(0))

inspect := podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal(define.HealthCheckHealthy))
Expect(inspect[0].State.Health.Status).To(Equal(define.HealthCheckHealthy))
})

It("podman healthcheck unhealthy but valid arguments check", func() {
Expand All @@ -195,14 +198,14 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(hc).Should(Exit(1))

inspect := podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal("starting"))
Expect(inspect[0].State.Health.Status).To(Equal("starting"))

hc = podmanTest.Podman([]string{"healthcheck", "run", "hc"})
hc.WaitWithDefaultTimeout()
Expect(hc).Should(Exit(1))

inspect = podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal(define.HealthCheckUnhealthy))
Expect(inspect[0].State.Health.Status).To(Equal(define.HealthCheckUnhealthy))

foo := podmanTest.Podman([]string{"exec", "hc", "touch", "/foo"})
foo.WaitWithDefaultTimeout()
Expand All @@ -213,6 +216,6 @@ var _ = Describe("Podman healthcheck run", func() {
Expect(hc).Should(Exit(0))

inspect = podmanTest.InspectContainer("hc")
Expect(inspect[0].State.Healthcheck.Status).To(Equal(define.HealthCheckHealthy))
Expect(inspect[0].State.Health.Status).To(Equal(define.HealthCheckHealthy))
})
})

0 comments on commit 214592c

Please sign in to comment.