From 7981235453e9129d935b5f08b225d4b6963ded76 Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Thu, 10 Jun 2021 23:39:31 +0100 Subject: [PATCH] Wait for valid container ID --- pkg/plugin/dhcp_manager.go | 32 ++++++++++++++++++++------------ pkg/util/docker.go | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/plugin/dhcp_manager.go b/pkg/plugin/dhcp_manager.go index c4a26fd..fe29daf 100644 --- a/pkg/plugin/dhcp_manager.go +++ b/pkg/plugin/dhcp_manager.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net" + "strings" "time" dTypes "github.com/docker/docker/api/types" @@ -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) diff --git a/pkg/util/docker.go b/pkg/util/docker.go index 196a547..dcc58ec 100644 --- a/pkg/util/docker.go +++ b/pkg/util/docker.go @@ -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() }