Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions driver/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strings"
"sync"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
dockerclient "github.com/moby/moby/client"
"github.com/pkg/errors"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
)

type Driver struct {
Expand Down Expand Up @@ -64,14 +65,39 @@ func (d *Driver) Dial(ctx context.Context) (net.Conn, error) {
}

func (d *Driver) Client(ctx context.Context, opts ...client.ClientOpt) (*client.Client, error) {
opts = append([]client.ClientOpt{
client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return d.Dial(ctx)
}), client.WithSessionDialer(func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return d.DockerAPI.DialHijack(ctx, "/session", proto, meta)
}),
}, opts...)
return client.New(ctx, "", opts...)
c, _, err := d.client(ctx, opts...)
return c, err
}

func (d *Driver) client(ctx context.Context, opts ...client.ClientOpt) (*client.Client, []*client.WorkerInfo, error) {
var (
c, err = client.New(ctx, d.DockerAPI.DaemonHost(), opts...)
workers []*client.WorkerInfo
)
if err == nil {
workers, err = c.ListWorkers(ctx)
if err != nil {
c.Close()
}
}
if err != nil {
Comment on lines +74 to +83
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by review:

Is this not missing a return after c.Close()? Here the second if err != nil { can happen after ListWorkers and it does not look like that is the actual intent.

opts = append([]client.ClientOpt{
client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return d.Dial(ctx)
Comment on lines +83 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some comment to describe that this is the fallback for the legacy approach @akerouanton (Which could include a link to the moby PR), so that we know when we could potentially remove the fallback?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that'd make sense.

}), client.WithSessionDialer(func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return d.DockerAPI.DialHijack(ctx, "/session", proto, meta)
}),
}, opts...)
c, err = client.New(ctx, "", opts...)
if err == nil {
workers, err = c.ListWorkers(ctx)
if err != nil {
c.Close()
}
}
}

return c, workers, err
}

type features struct {
Expand All @@ -82,15 +108,15 @@ type features struct {
func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
d.features.once.Do(func() {
var useContainerdSnapshotter bool
if c, err := d.Client(ctx); err == nil {
workers, _ := c.ListWorkers(ctx)
for _, w := range workers {
if _, ok := w.Labels["org.mobyproject.buildkit.worker.snapshotter"]; ok {
useContainerdSnapshotter = true
}
}
c, workers, err := d.client(ctx)
if err != nil {
c.Close()
}
for _, w := range workers {
if _, ok := w.Labels["org.mobyproject.buildkit.worker.snapshotter"]; ok {
useContainerdSnapshotter = true
}
}
d.features.list = map[driver.Feature]bool{
driver.OCIExporter: useContainerdSnapshotter,
driver.DockerExporter: useContainerdSnapshotter,
Expand Down