gateway.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package gateway
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "github.com/moby/buildkit/client/buildid"
  7. "github.com/moby/buildkit/frontend/gateway"
  8. gwapi "github.com/moby/buildkit/frontend/gateway/pb"
  9. "github.com/pkg/errors"
  10. "google.golang.org/grpc"
  11. )
  12. type GatewayForwarder struct {
  13. mu sync.RWMutex
  14. updateCond *sync.Cond
  15. builds map[string]gateway.LLBBridgeForwarder
  16. }
  17. func NewGatewayForwarder() *GatewayForwarder {
  18. gwf := &GatewayForwarder{
  19. builds: map[string]gateway.LLBBridgeForwarder{},
  20. }
  21. gwf.updateCond = sync.NewCond(gwf.mu.RLocker())
  22. return gwf
  23. }
  24. func (gwf *GatewayForwarder) Register(server *grpc.Server) {
  25. gwapi.RegisterLLBBridgeServer(server, gwf)
  26. }
  27. func (gwf *GatewayForwarder) RegisterBuild(ctx context.Context, id string, bridge gateway.LLBBridgeForwarder) error {
  28. gwf.mu.Lock()
  29. defer gwf.mu.Unlock()
  30. if _, ok := gwf.builds[id]; ok {
  31. return errors.Errorf("build ID %s exists", id)
  32. }
  33. gwf.builds[id] = bridge
  34. gwf.updateCond.Broadcast()
  35. return nil
  36. }
  37. func (gwf *GatewayForwarder) UnregisterBuild(ctx context.Context, id string) {
  38. gwf.mu.Lock()
  39. defer gwf.mu.Unlock()
  40. delete(gwf.builds, id)
  41. gwf.updateCond.Broadcast()
  42. }
  43. func (gwf *GatewayForwarder) lookupForwarder(ctx context.Context) (gateway.LLBBridgeForwarder, error) {
  44. bid := buildid.FromIncomingContext(ctx)
  45. if bid == "" {
  46. return nil, errors.New("no buildid found in context")
  47. }
  48. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  49. defer cancel()
  50. go func() {
  51. <-ctx.Done()
  52. gwf.updateCond.Broadcast()
  53. }()
  54. gwf.mu.RLock()
  55. defer gwf.mu.RUnlock()
  56. for {
  57. select {
  58. case <-ctx.Done():
  59. return nil, errors.Errorf("no such job %s", bid)
  60. default:
  61. }
  62. fwd, ok := gwf.builds[bid]
  63. if !ok {
  64. gwf.updateCond.Wait()
  65. continue
  66. }
  67. return fwd, nil
  68. }
  69. }
  70. func (gwf *GatewayForwarder) ResolveImageConfig(ctx context.Context, req *gwapi.ResolveImageConfigRequest) (*gwapi.ResolveImageConfigResponse, error) {
  71. fwd, err := gwf.lookupForwarder(ctx)
  72. if err != nil {
  73. return nil, errors.Wrap(err, "forwarding ResolveImageConfig")
  74. }
  75. return fwd.ResolveImageConfig(ctx, req)
  76. }
  77. func (gwf *GatewayForwarder) Solve(ctx context.Context, req *gwapi.SolveRequest) (*gwapi.SolveResponse, error) {
  78. fwd, err := gwf.lookupForwarder(ctx)
  79. if err != nil {
  80. return nil, errors.Wrap(err, "forwarding Solve")
  81. }
  82. return fwd.Solve(ctx, req)
  83. }
  84. func (gwf *GatewayForwarder) ReadFile(ctx context.Context, req *gwapi.ReadFileRequest) (*gwapi.ReadFileResponse, error) {
  85. fwd, err := gwf.lookupForwarder(ctx)
  86. if err != nil {
  87. return nil, errors.Wrap(err, "forwarding ReadFile")
  88. }
  89. return fwd.ReadFile(ctx, req)
  90. }
  91. func (gwf *GatewayForwarder) Ping(ctx context.Context, req *gwapi.PingRequest) (*gwapi.PongResponse, error) {
  92. fwd, err := gwf.lookupForwarder(ctx)
  93. if err != nil {
  94. return nil, errors.Wrap(err, "forwarding Ping")
  95. }
  96. return fwd.Ping(ctx, req)
  97. }
  98. func (gwf *GatewayForwarder) Return(ctx context.Context, req *gwapi.ReturnRequest) (*gwapi.ReturnResponse, error) {
  99. fwd, err := gwf.lookupForwarder(ctx)
  100. if err != nil {
  101. return nil, errors.Wrap(err, "forwarding Return")
  102. }
  103. res, err := fwd.Return(ctx, req)
  104. return res, err
  105. }