mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
driver: move Features and HostGatewayIP to specific driver
Adds a new HostGatewayIP entry in the Driver interface so we can move Features and HostGatewayIP handling with cache to the very specific driver (docker). Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@ -399,6 +399,10 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
|
||||||
|
return nil, errors.New("host-gateway is not supported by the docker-container driver")
|
||||||
|
}
|
||||||
|
|
||||||
func demuxConn(c net.Conn) net.Conn {
|
func demuxConn(c net.Conn) net.Conn {
|
||||||
pr, pw := io.Pipe()
|
pr, pw := io.Pipe()
|
||||||
// TODO: rewrite parser with Reader() to avoid goroutine switch
|
// TODO: rewrite parser with Reader() to avoid goroutine switch
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/buildx/driver"
|
"github.com/docker/buildx/driver"
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
@ -15,6 +16,9 @@ import (
|
|||||||
type Driver struct {
|
type Driver struct {
|
||||||
factory driver.Factory
|
factory driver.Factory
|
||||||
driver.InitConfig
|
driver.InitConfig
|
||||||
|
|
||||||
|
features features
|
||||||
|
hostGateway hostGateway
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
|
func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
|
||||||
@ -70,24 +74,67 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
|
|||||||
return client.New(ctx, "", opts...)
|
return client.New(ctx, "", opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type features struct {
|
||||||
|
once sync.Once
|
||||||
|
list map[driver.Feature]bool
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
|
func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
|
||||||
var useContainerdSnapshotter bool
|
d.features.once.Do(func() {
|
||||||
c, err := d.Client(ctx)
|
var useContainerdSnapshotter bool
|
||||||
if err == nil {
|
if c, err := d.Client(ctx); err == nil {
|
||||||
workers, _ := c.ListWorkers(ctx)
|
workers, _ := c.ListWorkers(ctx)
|
||||||
|
for _, w := range workers {
|
||||||
|
if _, ok := w.Labels["org.mobyproject.buildkit.worker.snapshotter"]; ok {
|
||||||
|
useContainerdSnapshotter = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
d.features.list = map[driver.Feature]bool{
|
||||||
|
driver.OCIExporter: useContainerdSnapshotter,
|
||||||
|
driver.DockerExporter: useContainerdSnapshotter,
|
||||||
|
driver.CacheExport: useContainerdSnapshotter,
|
||||||
|
driver.MultiPlatform: useContainerdSnapshotter,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return d.features.list
|
||||||
|
}
|
||||||
|
|
||||||
|
type hostGateway struct {
|
||||||
|
once sync.Once
|
||||||
|
ip net.IP
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
|
||||||
|
d.hostGateway.once.Do(func() {
|
||||||
|
c, err := d.Client(ctx)
|
||||||
|
if err != nil {
|
||||||
|
d.hostGateway.err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
workers, err := c.ListWorkers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
d.hostGateway.err = errors.Wrap(err, "listing workers")
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, w := range workers {
|
for _, w := range workers {
|
||||||
if _, ok := w.Labels["org.mobyproject.buildkit.worker.snapshotter"]; ok {
|
// should match github.com/docker/docker/builder/builder-next/worker/label.HostGatewayIP const
|
||||||
useContainerdSnapshotter = true
|
if v, ok := w.Labels["org.mobyproject.buildkit.worker.moby.host-gateway-ip"]; ok && v != "" {
|
||||||
|
ip := net.ParseIP(v)
|
||||||
|
if ip == nil {
|
||||||
|
d.hostGateway.err = errors.Errorf("failed to parse host-gateway IP: %s", v)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.hostGateway.ip = ip
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.Close()
|
d.hostGateway.err = errors.New("host-gateway IP not found")
|
||||||
}
|
})
|
||||||
return map[driver.Feature]bool{
|
return d.hostGateway.ip, d.hostGateway.err
|
||||||
driver.OCIExporter: useContainerdSnapshotter,
|
|
||||||
driver.DockerExporter: useContainerdSnapshotter,
|
|
||||||
driver.CacheExport: useContainerdSnapshotter,
|
|
||||||
driver.MultiPlatform: useContainerdSnapshotter,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Factory() driver.Factory {
|
func (d *Driver) Factory() driver.Factory {
|
||||||
|
@ -3,6 +3,7 @@ package driver
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/docker/buildx/store"
|
"github.com/docker/buildx/store"
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
@ -60,6 +61,7 @@ type Driver interface {
|
|||||||
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
|
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
|
||||||
Client(ctx context.Context) (*client.Client, error)
|
Client(ctx context.Context) (*client.Client, error)
|
||||||
Features(ctx context.Context) map[Feature]bool
|
Features(ctx context.Context) map[Feature]bool
|
||||||
|
HostGatewayIP(ctx context.Context) (net.IP, error)
|
||||||
IsMobyDriver() bool
|
IsMobyDriver() bool
|
||||||
Config() InitConfig
|
Config() InitConfig
|
||||||
}
|
}
|
||||||
|
@ -236,3 +236,7 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
|
|||||||
driver.MultiPlatform: true, // Untested (needs multiple Driver instances)
|
driver.MultiPlatform: true, // Untested (needs multiple Driver instances)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
|
||||||
|
return nil, errors.New("host-gateway is not supported by the kubernetes driver")
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@ package driver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@ -150,13 +149,8 @@ type DriverHandle struct {
|
|||||||
client *client.Client
|
client *client.Client
|
||||||
err error
|
err error
|
||||||
once sync.Once
|
once sync.Once
|
||||||
featuresOnce sync.Once
|
|
||||||
features map[Feature]bool
|
|
||||||
historyAPISupportedOnce sync.Once
|
historyAPISupportedOnce sync.Once
|
||||||
historyAPISupported bool
|
historyAPISupported bool
|
||||||
hostGatewayIPOnce sync.Once
|
|
||||||
hostGatewayIP net.IP
|
|
||||||
hostGatewayIPErr error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
|
func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
|
||||||
@ -166,13 +160,6 @@ func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
|
|||||||
return d.client, d.err
|
return d.client, d.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DriverHandle) Features(ctx context.Context) map[Feature]bool {
|
|
||||||
d.featuresOnce.Do(func() {
|
|
||||||
d.features = d.Driver.Features(ctx)
|
|
||||||
})
|
|
||||||
return d.features
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
|
func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
|
||||||
d.historyAPISupportedOnce.Do(func() {
|
d.historyAPISupportedOnce.Do(func() {
|
||||||
if c, err := d.Client(ctx); err == nil {
|
if c, err := d.Client(ctx); err == nil {
|
||||||
@ -181,36 +168,3 @@ func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
|
|||||||
})
|
})
|
||||||
return d.historyAPISupported
|
return d.historyAPISupported
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DriverHandle) HostGatewayIP(ctx context.Context) (net.IP, error) {
|
|
||||||
d.hostGatewayIPOnce.Do(func() {
|
|
||||||
if !d.Driver.IsMobyDriver() {
|
|
||||||
d.hostGatewayIPErr = errors.New("host-gateway is only supported with the docker driver")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c, err := d.Client(ctx)
|
|
||||||
if err != nil {
|
|
||||||
d.hostGatewayIPErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
workers, err := c.ListWorkers(ctx)
|
|
||||||
if err != nil {
|
|
||||||
d.hostGatewayIPErr = errors.Wrap(err, "listing workers")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, w := range workers {
|
|
||||||
// should match github.com/docker/docker/builder/builder-next/worker/label.HostGatewayIP const
|
|
||||||
if v, ok := w.Labels["org.mobyproject.buildkit.worker.moby.host-gateway-ip"]; ok && v != "" {
|
|
||||||
ip := net.ParseIP(v)
|
|
||||||
if ip == nil {
|
|
||||||
d.hostGatewayIPErr = errors.Errorf("failed to parse host-gateway IP: %s", v)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
d.hostGatewayIP = ip
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.hostGatewayIPErr = errors.New("host-gateway IP not found")
|
|
||||||
})
|
|
||||||
return d.hostGatewayIP, d.hostGatewayIPErr
|
|
||||||
}
|
|
||||||
|
@ -2,6 +2,8 @@ package remote
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/docker/buildx/driver"
|
"github.com/docker/buildx/driver"
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
@ -91,6 +93,10 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
|
||||||
|
return nil, errors.New("host-gateway is not supported by the remote driver")
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Driver) Factory() driver.Factory {
|
func (d *Driver) Factory() driver.Factory {
|
||||||
return d.factory
|
return d.factory
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user