Skip to content

Commit

Permalink
Merge pull request #44650 from thaJeztah/20.10_backport_builder_host_…
Browse files Browse the repository at this point in the history
…gateway

[20.10 backport] builder: handle host-gateway with extra hosts
  • Loading branch information
thaJeztah committed Dec 29, 2022
2 parents d3bf459 + 877a5e6 commit d46fe80
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions builder/builder-next/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/docker/docker/builder"
"github.com/docker/docker/daemon/config"
"github.com/docker/docker/daemon/images"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/pkg/system"
Expand Down Expand Up @@ -81,6 +82,7 @@ type Opt struct {
// Builder can build using BuildKit backend
type Builder struct {
controller *control.Controller
dnsconfig config.DNSConfig
reqBodyHandler *reqBodyHandler

mu sync.Mutex
Expand All @@ -101,6 +103,7 @@ func New(opt Opt) (*Builder, error) {
}
b := &Builder{
controller: c,
dnsconfig: opt.DNSConfig,
reqBodyHandler: reqHandler,
jobs: map[string]*buildJob{},
}
Expand Down Expand Up @@ -317,7 +320,7 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
return nil, errors.Errorf("network mode %q not supported by buildkit", opt.Options.NetworkMode)
}

extraHosts, err := toBuildkitExtraHosts(opt.Options.ExtraHosts)
extraHosts, err := toBuildkitExtraHosts(opt.Options.ExtraHosts, b.dnsconfig.HostGatewayIP)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -546,18 +549,28 @@ func (j *buildJob) SetUpload(ctx context.Context, rc io.ReadCloser) error {
}

// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
func toBuildkitExtraHosts(inp []string) (string, error) {
func toBuildkitExtraHosts(inp []string, hostGatewayIP net.IP) (string, error) {
if len(inp) == 0 {
return "", nil
}
hosts := make([]string, 0, len(inp))
for _, h := range inp {
parts := strings.Split(h, ":")

if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
host, ip, ok := strings.Cut(h, ":")
if !ok || host == "" || ip == "" {
return "", errors.Errorf("invalid host %s", h)
}
hosts = append(hosts, parts[0]+"="+parts[1])
// If the IP Address is a "host-gateway", replace this value with the
// IP address stored in the daemon level HostGatewayIP config variable.
if ip == opts.HostGatewayName {
gateway := hostGatewayIP.String()
if gateway == "" {
return "", fmt.Errorf("unable to derive the IP value for host-gateway")
}
ip = gateway
} else if net.ParseIP(ip) == nil {
return "", fmt.Errorf("invalid host %s", h)
}
hosts = append(hosts, host+"="+ip)
}
return strings.Join(hosts, ","), nil
}
Expand Down

0 comments on commit d46fe80

Please sign in to comment.