Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
fix(docker): ensure connection is established with host
Browse files Browse the repository at this point in the history
The `docker` and `docker_swarm` providers have been patched to ensure that the connection is properly established upon starting.

If the docker host is not available at starting time, then the application will stop. This will prevent from trying to register to the event stream on a non working client.
  • Loading branch information
acouvreur committed Oct 2, 2024
1 parent 372fa39 commit 7ddb6ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
23 changes: 19 additions & 4 deletions app/providers/docker_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ type DockerClassicProvider struct {
func NewDockerClassicProvider() (*DockerClassicProvider, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatal(fmt.Errorf("%+v", "Could not connect to docker API"))
return nil, err
return nil, fmt.Errorf("cannot create docker client: %v", err)
}

serverVersion, err := cli.ServerVersion(context.Background())
if err != nil {
return nil, fmt.Errorf("cannot connect to docker host: %v", err)
}

log.Trace(fmt.Sprintf("connection established with docker %s (API %s)", serverVersion.Version, serverVersion.APIVersion))

return &DockerClassicProvider{
Client: cli,
desiredReplicas: 1,
Expand Down Expand Up @@ -144,14 +150,23 @@ func (provider *DockerClassicProvider) NotifyInstanceStopped(ctx context.Context
})
for {
select {
case msg := <-msgs:
case msg, ok := <-msgs:
if !ok {
log.Error("provider event stream is closed")
return
}
// Send the container that has died to the channel
instance <- strings.TrimPrefix(msg.Actor.Attributes["name"], "/")
case err := <-errs:
case err, ok := <-errs:
if !ok {
log.Error("provider event stream is closed", err)
return
}
if errors.Is(err, io.EOF) {
log.Debug("provider event stream closed")
return
}
log.Error("provider event stream error", err)
case <-ctx.Done():
return
}
Expand Down
23 changes: 20 additions & 3 deletions app/providers/docker_swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ type DockerSwarmProvider struct {
func NewDockerSwarmProvider() (*DockerSwarmProvider, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot create docker client: %v", err)
}

serverVersion, err := cli.ServerVersion(context.Background())
if err != nil {
return nil, fmt.Errorf("cannot connect to docker host: %v", err)
}

log.Trace(fmt.Sprintf("connection established with docker %s (API %s)", serverVersion.Version, serverVersion.APIVersion))

return &DockerSwarmProvider{
Client: cli,
desiredReplicas: 1,
Expand Down Expand Up @@ -161,17 +169,26 @@ func (provider *DockerSwarmProvider) NotifyInstanceStopped(ctx context.Context,
go func() {
for {
select {
case msg := <-msgs:
case msg, ok := <-msgs:
if !ok {
log.Error("provider event stream is closed")
return
}
if msg.Actor.Attributes["replicas.new"] == "0" {
instance <- msg.Actor.Attributes["name"]
} else if msg.Action == "remove" {
instance <- msg.Actor.Attributes["name"]
}
case err := <-errs:
case err, ok := <-errs:
if !ok {
log.Error("provider event stream is closed", err)
return
}
if errors.Is(err, io.EOF) {
log.Debug("provider event stream closed")
return
}
log.Error("provider event stream error", err)
case <-ctx.Done():
return
}
Expand Down

0 comments on commit 7ddb6ae

Please sign in to comment.