Skip to content

Commit

Permalink
Wait for valid container ID
Browse files Browse the repository at this point in the history
  • Loading branch information
devplayer0 committed Jun 10, 2021
1 parent 3da81d3 commit 7981235
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions pkg/plugin/dhcp_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"strings"
"time"

dTypes "github.com/docker/docker/api/types"
Expand Down Expand Up @@ -199,20 +200,27 @@ func (m *dhcpManager) setupClient(v6 bool) (chan error, error) {
}

func (m *dhcpManager) Start(ctx context.Context) error {
dockerNet, err := m.docker.NetworkInspect(ctx, m.joinReq.NetworkID, dTypes.NetworkInspectOptions{})
if err != nil {
return fmt.Errorf("failed to get Docker network info: %w", err)
}

var ctrID string
for id, info := range dockerNet.Containers {
if info.EndpointID == m.joinReq.EndpointID {
ctrID = id
break
if err := util.AwaitCondition(ctx, func() (bool, error) {
dockerNet, err := m.docker.NetworkInspect(ctx, m.joinReq.NetworkID, dTypes.NetworkInspectOptions{})
if err != nil {
return false, fmt.Errorf("failed to get Docker network info: %w", err)
}
}
if ctrID == "" {
return util.ErrNoContainer

for id, info := range dockerNet.Containers {
if info.EndpointID == m.joinReq.EndpointID {
ctrID = id
break
}
}
if ctrID == "" {
return false, util.ErrNoContainer
}

// Seems like Docker makes the container ID just the endpoint until it's ready
return !strings.HasPrefix(ctrID, "ep-"), nil
}, pollTime); err != nil {
return err
}

ctr, err := util.AwaitContainerInspect(ctx, m.docker, ctrID, pollTime)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func AwaitContainerInspect(ctx context.Context, docker *client.Client, id string
return link, nil
case <-ctx.Done():
if err != nil {
log.WithError(err).WithField("id", id).Error("Failed to await container by index")
log.WithError(err).WithField("id", id).Error("Failed to await container by ID")
}
return dummy, ctx.Err()
}
Expand Down

0 comments on commit 7981235

Please sign in to comment.