@@ -6,11 +6,12 @@ import (
66 "strings"
77 "sync"
88
9- "github.com/docker/buildx/driver"
10- "github.com/docker/buildx/util/progress"
119 "github.com/moby/buildkit/client"
1210 dockerclient "github.com/moby/moby/client"
1311 "github.com/pkg/errors"
12+
13+ "github.com/docker/buildx/driver"
14+ "github.com/docker/buildx/util/progress"
1415)
1516
1617type Driver struct {
@@ -64,14 +65,39 @@ func (d *Driver) Dial(ctx context.Context) (net.Conn, error) {
6465}
6566
6667func (d * Driver ) Client (ctx context.Context , opts ... client.ClientOpt ) (* client.Client , error ) {
67- opts = append ([]client.ClientOpt {
68- client .WithContextDialer (func (context.Context , string ) (net.Conn , error ) {
69- return d .Dial (ctx )
70- }), client .WithSessionDialer (func (ctx context.Context , proto string , meta map [string ][]string ) (net.Conn , error ) {
71- return d .DockerAPI .DialHijack (ctx , "/session" , proto , meta )
72- }),
73- }, opts ... )
74- return client .New (ctx , "" , opts ... )
68+ c , _ , err := d .client (ctx , opts ... )
69+ return c , err
70+ }
71+
72+ func (d * Driver ) client (ctx context.Context , opts ... client.ClientOpt ) (* client.Client , []* client.WorkerInfo , error ) {
73+ var (
74+ c , err = client .New (ctx , d .DockerAPI .DaemonHost (), opts ... )
75+ workers []* client.WorkerInfo
76+ )
77+ if err == nil {
78+ workers , err = c .ListWorkers (ctx )
79+ if err != nil {
80+ c .Close ()
81+ }
82+ }
83+ if err != nil {
84+ opts = append ([]client.ClientOpt {
85+ client .WithContextDialer (func (context.Context , string ) (net.Conn , error ) {
86+ return d .Dial (ctx )
87+ }), client .WithSessionDialer (func (ctx context.Context , proto string , meta map [string ][]string ) (net.Conn , error ) {
88+ return d .DockerAPI .DialHijack (ctx , "/session" , proto , meta )
89+ }),
90+ }, opts ... )
91+ c , err = client .New (ctx , "" , opts ... )
92+ if err == nil {
93+ workers , err = c .ListWorkers (ctx )
94+ if err != nil {
95+ c .Close ()
96+ }
97+ }
98+ }
99+
100+ return c , workers , err
75101}
76102
77103type features struct {
@@ -82,15 +108,15 @@ type features struct {
82108func (d * Driver ) Features (ctx context.Context ) map [driver.Feature ]bool {
83109 d .features .once .Do (func () {
84110 var useContainerdSnapshotter bool
85- if c , err := d .Client (ctx ); err == nil {
86- workers , _ := c .ListWorkers (ctx )
87- for _ , w := range workers {
88- if _ , ok := w .Labels ["org.mobyproject.buildkit.worker.snapshotter" ]; ok {
89- useContainerdSnapshotter = true
90- }
91- }
111+ c , workers , err := d .client (ctx )
112+ if err != nil {
92113 c .Close ()
93114 }
115+ for _ , w := range workers {
116+ if _ , ok := w .Labels ["org.mobyproject.buildkit.worker.snapshotter" ]; ok {
117+ useContainerdSnapshotter = true
118+ }
119+ }
94120 d .features .list = map [driver.Feature ]bool {
95121 driver .OCIExporter : useContainerdSnapshotter ,
96122 driver .DockerExporter : useContainerdSnapshotter ,
0 commit comments