11package builder
22
33import (
4- "context"
54 "fmt"
6- "net"
75 "time"
86
97 "github.com/depot/cli/pkg/api"
108 "github.com/docker/buildx/util/progress"
11- "github.com/moby/buildkit/client"
129 "github.com/pkg/errors"
1310)
1411
@@ -28,11 +25,19 @@ func NewBuilder(depot *api.Depot, buildID, platform string) *Builder {
2825 }
2926}
3027
31- func (b * Builder ) Acquire (l progress.Logger ) (string , error ) {
32- var addr string
28+ type AcquiredBuilder struct {
29+ Version string
30+ Addr string
31+ AccessToken string
32+ CACert string
33+ Cert string
34+ Key string
35+ }
36+
37+ func (b * Builder ) Acquire (l progress.Logger ) (* AcquiredBuilder , error ) {
3338 var resp * api.BuilderResponse
3439 var err error
35- var accessToken string
40+ var builder AcquiredBuilder
3641
3742 acquireFn := func (sub progress.SubLogger ) error {
3843 resp , err = b .depot .GetBuilder (b .BuildID , b .Platform )
@@ -41,7 +46,11 @@ func (b *Builder) Acquire(l progress.Logger) (string, error) {
4146 }
4247
4348 if resp .OK {
44- accessToken = resp .AccessToken
49+ builder .Version = resp .Version
50+ builder .AccessToken = resp .AccessToken
51+ builder .CACert = resp .CACert
52+ builder .Cert = resp .Cert
53+ builder .Key = resp .Key
4554 }
4655
4756 // Loop if the builder is not ready
@@ -79,64 +88,31 @@ func (b *Builder) Acquire(l progress.Logger) (string, error) {
7988 if err != nil {
8089 err = progress .Wrap ("[depot] launching " + b .Platform + " builder" , l , acquireFn )
8190 if err != nil {
82- return "" , err
91+ return nil , err
8392 }
8493 }
8594
86- err = progress .Wrap ("[depot] connecting to " + b .Platform + " builder" , l , func (sub progress.SubLogger ) error {
87- proxy , err := newProxyServer (resp .Endpoint , accessToken )
88- if err != nil {
89- return errors .Wrap (err , "failed to construct proxy server" )
90- }
91-
92- b .proxy = proxy
93- proxy .Start ()
94- addr = proxy .Addr ().String ()
95-
96- sub .Log (2 , []byte ("Waiting for builder to report ready...\n " ))
97-
98- count := 0
99-
100- for {
101- if count > 30 {
102- return fmt .Errorf ("timed out waiting for builder to be ready" )
103- }
104-
105- if count > 0 && count % 10 == 0 {
106- sub .Log (2 , []byte ("Still waiting for builder to report ready...\n " ))
107- }
108-
109- if count > 0 {
110- time .Sleep (time .Second )
111- }
112-
113- count ++
95+ if builder .Version == "2" {
96+ builder .Addr = resp .Endpoint
97+ return & builder , nil
98+ }
11499
115- conn , err := net . Dial ( "tcp" , proxy . Addr (). String () )
116- if err != nil {
117- continue
118- }
100+ proxy , err := newProxyServer ( resp . Endpoint , builder . AccessToken )
101+ if err != nil {
102+ return nil , errors . Wrap ( err , "failed to construct proxy server" )
103+ }
119104
120- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
121- defer cancel ()
122- testClient , err := client .New (ctx , "" , client .WithContextDialer (func (context.Context , string ) (net.Conn , error ) {
123- return conn , nil
124- }))
125- if err != nil {
126- continue
127- }
105+ b .proxy = proxy
106+ proxy .Start ()
107+ builder .Addr = fmt .Sprintf ("tcp://%s" , proxy .Addr ().String ())
128108
129- ctx2 , cancel2 := context .WithTimeout (context .Background (), 5 * time .Second )
130- defer cancel2 ()
131- workers , err := testClient .ListWorkers (ctx2 )
132- if err != nil {
133- continue
134- }
109+ return & builder , err
110+ }
135111
136- if len ( workers ) > 0 {
137- return nil
138- }
139- }
140- })
141- return addr , err
112+ func ( b * Builder ) ReportHealth ( status string ) error {
113+ _ , err := b . depot . ReportBuilderHealth ( b . BuildID , b . Platform , status )
114+ if err != nil {
115+ return err
116+ }
117+ return nil
142118}
0 commit comments