buildkit: wait for buildkitd to be ready before connecting

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-17 14:54:51 -07:00
parent c42ccd4385
commit d7e9c91b01

View File

@ -2,11 +2,15 @@ package buildkitd
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os/exec" "os/exec"
"strings" "strings"
"time"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
bk "github.com/moby/buildkit/client"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -120,8 +124,32 @@ func startBuildkit(ctx context.Context) error {
Msg("unable to start buildkitd") Msg("unable to start buildkitd")
return err return err
} }
return waitBuildkit(ctx)
}
// waitBuildkit waits for the buildkit daemon to be responsive.
func waitBuildkit(ctx context.Context) error {
c, err := bk.New(ctx, "docker-container://"+containerName)
if err != nil {
return err
}
defer c.Close()
// Try to connect every 100ms up to 50 times (5 seconds total)
const (
retryPeriod = 100 * time.Millisecond
retryAttempts = 50
)
for retry := 0; retry < retryAttempts; retry++ {
_, err = c.ListWorkers(ctx)
if err == nil {
return nil return nil
} }
time.Sleep(retryPeriod)
}
return errors.New("buildkit failed to respond")
}
func remvoveBuildkit(ctx context.Context) error { func remvoveBuildkit(ctx context.Context) error {
lg := log. lg := log.