@@ -3,20 +3,14 @@ package build
3
3
import (
4
4
"context"
5
5
_ "crypto/sha256" // ensure digests can be computed
6
- "encoding/json"
7
- "io"
8
6
"sync"
9
7
10
- controllerapi "github.com/docker/buildx/controller/pb"
11
8
"github.com/moby/buildkit/client"
12
- "github.com/moby/buildkit/exporter/containerimage/exptypes"
13
9
gateway "github.com/moby/buildkit/frontend/gateway/client"
14
10
"github.com/moby/buildkit/solver/errdefs"
15
11
"github.com/moby/buildkit/solver/pb"
16
12
"github.com/moby/buildkit/solver/result"
17
- specs "github.com/opencontainers/image-spec/specs-go/v1"
18
13
"github.com/pkg/errors"
19
- "github.com/sirupsen/logrus"
20
14
"golang.org/x/sync/errgroup"
21
15
)
22
16
@@ -280,216 +274,3 @@ func (r *ResultHandle) Done() {
280
274
<- r .gwCtx .Done ()
281
275
})
282
276
}
283
-
284
- func (r * ResultHandle ) registerCleanup (f func ()) {
285
- r .cleanupsMu .Lock ()
286
- r .cleanups = append (r .cleanups , f )
287
- r .cleanupsMu .Unlock ()
288
- }
289
-
290
- func (r * ResultHandle ) build (buildFunc gateway.BuildFunc ) (err error ) {
291
- _ , err = buildFunc (r .gwCtx , r .gwClient )
292
- return err
293
- }
294
-
295
- func (r * ResultHandle ) getContainerConfig (cfg * controllerapi.InvokeConfig ) (containerCfg gateway.NewContainerRequest , _ error ) {
296
- if r .res != nil && r .solveErr == nil {
297
- logrus .Debugf ("creating container from successful build" )
298
- ccfg , err := containerConfigFromResult (r .res , cfg )
299
- if err != nil {
300
- return containerCfg , err
301
- }
302
- containerCfg = * ccfg
303
- } else {
304
- logrus .Debugf ("creating container from failed build %+v" , cfg )
305
- ccfg , err := containerConfigFromError (r .solveErr , cfg )
306
- if err != nil {
307
- return containerCfg , errors .Wrapf (err , "no result nor error is available" )
308
- }
309
- containerCfg = * ccfg
310
- }
311
- return containerCfg , nil
312
- }
313
-
314
- func (r * ResultHandle ) getProcessConfig (cfg * controllerapi.InvokeConfig , stdin io.ReadCloser , stdout io.WriteCloser , stderr io.WriteCloser ) (_ gateway.StartRequest , err error ) {
315
- processCfg := newStartRequest (stdin , stdout , stderr )
316
- if r .res != nil && r .solveErr == nil {
317
- logrus .Debugf ("creating container from successful build" )
318
- if err := populateProcessConfigFromResult (& processCfg , r .res , cfg ); err != nil {
319
- return processCfg , err
320
- }
321
- } else {
322
- logrus .Debugf ("creating container from failed build %+v" , cfg )
323
- if err := populateProcessConfigFromError (& processCfg , r .solveErr , cfg ); err != nil {
324
- return processCfg , err
325
- }
326
- }
327
- return processCfg , nil
328
- }
329
-
330
- func containerConfigFromResult (res * gateway.Result , cfg * controllerapi.InvokeConfig ) (* gateway.NewContainerRequest , error ) {
331
- if cfg .Initial {
332
- return nil , errors .Errorf ("starting from the container from the initial state of the step is supported only on the failed steps" )
333
- }
334
-
335
- ps , err := exptypes .ParsePlatforms (res .Metadata )
336
- if err != nil {
337
- return nil , err
338
- }
339
- ref , ok := res .FindRef (ps .Platforms [0 ].ID )
340
- if ! ok {
341
- return nil , errors .Errorf ("no reference found" )
342
- }
343
-
344
- return & gateway.NewContainerRequest {
345
- Mounts : []gateway.Mount {
346
- {
347
- Dest : "/" ,
348
- MountType : pb .MountType_BIND ,
349
- Ref : ref ,
350
- },
351
- },
352
- }, nil
353
- }
354
-
355
- func populateProcessConfigFromResult (req * gateway.StartRequest , res * gateway.Result , cfg * controllerapi.InvokeConfig ) error {
356
- imgData := res .Metadata [exptypes .ExporterImageConfigKey ]
357
- var img * specs.Image
358
- if len (imgData ) > 0 {
359
- img = & specs.Image {}
360
- if err := json .Unmarshal (imgData , img ); err != nil {
361
- return err
362
- }
363
- }
364
-
365
- user := ""
366
- if ! cfg .NoUser {
367
- user = cfg .User
368
- } else if img != nil {
369
- user = img .Config .User
370
- }
371
-
372
- cwd := ""
373
- if ! cfg .NoCwd {
374
- cwd = cfg .Cwd
375
- } else if img != nil {
376
- cwd = img .Config .WorkingDir
377
- }
378
-
379
- env := []string {}
380
- if img != nil {
381
- env = append (env , img .Config .Env ... )
382
- }
383
- env = append (env , cfg .Env ... )
384
-
385
- args := []string {}
386
- if cfg .Entrypoint != nil {
387
- args = append (args , cfg .Entrypoint ... )
388
- } else if img != nil {
389
- args = append (args , img .Config .Entrypoint ... )
390
- }
391
- if ! cfg .NoCmd {
392
- args = append (args , cfg .Cmd ... )
393
- } else if img != nil {
394
- args = append (args , img .Config .Cmd ... )
395
- }
396
-
397
- req .Args = args
398
- req .Env = env
399
- req .User = user
400
- req .Cwd = cwd
401
- req .Tty = cfg .Tty
402
-
403
- return nil
404
- }
405
-
406
- func containerConfigFromError (solveErr * errdefs.SolveError , cfg * controllerapi.InvokeConfig ) (* gateway.NewContainerRequest , error ) {
407
- exec , err := execOpFromError (solveErr )
408
- if err != nil {
409
- return nil , err
410
- }
411
- var mounts []gateway.Mount
412
- for i , mnt := range exec .Mounts {
413
- rid := solveErr .Solve .MountIDs [i ]
414
- if cfg .Initial {
415
- rid = solveErr .Solve .InputIDs [i ]
416
- }
417
- mounts = append (mounts , gateway.Mount {
418
- Selector : mnt .Selector ,
419
- Dest : mnt .Dest ,
420
- ResultID : rid ,
421
- Readonly : mnt .Readonly ,
422
- MountType : mnt .MountType ,
423
- CacheOpt : mnt .CacheOpt ,
424
- SecretOpt : mnt .SecretOpt ,
425
- SSHOpt : mnt .SSHOpt ,
426
- })
427
- }
428
- return & gateway.NewContainerRequest {
429
- Mounts : mounts ,
430
- NetMode : exec .Network ,
431
- }, nil
432
- }
433
-
434
- func populateProcessConfigFromError (req * gateway.StartRequest , solveErr * errdefs.SolveError , cfg * controllerapi.InvokeConfig ) error {
435
- exec , err := execOpFromError (solveErr )
436
- if err != nil {
437
- return err
438
- }
439
- meta := exec .Meta
440
- user := ""
441
- if ! cfg .NoUser {
442
- user = cfg .User
443
- } else {
444
- user = meta .User
445
- }
446
-
447
- cwd := ""
448
- if ! cfg .NoCwd {
449
- cwd = cfg .Cwd
450
- } else {
451
- cwd = meta .Cwd
452
- }
453
-
454
- env := append (meta .Env , cfg .Env ... )
455
-
456
- args := []string {}
457
- if cfg .Entrypoint != nil {
458
- args = append (args , cfg .Entrypoint ... )
459
- }
460
- if cfg .Cmd != nil {
461
- args = append (args , cfg .Cmd ... )
462
- }
463
- if len (args ) == 0 {
464
- args = meta .Args
465
- }
466
-
467
- req .Args = args
468
- req .Env = env
469
- req .User = user
470
- req .Cwd = cwd
471
- req .Tty = cfg .Tty
472
-
473
- return nil
474
- }
475
-
476
- func execOpFromError (solveErr * errdefs.SolveError ) (* pb.ExecOp , error ) {
477
- if solveErr == nil {
478
- return nil , errors .Errorf ("no error is available" )
479
- }
480
- switch op := solveErr .Solve .Op .GetOp ().(type ) {
481
- case * pb.Op_Exec :
482
- return op .Exec , nil
483
- default :
484
- return nil , errors .Errorf ("invoke: unsupported error type" )
485
- }
486
- // TODO: support other ops
487
- }
488
-
489
- func newStartRequest (stdin io.ReadCloser , stdout io.WriteCloser , stderr io.WriteCloser ) gateway.StartRequest {
490
- return gateway.StartRequest {
491
- Stdin : stdin ,
492
- Stdout : stdout ,
493
- Stderr : stderr ,
494
- }
495
- }
0 commit comments