mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
Add dial-stdio command
This allows the buildx CLI to act a proxy to the configured instance. It allows external code to use buildx itself as a driver for connecting to buildkitd instances. Instance and node selection should follow the same semantics as as `buildx build`, including taking into account the `BUILDX_BUILDER` env var and the `--builder` global flag. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
62
build/dial.go
Normal file
62
build/dial.go
Normal file
@ -0,0 +1,62 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"net"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/docker/buildx/builder"
|
||||
"github.com/docker/buildx/util/progress"
|
||||
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func Dial(ctx context.Context, nodes []builder.Node, pw progress.Writer, platform *v1.Platform) (net.Conn, error) {
|
||||
nodes, err := filterAvailableNodes(nodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(nodes) == 0 {
|
||||
return nil, errors.New("no nodes available")
|
||||
}
|
||||
|
||||
var pls []v1.Platform
|
||||
if platform != nil {
|
||||
pls = []v1.Platform{*platform}
|
||||
}
|
||||
|
||||
opts := map[string]Options{"default": {Platforms: pls}}
|
||||
resolved, err := resolveDrivers(ctx, nodes, opts, pw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var dialError error
|
||||
for _, ls := range resolved {
|
||||
for _, rn := range ls {
|
||||
if platform != nil {
|
||||
p := *platform
|
||||
var found bool
|
||||
for _, pp := range rn.platforms {
|
||||
if platforms.Only(p).Match(pp) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := nodes[rn.driverIndex].Driver.Dial(ctx)
|
||||
if err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
dialError = stderrors.Join(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.Wrap(dialError, "no nodes available")
|
||||
}
|
Reference in New Issue
Block a user