mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 18:13:42 +08:00 
			
		
		
		
	build: fix host-gateway handling
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
		@@ -645,7 +645,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// setup extrahosts
 | 
						// setup extrahosts
 | 
				
			||||||
	extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts, nodeDriver.IsMobyDriver())
 | 
						extraHosts, err := toBuildkitExtraHosts(ctx, opt.ExtraHosts, nodeDriver)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, nil, err
 | 
							return nil, nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,10 +3,12 @@ package build
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"archive/tar"
 | 
						"archive/tar"
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/docker/buildx/driver"
 | 
				
			||||||
	"github.com/docker/cli/opts"
 | 
						"github.com/docker/cli/opts"
 | 
				
			||||||
	"github.com/docker/docker/builder/remotecontext/urlutil"
 | 
						"github.com/docker/docker/builder/remotecontext/urlutil"
 | 
				
			||||||
	"github.com/moby/buildkit/util/gitutil"
 | 
						"github.com/moby/buildkit/util/gitutil"
 | 
				
			||||||
@@ -57,7 +59,7 @@ func isArchive(header []byte) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
 | 
					// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
 | 
				
			||||||
func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
 | 
					func toBuildkitExtraHosts(ctx context.Context, inp []string, nodeDriver *driver.DriverHandle) (string, error) {
 | 
				
			||||||
	if len(inp) == 0 {
 | 
						if len(inp) == 0 {
 | 
				
			||||||
		return "", nil
 | 
							return "", nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -67,11 +69,16 @@ func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
 | 
				
			|||||||
		if !ok || host == "" || ip == "" {
 | 
							if !ok || host == "" || ip == "" {
 | 
				
			||||||
			return "", errors.Errorf("invalid host %s", h)
 | 
								return "", errors.Errorf("invalid host %s", h)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// Skip IP address validation for "host-gateway" string with moby driver
 | 
							// If the IP Address is a "host-gateway", replace this value with the
 | 
				
			||||||
		if !mobyDriver || ip != mobyHostGatewayName {
 | 
							// IP address provided by the worker's label.
 | 
				
			||||||
			if net.ParseIP(ip) == nil {
 | 
							if ip == mobyHostGatewayName {
 | 
				
			||||||
				return "", errors.Errorf("invalid host %s", h)
 | 
								hgip, err := nodeDriver.HostGatewayIP(ctx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return "", errors.Wrap(err, "unable to derive the IP value for host-gateway")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								ip = hgip.String()
 | 
				
			||||||
 | 
							} else if net.ParseIP(ip) == nil {
 | 
				
			||||||
 | 
								return "", errors.Errorf("invalid host %s", h)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		hosts = append(hosts, host+"="+ip)
 | 
							hosts = append(hosts, host+"="+ip)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,17 +2,17 @@ package driver
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/client-go/rest"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	dockerclient "github.com/docker/docker/client"
 | 
						dockerclient "github.com/docker/docker/client"
 | 
				
			||||||
	"github.com/moby/buildkit/client"
 | 
						"github.com/moby/buildkit/client"
 | 
				
			||||||
	specs "github.com/opencontainers/image-spec/specs-go/v1"
 | 
						specs "github.com/opencontainers/image-spec/specs-go/v1"
 | 
				
			||||||
	"github.com/pkg/errors"
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
						"k8s.io/client-go/rest"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Factory interface {
 | 
					type Factory interface {
 | 
				
			||||||
@@ -154,6 +154,9 @@ type DriverHandle struct {
 | 
				
			|||||||
	features                map[Feature]bool
 | 
						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) {
 | 
				
			||||||
@@ -178,3 +181,36 @@ 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user