explicitly access Container.State.Health.Health

The State.Health struct has a mutex, but in various places
we access the embedded Health struct directly.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-18 23:26:53 +02:00
committed by Rob Murray
parent 0df791cb72
commit d06f0d008d
4 changed files with 18 additions and 18 deletions

View File

@@ -296,7 +296,7 @@ func (v *View) transform(ctr *Container) *Snapshot {
failingStreak := 0 failingStreak := 0
if ctr.State.Health != nil { if ctr.State.Health != nil {
health = ctr.State.Health.Status() health = ctr.State.Health.Status()
failingStreak = ctr.State.Health.FailingStreak failingStreak = ctr.State.Health.Health.FailingStreak
} }
snapshot := &Snapshot{ snapshot := &Snapshot{

View File

@@ -197,14 +197,14 @@ func handleProbeResult(d *Daemon, c *container.Container, result *containertypes
h := c.State.Health h := c.State.Health
oldStatus := h.Status() oldStatus := h.Status()
if len(h.Log) >= maxLogEntries { if len(h.Health.Log) >= maxLogEntries {
h.Log = append(h.Log[len(h.Log)+1-maxLogEntries:], result) h.Health.Log = append(h.Health.Log[len(h.Health.Log)+1-maxLogEntries:], result)
} else { } else {
h.Log = append(h.Log, result) h.Health.Log = append(h.Health.Log, result)
} }
if result.ExitCode == exitStatusHealthy { if result.ExitCode == exitStatusHealthy {
h.FailingStreak = 0 h.Health.FailingStreak = 0
h.SetStatus(containertypes.Healthy) h.SetStatus(containertypes.Healthy)
} else { // Failure (including invalid exit code) } else { // Failure (including invalid exit code)
shouldIncrementStreak := true shouldIncrementStreak := true
@@ -223,9 +223,9 @@ func handleProbeResult(d *Daemon, c *container.Container, result *containertypes
} }
if shouldIncrementStreak { if shouldIncrementStreak {
h.FailingStreak++ h.Health.FailingStreak++
if h.FailingStreak >= retries { if h.Health.FailingStreak >= retries {
h.SetStatus(containertypes.Unhealthy) h.SetStatus(containertypes.Unhealthy)
} }
} }
@@ -377,7 +377,7 @@ func (daemon *Daemon) initHealthMonitor(c *container.Container) {
if h := c.State.Health; h != nil { if h := c.State.Health; h != nil {
h.SetStatus(containertypes.Starting) h.SetStatus(containertypes.Starting)
h.FailingStreak = 0 h.Health.FailingStreak = 0
} else { } else {
h := &container.Health{} h := &container.Health{}
h.SetStatus(containertypes.Starting) h.SetStatus(containertypes.Starting)

View File

@@ -114,8 +114,8 @@ func TestHealthStates(t *testing.T) {
if status := c.State.Health.Status(); status != containertypes.Starting { if status := c.State.Health.Status(); status != containertypes.Starting {
t.Errorf("Expecting starting, but got %#v\n", status) t.Errorf("Expecting starting, but got %#v\n", status)
} }
if c.State.Health.FailingStreak != 2 { if c.State.Health.Health.FailingStreak != 2 {
t.Errorf("Expecting FailingStreak=2, but got %d\n", c.State.Health.FailingStreak) t.Errorf("Expecting FailingStreak=2, but got %d\n", c.State.Health.Health.FailingStreak)
} }
handleResult(c.State.StartedAt.Add(60*time.Second), 1) handleResult(c.State.StartedAt.Add(60*time.Second), 1)
expect(eventtypes.ActionHealthStatusUnhealthy) expect(eventtypes.ActionHealthStatusUnhealthy)
@@ -123,7 +123,7 @@ func TestHealthStates(t *testing.T) {
handleResult(c.State.StartedAt.Add(80*time.Second), 0) handleResult(c.State.StartedAt.Add(80*time.Second), 0)
expect(eventtypes.ActionHealthStatusHealthy) expect(eventtypes.ActionHealthStatusHealthy)
if c.State.Health.FailingStreak != 0 { if c.State.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.Health.FailingStreak)
} }
// Test start period // Test start period
@@ -136,19 +136,19 @@ func TestHealthStates(t *testing.T) {
if status := c.State.Health.Status(); status != containertypes.Starting { if status := c.State.Health.Status(); status != containertypes.Starting {
t.Errorf("Expecting starting, but got %#v\n", status) t.Errorf("Expecting starting, but got %#v\n", status)
} }
if c.State.Health.FailingStreak != 0 { if c.State.Health.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
} }
handleResult(c.State.StartedAt.Add(50*time.Second), 1) handleResult(c.State.StartedAt.Add(50*time.Second), 1)
if status := c.State.Health.Status(); status != containertypes.Starting { if status := c.State.Health.Status(); status != containertypes.Starting {
t.Errorf("Expecting starting, but got %#v\n", status) t.Errorf("Expecting starting, but got %#v\n", status)
} }
if c.State.Health.FailingStreak != 1 { if c.State.Health.Health.FailingStreak != 1 {
t.Errorf("Expecting FailingStreak=1, but got %d\n", c.State.Health.FailingStreak) t.Errorf("Expecting FailingStreak=1, but got %d\n", c.State.Health.Health.FailingStreak)
} }
handleResult(c.State.StartedAt.Add(80*time.Second), 0) handleResult(c.State.StartedAt.Add(80*time.Second), 0)
expect(eventtypes.ActionHealthStatusHealthy) expect(eventtypes.ActionHealthStatusHealthy)
if c.State.Health.FailingStreak != 0 { if c.State.Health.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.Health.FailingStreak)
} }
} }

View File

@@ -113,8 +113,8 @@ func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Co
if ctr.State.Health != nil { if ctr.State.Health != nil {
containerHealth = &containertypes.Health{ containerHealth = &containertypes.Health{
Status: ctr.State.Health.Status(), Status: ctr.State.Health.Status(),
FailingStreak: ctr.State.Health.FailingStreak, FailingStreak: ctr.State.Health.Health.FailingStreak,
Log: append([]*containertypes.HealthcheckResult{}, ctr.State.Health.Log...), Log: append([]*containertypes.HealthcheckResult{}, ctr.State.Health.Health.Log...),
} }
} }