Skip to content

Commit

Permalink
fix: cannot bootstrap buildkitd if the container does not exist (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege authored Apr 29, 2022
1 parent 303e244 commit 3d3677e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
6 changes: 1 addition & 5 deletions cmd/midi/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ func bootstrap(clicontext *cli.Context) error {
return errors.Wrap(err, "failed to create buildkit client")
}
defer bkClient.Close()
addr, err := bkClient.Bootstrap(clicontext.Context)
if err != nil {
return errors.Wrap(err, "failed to bootstrap buildkit")
}
logrus.Infof("The buildkit is running at %s", addr)
logrus.Infof("The buildkit is running at %s", bkClient.BuildkitdAddr())
}
return nil
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/buildkitd/buildkitd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
// Client is a client for the buildkitd daemon.
// It's up to the caller to close the client.
type Client interface {
BuildkitdAddr() string
Bootstrap(ctx context.Context) (string, error)
// Solve calls Solve on the controller.
Solve(ctx context.Context, def *llb.Definition, opt client.SolveOpt, statusChan chan *client.SolveStatus) (*client.SolveResponse, error)
Expand All @@ -46,18 +47,14 @@ func NewClient(ctx context.Context) (Client, error) {
"image": c.image,
})

cli, err := client.New(ctx, c.buildkitdAddr(), client.WithFailFast())
cli, err := client.New(ctx, c.BuildkitdAddr(), client.WithFailFast())
if err != nil {
return nil, errors.Wrap(err, "failed to create the buildkit client")
return nil, errors.Wrap(err, "failed to create the client")
}
c.Client = cli

connected, err := c.connected(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to buildkitd")
}
if !connected {
return nil, errors.New("buildkitd is not running")
if _, err := c.Bootstrap(ctx); err != nil {
return nil, errors.Wrap(err, "failed to bootstrap the buildkitd")
}
return c, nil
}
Expand Down Expand Up @@ -97,13 +94,18 @@ func (c *generalClient) maybeStart(ctx context.Context,
return "", err
}
}

c.logger.Debug("container is running, check if it's ready...")
cli, err := client.New(ctx, c.BuildkitdAddr(), client.WithFailFast())
if err != nil {
return "", errors.Wrap(err, "failed to create the buildkit client")
}
c.Client = cli

if err := c.waitUntilConnected(ctx, runningTimeout); err != nil {
return "", errors.Wrap(err, "failed to connect to buildkitd")
}

return c.buildkitdAddr(), nil
return c.BuildkitdAddr(), nil
}

func (c generalClient) waitUntilConnected(
Expand Down Expand Up @@ -142,6 +144,6 @@ func (c generalClient) connected(ctx context.Context) (bool, error) {
return true, nil
}

func (c generalClient) buildkitdAddr() string {
func (c generalClient) BuildkitdAddr() string {
return fmt.Sprintf("docker-container://%s", c.containerName)
}
14 changes: 14 additions & 0 deletions pkg/buildkitd/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d3677e

Please sign in to comment.