Skip to content

Commit

Permalink
feat: improves healthcheck for swarm nodes (#3422)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 authored Nov 25, 2024
1 parent 036bee5 commit 5b4ca4d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
8 changes: 7 additions & 1 deletion examples/docker.swarm.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
services:
dozzle-service:
image: amir20/dozzle:latest
image: amir20/dozzle:local-test
environment:
- DOZZLE_LEVEL=debug
- DOZZLE_MODE=swarm
healthcheck:
test: ["CMD", "/dozzle", "healthcheck"]
interval: 3s
timeout: 30s
retries: 5
start_period: 30s
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
Expand Down
5 changes: 5 additions & 0 deletions internal/support/docker/multi_host_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ClientManager interface {
RetryAndList() ([]ClientService, []error)
Subscribe(ctx context.Context, channel chan<- docker.Host)
Hosts(ctx context.Context) []docker.Host
LocalClients() []docker.Client
}

type MultiHostService struct {
Expand Down Expand Up @@ -156,3 +157,7 @@ func (m *MultiHostService) LocalHost() (docker.Host, error) {
func (m *MultiHostService) SubscribeAvailableHosts(ctx context.Context, hosts chan<- docker.Host) {
m.manager.Subscribe(ctx, hosts)
}

func (m *MultiHostService) LocalClients() []docker.Client {
return m.manager.LocalClients()
}
14 changes: 14 additions & 0 deletions internal/support/docker/retriable_client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,17 @@ func (m *RetriableClientManager) Hosts(ctx context.Context) []docker.Host {

return hosts
}

func (m *RetriableClientManager) LocalClients() []docker.Client {
services := m.List()

clients := make([]docker.Client, 0)

for _, service := range services {
if clientService, ok := service.(*dockerClientService); ok {
clients = append(clients, clientService.client)
}
}

return clients
}
4 changes: 4 additions & 0 deletions internal/support/docker/swarm_client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,7 @@ func (m *SwarmClientManager) Hosts(ctx context.Context) []docker.Host {
func (m *SwarmClientManager) String() string {
return fmt.Sprintf("SwarmClientManager{clients: %d}", len(m.clients))
}

func (m *SwarmClientManager) LocalClients() []docker.Client {
return []docker.Client{m.localClient}
}
16 changes: 5 additions & 11 deletions internal/web/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import (
func (h *handler) healthcheck(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("Executing healthcheck")

for _, host := range h.multiHostService.Hosts() {
if host.Type == "agent" {
log.Debug().Str("host", host.ID).Msg("Skipping agent host for healthcheck")
continue
}

_, err := h.multiHostService.ListContainersForHost(host.ID)
if err != nil {
log.Error().Err(err).Str("host", host.ID).Msg("Error listing containers")
http.Error(w, "Error listing containers", http.StatusInternalServerError)
return
clients := h.multiHostService.LocalClients()
for _, client := range clients {
if _, err := client.Ping(r.Context()); err != nil {
log.Error().Err(err).Str("host", client.Host().Name).Msg("error pinging host")
w.WriteHeader(http.StatusInternalServerError)
}
}

Expand Down

0 comments on commit 5b4ca4d

Please sign in to comment.