Retry fetching Docker container info

This commit is contained in:
Jack O'Sullivan 2021-06-10 23:22:33 +01:00
parent e109e9923e
commit 3da81d344d
2 changed files with 38 additions and 1 deletions

View File

@ -215,7 +215,7 @@ func (m *dhcpManager) Start(ctx context.Context) error {
return util.ErrNoContainer
}
ctr, err := m.docker.ContainerInspect(ctx, ctrID)
ctr, err := util.AwaitContainerInspect(ctx, m.docker, ctrID, pollTime)
if err != nil {
return fmt.Errorf("failed to get Docker container info: %w", err)
}

View File

@ -1,5 +1,42 @@
package util
import (
"context"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)
const (
OptionsKeyGeneric = "com.docker.network.generic"
)
func AwaitContainerInspect(ctx context.Context, docker *client.Client, id string, interval time.Duration) (types.ContainerJSON, error) {
var err error
ctrChan := make(chan types.ContainerJSON)
go func() {
for {
var ctr types.ContainerJSON
ctr, err = docker.ContainerInspect(ctx, id)
if err == nil {
ctrChan <- ctr
return
}
time.Sleep(interval)
}
}()
var dummy types.ContainerJSON
select {
case link := <-ctrChan:
return link, nil
case <-ctx.Done():
if err != nil {
log.WithError(err).WithField("id", id).Error("Failed to await container by index")
}
return dummy, ctx.Err()
}
}