Skip to content

Commit 8cbb7a9

Browse files
committed
build: fix host-gateway handling
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 4f5a56a commit 8cbb7a9

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
645645
}
646646

647647
// setup extrahosts
648-
extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts, nodeDriver.IsMobyDriver())
648+
extraHosts, err := toBuildkitExtraHosts(ctx, opt.ExtraHosts, nodeDriver)
649649
if err != nil {
650650
return nil, nil, err
651651
}

build/utils.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package build
33
import (
44
"archive/tar"
55
"bytes"
6+
"context"
67
"net"
78
"os"
89
"strings"
910

11+
"github.com/docker/buildx/driver"
1012
"github.com/docker/cli/opts"
1113
"github.com/docker/docker/builder/remotecontext/urlutil"
1214
"github.com/moby/buildkit/util/gitutil"
@@ -57,7 +59,7 @@ func isArchive(header []byte) bool {
5759
}
5860

5961
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
60-
func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
62+
func toBuildkitExtraHosts(ctx context.Context, inp []string, nodeDriver *driver.DriverHandle) (string, error) {
6163
if len(inp) == 0 {
6264
return "", nil
6365
}
@@ -67,11 +69,16 @@ func toBuildkitExtraHosts(inp []string, mobyDriver bool) (string, error) {
6769
if !ok || host == "" || ip == "" {
6870
return "", errors.Errorf("invalid host %s", h)
6971
}
70-
// Skip IP address validation for "host-gateway" string with moby driver
71-
if !mobyDriver || ip != mobyHostGatewayName {
72-
if net.ParseIP(ip) == nil {
73-
return "", errors.Errorf("invalid host %s", h)
72+
// If the IP Address is a "host-gateway", replace this value with the
73+
// IP address provided by the worker's label.
74+
if ip == mobyHostGatewayName {
75+
hgip, err := nodeDriver.HostGatewayIP(ctx)
76+
if err != nil {
77+
return "", errors.Wrap(err, "unable to derive the IP value for host-gateway")
7478
}
79+
ip = hgip.String()
80+
} else if net.ParseIP(ip) == nil {
81+
return "", errors.Errorf("invalid host %s", h)
7582
}
7683
hosts = append(hosts, host+"="+ip)
7784
}

driver/manager.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package driver
22

33
import (
44
"context"
5+
"net"
56
"os"
67
"sort"
78
"strings"
89
"sync"
910

10-
"k8s.io/client-go/rest"
11-
1211
dockerclient "github.com/docker/docker/client"
1312
"github.com/moby/buildkit/client"
1413
specs "github.com/opencontainers/image-spec/specs-go/v1"
1514
"github.com/pkg/errors"
15+
"k8s.io/client-go/rest"
1616
)
1717

1818
type Factory interface {
@@ -154,6 +154,9 @@ type DriverHandle struct {
154154
features map[Feature]bool
155155
historyAPISupportedOnce sync.Once
156156
historyAPISupported bool
157+
hostGatewayIPOnce sync.Once
158+
hostGatewayIP net.IP
159+
hostGatewayIPErr error
157160
}
158161

159162
func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
@@ -178,3 +181,36 @@ func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
178181
})
179182
return d.historyAPISupported
180183
}
184+
185+
func (d *DriverHandle) HostGatewayIP(ctx context.Context) (net.IP, error) {
186+
d.hostGatewayIPOnce.Do(func() {
187+
if !d.Driver.IsMobyDriver() {
188+
d.hostGatewayIPErr = errors.New("host-gateway is only supported with the docker driver")
189+
return
190+
}
191+
c, err := d.Client(ctx)
192+
if err != nil {
193+
d.hostGatewayIPErr = err
194+
return
195+
}
196+
workers, err := c.ListWorkers(ctx)
197+
if err != nil {
198+
d.hostGatewayIPErr = errors.Wrap(err, "listing workers")
199+
return
200+
}
201+
for _, w := range workers {
202+
// should match github.com/docker/docker/builder/builder-next/worker/label.HostGatewayIP const
203+
if v, ok := w.Labels["org.mobyproject.buildkit.worker.moby.host-gateway-ip"]; ok && v != "" {
204+
ip := net.ParseIP(v)
205+
if ip == nil {
206+
d.hostGatewayIPErr = errors.Errorf("failed to parse host-gateway IP: %s", v)
207+
return
208+
}
209+
d.hostGatewayIP = ip
210+
return
211+
}
212+
}
213+
d.hostGatewayIPErr = errors.New("host-gateway IP not found")
214+
})
215+
return d.hostGatewayIP, d.hostGatewayIPErr
216+
}

0 commit comments

Comments
 (0)