Skip to content

Commit

Permalink
build: fix host-gateway handling
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Jun 20, 2023
1 parent daba16f commit 5515d9f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
8 changes: 7 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,14 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
return nil, nil, errors.Errorf("network mode %q not supported by buildkit - you can define a custom network for your builder using the network driver-opt in buildx create", opt.NetworkMode)
}

// retrieve host gateway IP from node driver
hostGatewayIP, err := nodeDriver.HostGatewayIP(ctx)
if err != nil {
return nil, nil, err
}

// setup extrahosts
extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts, nodeDriver.IsMobyDriver())
extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts, hostGatewayIP)
if err != nil {
return nil, nil, err
}
Expand Down
15 changes: 10 additions & 5 deletions build/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func isArchive(header []byte) bool {
}

// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
func toBuildkitExtraHosts(inp []string, hgip net.IP) (string, error) {
if len(inp) == 0 {
return "", nil
}
Expand All @@ -67,11 +67,16 @@ func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
if !ok || host == "" || ip == "" {
return "", errors.Errorf("invalid host %s", h)
}
// Skip IP address validation for "host-gateway" string with moby driver
if !mobyDriver || ip != mobyHostGatewayName {
if net.ParseIP(ip) == nil {
return "", errors.Errorf("invalid host %s", h)
// If the IP Address is a "host-gateway", replace this value with the
// IP address provided by the worker's label.
if ip == mobyHostGatewayName {
if hgip == nil {
return "", errors.Errorf("unable to derive the IP value for host-gateway")
}
ip = hgip.String()
}
if net.ParseIP(ip) == nil {
return "", errors.Errorf("invalid host %s", h)
}
hosts = append(hosts, host+"="+ip)
}
Expand Down
38 changes: 36 additions & 2 deletions driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package driver

import (
"context"
"net"
"os"
"sort"
"strings"
"sync"

"k8s.io/client-go/rest"

dockerclient "github.com/docker/docker/client"
"github.com/moby/buildkit/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"k8s.io/client-go/rest"
)

type Factory interface {
Expand Down Expand Up @@ -154,6 +154,9 @@ type DriverHandle struct {
features map[Feature]bool
historyAPISupportedOnce sync.Once
historyAPISupported bool
hostGatewayIPOnce sync.Once
hostGatewayIP net.IP
hostGatewayIPErr error
}

func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
Expand All @@ -178,3 +181,34 @@ func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
})
return d.historyAPISupported
}

func (d *DriverHandle) HostGatewayIP(ctx context.Context) (net.IP, error) {
d.hostGatewayIPOnce.Do(func() {
if !d.Driver.IsMobyDriver() {
return
}
c, err := d.Client(ctx)
if err != nil {
d.hostGatewayIPErr = err
return
}
workers, err := c.ListWorkers(ctx)
if err != nil {
d.hostGatewayIPErr = err
return
}
if len(workers) == 0 {
return
}
// TODO: Use HostGatewayIP const from github.com/docker/docker/builder/builder-next/worker/label pkg
if v, ok := workers[0].Labels["org.mobyproject.buildkit.worker.hostgatewayip"]; ok && v != "" {
ip := net.ParseIP(v)
if ip == nil {
d.hostGatewayIPErr = errors.Wrapf(err, "failed to parse host gateway IP: %s", v)
return
}
d.hostGatewayIP = ip
}
})
return d.hostGatewayIP, d.hostGatewayIPErr
}

0 comments on commit 5515d9f

Please sign in to comment.