| 1 |
package forwarder |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"sync" |
| 6 |
|
| 7 |
cacheutil "github.com/moby/buildkit/cache/util" |
| 8 |
"github.com/moby/buildkit/client/llb" |
| 9 |
"github.com/moby/buildkit/client/llb/sourceresolver" |
| 10 |
"github.com/moby/buildkit/executor" |
| 11 |
"github.com/moby/buildkit/frontend" |
| 12 |
"github.com/moby/buildkit/frontend/gateway/client" |
| 13 |
"github.com/moby/buildkit/frontend/gateway/container" |
| 14 |
gwpb "github.com/moby/buildkit/frontend/gateway/pb" |
| 15 |
"github.com/moby/buildkit/identity" |
| 16 |
"github.com/moby/buildkit/session" |
| 17 |
"github.com/moby/buildkit/snapshot" |
| 18 |
"github.com/moby/buildkit/solver" |
| 19 |
"github.com/moby/buildkit/solver/errdefs" |
| 20 |
llberrdefs "github.com/moby/buildkit/solver/llbsolver/errdefs" |
| 21 |
opspb "github.com/moby/buildkit/solver/pb" |
| 22 |
"github.com/moby/buildkit/solver/result" |
| 23 |
"github.com/moby/buildkit/util/apicaps" |
| 24 |
"github.com/moby/buildkit/worker" |
| 25 |
digest "github.com/opencontainers/go-digest" |
| 26 |
"github.com/pkg/errors" |
| 27 |
fstypes "github.com/tonistiigi/fsutil/types" |
| 28 |
"golang.org/x/sync/errgroup" |
| 29 |
) |
| 30 |
|
| 31 |
func LLBBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLLBBridge, exec executor.Executor, opts map[string]string, inputs map[string]*opspb.Definition, w worker.Infos, sid string, sm *session.Manager) (*BridgeClient, error) { |
| 32 |
bc := &BridgeClient{ |
| 33 |
opts: opts, |
| 34 |
inputs: inputs, |
| 35 |
FrontendLLBBridge: llbBridge, |
| 36 |
sid: sid, |
| 37 |
sm: sm, |
| 38 |
workers: w, |
| 39 |
workerRefByID: make(map[string]*worker.WorkerRef), |
| 40 |
executor: exec, |
| 41 |
} |
| 42 |
bc.buildOpts = bc.loadBuildOpts() |
| 43 |
return bc, nil |
| 44 |
} |
| 45 |
|
| 46 |
type BridgeClient struct { |
| 47 |
frontend.FrontendLLBBridge |
| 48 |
mu sync.Mutex |
| 49 |
opts map[string]string |
| 50 |
inputs map[string]*opspb.Definition |
| 51 |
sid string |
| 52 |
sm *session.Manager |
| 53 |
refs []*ref |
| 54 |
workers worker.Infos |
| 55 |
workerRefByID map[string]*worker.WorkerRef |
| 56 |
buildOpts client.BuildOpts |
| 57 |
ctrs []client.Container |
| 58 |
executor executor.Executor |
| 59 |
} |
| 60 |
|
| 61 |
func (c *BridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*client.Result, error) { |
| 62 |
res, err := c.FrontendLLBBridge.Solve(ctx, req, c.sid) |
| 63 |
if err != nil { |
| 64 |
return nil, c.wrapSolveError(err) |
| 65 |
} |
| 66 |
for _, atts := range res.Attestations { |
| 67 |
for _, att := range atts { |
| 68 |
if att.ContentFunc != nil { |
| 69 |
return nil, errors.Errorf("attestation callback cannot be sent through gateway") |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
c.mu.Lock() |
| 75 |
cRes, err := result.ConvertResult(res, func(r solver.ResultProxy) (client.Reference, error) { |
| 76 |
rr, err := c.newRef(r, session.NewGroup(c.sid)) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
c.refs = append(c.refs, rr) |
| 81 |
return rr, nil |
| 82 |
}) |
| 83 |
c.mu.Unlock() |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
return cRes, nil |
| 89 |
} |
| 90 |
|
| 91 |
func (c *BridgeClient) ResolveImageConfig(ctx context.Context, ref string, opt sourceresolver.Opt) (string, digest.Digest, []byte, error) { |
| 92 |
imr := sourceresolver.NewImageMetaResolver(c) |
| 93 |
return imr.ResolveImageConfig(ctx, ref, opt) |
| 94 |
} |
| 95 |
|
| 96 |
func (c *BridgeClient) loadBuildOpts() client.BuildOpts { |
| 97 |
wis := c.workers.WorkerInfos() |
| 98 |
workers := make([]client.WorkerInfo, len(wis)) |
| 99 |
for i, w := range wis { |
| 100 |
workers[i] = client.WorkerInfo{ |
| 101 |
ID: w.ID, |
| 102 |
Labels: w.Labels, |
| 103 |
Platforms: w.Platforms, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return client.BuildOpts{ |
| 108 |
Opts: c.opts, |
| 109 |
SessionID: c.sid, |
| 110 |
Workers: workers, |
| 111 |
Product: apicaps.ExportedProduct, |
| 112 |
Caps: gwpb.Caps.CapSet(gwpb.Caps.All()), |
| 113 |
LLBCaps: opspb.Caps.CapSet(opspb.Caps.All()), |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (c *BridgeClient) BuildOpts() client.BuildOpts { |
| 118 |
return c.buildOpts |
| 119 |
} |
| 120 |
|
| 121 |
func (c *BridgeClient) Inputs(ctx context.Context) (map[string]llb.State, error) { |
| 122 |
inputs := make(map[string]llb.State) |
| 123 |
for key, def := range c.inputs { |
| 124 |
defop, err := llb.NewDefinitionOp(def) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
inputs[key] = llb.NewState(defop) |
| 129 |
} |
| 130 |
return inputs, nil |
| 131 |
} |
| 132 |
|
| 133 |
func (c *BridgeClient) wrapSolveError(solveErr error) error { |
| 134 |
var ( |
| 135 |
ee *llberrdefs.ExecError |
| 136 |
fae *llberrdefs.FileActionError |
| 137 |
sce *solver.SlowCacheError |
| 138 |
inputIDs []string |
| 139 |
mountIDs []string |
| 140 |
subject errdefs.IsSolve_Subject |
| 141 |
) |
| 142 |
if errors.As(solveErr, &ee) { |
| 143 |
var err error |
| 144 |
inputIDs, err = c.registerResultIDs(ee.Inputs...) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
mountIDs, err = c.registerResultIDs(ee.Mounts...) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
} |
| 153 |
if errors.As(solveErr, &fae) { |
| 154 |
subject = fae.ToSubject() |
| 155 |
} |
| 156 |
if errors.As(solveErr, &sce) { |
| 157 |
var err error |
| 158 |
inputIDs, err = c.registerResultIDs(sce.Result) |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
subject = sce.ToSubject() |
| 163 |
} |
| 164 |
return errdefs.WithSolveError(solveErr, subject, inputIDs, mountIDs) |
| 165 |
} |
| 166 |
|
| 167 |
func (c *BridgeClient) registerResultIDs(results ...solver.Result) (ids []string, err error) { |
| 168 |
c.mu.Lock() |
| 169 |
defer c.mu.Unlock() |
| 170 |
|
| 171 |
ids = make([]string, len(results)) |
| 172 |
for i, res := range results { |
| 173 |
if res == nil { |
| 174 |
continue |
| 175 |
} |
| 176 |
workerRef, ok := res.Sys().(*worker.WorkerRef) |
| 177 |
if !ok { |
| 178 |
return ids, errors.Errorf("unexpected type for result, got %T", res.Sys()) |
| 179 |
} |
| 180 |
ids[i] = workerRef.ID() |
| 181 |
c.workerRefByID[workerRef.ID()] = workerRef |
| 182 |
} |
| 183 |
return ids, nil |
| 184 |
} |
| 185 |
|
| 186 |
func (c *BridgeClient) toFrontendResult(r *client.Result) (*frontend.Result, error) { |
| 187 |
if r == nil { |
| 188 |
return nil, nil |
| 189 |
} |
| 190 |
for _, atts := range r.Attestations { |
| 191 |
for _, att := range atts { |
| 192 |
if att.ContentFunc != nil { |
| 193 |
return nil, errors.Errorf("attestation callback cannot be sent through gateway") |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
res, err := result.ConvertResult(r, func(r client.Reference) (solver.ResultProxy, error) { |
| 199 |
rr, ok := r.(*ref) |
| 200 |
if !ok { |
| 201 |
return nil, errors.Errorf("invalid reference type for forward %T", r) |
| 202 |
} |
| 203 |
return rr.acquireResultProxy(), nil |
| 204 |
}) |
| 205 |
if err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
return res, nil |
| 209 |
} |
| 210 |
|
| 211 |
func (c *BridgeClient) discard(err error) { |
| 212 |
for _, ctr := range c.ctrs { |
| 213 |
ctr.Release(context.TODO()) |
| 214 |
} |
| 215 |
|
| 216 |
for id, workerRef := range c.workerRefByID { |
| 217 |
workerRef.ImmutableRef.Release(context.TODO()) |
| 218 |
delete(c.workerRefByID, id) |
| 219 |
} |
| 220 |
for _, r := range c.refs { |
| 221 |
if r != nil { |
| 222 |
r.resultProxy.Release(context.TODO()) |
| 223 |
if err != nil { |
| 224 |
for _, clone := range r.resultProxyClones { |
| 225 |
clone.Release(context.TODO()) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func (c *BridgeClient) Warn(ctx context.Context, dgst digest.Digest, msg string, opts client.WarnOpts) error { |
| 233 |
return c.FrontendLLBBridge.Warn(ctx, dgst, msg, opts) |
| 234 |
} |
| 235 |
|
| 236 |
func (c *BridgeClient) NewContainer(ctx context.Context, req client.NewContainerRequest) (client.Container, error) { |
| 237 |
ctrReq := container.NewContainerRequest{ |
| 238 |
ContainerID: identity.NewID(), |
| 239 |
NetMode: req.NetMode, |
| 240 |
Hostname: req.Hostname, |
| 241 |
Mounts: make([]container.Mount, len(req.Mounts)), |
| 242 |
} |
| 243 |
|
| 244 |
eg, ctx := errgroup.WithContext(ctx) |
| 245 |
|
| 246 |
for i, m := range req.Mounts { |
| 247 |
i, m := i, m |
| 248 |
eg.Go(func() error { |
| 249 |
var workerRef *worker.WorkerRef |
| 250 |
if m.Ref != nil { |
| 251 |
refProxy, ok := m.Ref.(*ref) |
| 252 |
if !ok { |
| 253 |
return errors.Errorf("unexpected Ref type: %T", m.Ref) |
| 254 |
} |
| 255 |
|
| 256 |
res, err := refProxy.resultProxy.Result(ctx) |
| 257 |
if err != nil { |
| 258 |
return err |
| 259 |
} |
| 260 |
|
| 261 |
workerRef, ok = res.Sys().(*worker.WorkerRef) |
| 262 |
if !ok { |
| 263 |
return errors.Errorf("invalid ref: %T", res.Sys()) |
| 264 |
} |
| 265 |
} else if m.ResultID != "" { |
| 266 |
var ok bool |
| 267 |
workerRef, ok = c.workerRefByID[m.ResultID] |
| 268 |
if !ok { |
| 269 |
return errors.Errorf("failed to find ref %s for %q mount", m.ResultID, m.Dest) |
| 270 |
} |
| 271 |
} |
| 272 |
ctrReq.Mounts[i] = container.Mount{ |
| 273 |
WorkerRef: workerRef, |
| 274 |
Mount: &opspb.Mount{ |
| 275 |
Dest: m.Dest, |
| 276 |
Selector: m.Selector, |
| 277 |
Readonly: m.Readonly, |
| 278 |
MountType: m.MountType, |
| 279 |
CacheOpt: m.CacheOpt, |
| 280 |
SecretOpt: m.SecretOpt, |
| 281 |
SSHOpt: m.SSHOpt, |
| 282 |
}, |
| 283 |
} |
| 284 |
return nil |
| 285 |
}) |
| 286 |
} |
| 287 |
|
| 288 |
err := eg.Wait() |
| 289 |
if err != nil { |
| 290 |
return nil, err |
| 291 |
} |
| 292 |
|
| 293 |
ctrReq.ExtraHosts, err = container.ParseExtraHosts(req.ExtraHosts) |
| 294 |
if err != nil { |
| 295 |
return nil, err |
| 296 |
} |
| 297 |
|
| 298 |
cm, err := c.workers.DefaultCacheManager() |
| 299 |
if err != nil { |
| 300 |
return nil, err |
| 301 |
} |
| 302 |
|
| 303 |
group := session.NewGroup(c.sid) |
| 304 |
ctr, err := container.NewContainer(ctx, cm, c.executor, c.sm, group, ctrReq) |
| 305 |
if err != nil { |
| 306 |
return nil, err |
| 307 |
} |
| 308 |
c.ctrs = append(c.ctrs, ctr) |
| 309 |
return ctr, nil |
| 310 |
} |
| 311 |
|
| 312 |
func (c *BridgeClient) newRef(r solver.ResultProxy, s session.Group) (*ref, error) { |
| 313 |
return &ref{resultProxy: r, session: s, c: c}, nil |
| 314 |
} |
| 315 |
|
| 316 |
type ref struct { |
| 317 |
resultProxy solver.ResultProxy |
| 318 |
resultProxyClones []solver.ResultProxy |
| 319 |
|
| 320 |
session session.Group |
| 321 |
c *BridgeClient |
| 322 |
} |
| 323 |
|
| 324 |
func (r *ref) acquireResultProxy() solver.ResultProxy { |
| 325 |
s1, s2 := solver.SplitResultProxy(r.resultProxy) |
| 326 |
r.resultProxy = s1 |
| 327 |
r.resultProxyClones = append(r.resultProxyClones, s2) |
| 328 |
return s2 |
| 329 |
} |
| 330 |
|
| 331 |
func (r *ref) ToState() (st llb.State, err error) { |
| 332 |
defop, err := llb.NewDefinitionOp(r.resultProxy.Definition()) |
| 333 |
if err != nil { |
| 334 |
return st, err |
| 335 |
} |
| 336 |
return llb.NewState(defop), nil |
| 337 |
} |
| 338 |
|
| 339 |
func (r *ref) Evaluate(ctx context.Context) error { |
| 340 |
_, err := r.resultProxy.Result(ctx) |
| 341 |
if err != nil { |
| 342 |
return r.c.wrapSolveError(err) |
| 343 |
} |
| 344 |
return nil |
| 345 |
} |
| 346 |
|
| 347 |
func (r *ref) ReadFile(ctx context.Context, req client.ReadRequest) ([]byte, error) { |
| 348 |
m, err := r.getMountable(ctx) |
| 349 |
if err != nil { |
| 350 |
return nil, err |
| 351 |
} |
| 352 |
newReq := cacheutil.ReadRequest{ |
| 353 |
Filename: req.Filename, |
| 354 |
} |
| 355 |
if r := req.Range; r != nil { |
| 356 |
newReq.Range = &cacheutil.FileRange{ |
| 357 |
Offset: r.Offset, |
| 358 |
Length: r.Length, |
| 359 |
} |
| 360 |
} |
| 361 |
return cacheutil.ReadFile(ctx, m, newReq) |
| 362 |
} |
| 363 |
|
| 364 |
func (r *ref) ReadDir(ctx context.Context, req client.ReadDirRequest) ([]*fstypes.Stat, error) { |
| 365 |
m, err := r.getMountable(ctx) |
| 366 |
if err != nil { |
| 367 |
return nil, err |
| 368 |
} |
| 369 |
newReq := cacheutil.ReadDirRequest{ |
| 370 |
Path: req.Path, |
| 371 |
IncludePattern: req.IncludePattern, |
| 372 |
} |
| 373 |
return cacheutil.ReadDir(ctx, m, newReq) |
| 374 |
} |
| 375 |
|
| 376 |
func (r *ref) StatFile(ctx context.Context, req client.StatRequest) (*fstypes.Stat, error) { |
| 377 |
m, err := r.getMountable(ctx) |
| 378 |
if err != nil { |
| 379 |
return nil, err |
| 380 |
} |
| 381 |
return cacheutil.StatFile(ctx, m, req.Path) |
| 382 |
} |
| 383 |
|
| 384 |
func (r *ref) getMountable(ctx context.Context) (snapshot.Mountable, error) { |
| 385 |
rr, err := r.resultProxy.Result(ctx) |
| 386 |
if err != nil { |
| 387 |
return nil, r.c.wrapSolveError(err) |
| 388 |
} |
| 389 |
ref, ok := rr.Sys().(*worker.WorkerRef) |
| 390 |
if !ok { |
| 391 |
return nil, errors.Errorf("invalid ref: %T", rr.Sys()) |
| 392 |
} |
| 393 |
return ref.ImmutableRef.Mount(ctx, true, r.session) |
| 394 |
} |