Skip to content

Commit 5b4ca4d

Browse files
authored
feat: improves healthcheck for swarm nodes (#3422)
1 parent 036bee5 commit 5b4ca4d

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

examples/docker.swarm.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
services:
22
dozzle-service:
3-
image: amir20/dozzle:latest
3+
image: amir20/dozzle:local-test
44
environment:
55
- DOZZLE_LEVEL=debug
66
- DOZZLE_MODE=swarm
7+
healthcheck:
8+
test: ["CMD", "/dozzle", "healthcheck"]
9+
interval: 3s
10+
timeout: 30s
11+
retries: 5
12+
start_period: 30s
713
volumes:
814
- /var/run/docker.sock:/var/run/docker.sock
915
ports:

internal/support/docker/multi_host_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type ClientManager interface {
2626
RetryAndList() ([]ClientService, []error)
2727
Subscribe(ctx context.Context, channel chan<- docker.Host)
2828
Hosts(ctx context.Context) []docker.Host
29+
LocalClients() []docker.Client
2930
}
3031

3132
type MultiHostService struct {
@@ -156,3 +157,7 @@ func (m *MultiHostService) LocalHost() (docker.Host, error) {
156157
func (m *MultiHostService) SubscribeAvailableHosts(ctx context.Context, hosts chan<- docker.Host) {
157158
m.manager.Subscribe(ctx, hosts)
158159
}
160+
161+
func (m *MultiHostService) LocalClients() []docker.Client {
162+
return m.manager.LocalClients()
163+
}

internal/support/docker/retriable_client_manager.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,17 @@ func (m *RetriableClientManager) Hosts(ctx context.Context) []docker.Host {
179179

180180
return hosts
181181
}
182+
183+
func (m *RetriableClientManager) LocalClients() []docker.Client {
184+
services := m.List()
185+
186+
clients := make([]docker.Client, 0)
187+
188+
for _, service := range services {
189+
if clientService, ok := service.(*dockerClientService); ok {
190+
clients = append(clients, clientService.client)
191+
}
192+
}
193+
194+
return clients
195+
}

internal/support/docker/swarm_client_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,7 @@ func (m *SwarmClientManager) Hosts(ctx context.Context) []docker.Host {
229229
func (m *SwarmClientManager) String() string {
230230
return fmt.Sprintf("SwarmClientManager{clients: %d}", len(m.clients))
231231
}
232+
233+
func (m *SwarmClientManager) LocalClients() []docker.Client {
234+
return []docker.Client{m.localClient}
235+
}

internal/web/healthcheck.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ import (
99
func (h *handler) healthcheck(w http.ResponseWriter, r *http.Request) {
1010
log.Debug().Msg("Executing healthcheck")
1111

12-
for _, host := range h.multiHostService.Hosts() {
13-
if host.Type == "agent" {
14-
log.Debug().Str("host", host.ID).Msg("Skipping agent host for healthcheck")
15-
continue
16-
}
17-
18-
_, err := h.multiHostService.ListContainersForHost(host.ID)
19-
if err != nil {
20-
log.Error().Err(err).Str("host", host.ID).Msg("Error listing containers")
21-
http.Error(w, "Error listing containers", http.StatusInternalServerError)
22-
return
12+
clients := h.multiHostService.LocalClients()
13+
for _, client := range clients {
14+
if _, err := client.Ping(r.Context()); err != nil {
15+
log.Error().Err(err).Str("host", client.Host().Name).Msg("error pinging host")
16+
w.WriteHeader(http.StatusInternalServerError)
2317
}
2418
}
2519

0 commit comments

Comments
 (0)