Skip to content

Commit b2d103f

Browse files
committed
executor/oci: use fork of libnetwork/resolvconf
Rewrite the resolvconf code to use libnetwork's internal packege, which allows us to skip some of the moby-specific handling (writing to a file, creating a hash of the file to detect changes made by the user (not supported by BuildKit, which always mounts read-only). This rewrite also allows us to skip GetNameservers, GetSearchDomains, GetOptions, and FilterResolvDNS, which repeatedly would parse the resolvconf file for each of them. The new code parses the original resolvconf once, after which mutations (overrides) are done in memory, after which we generate the resolv.conf to write to disk. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 504b10d commit b2d103f

File tree

6 files changed

+27
-741
lines changed

6 files changed

+27
-741
lines changed

executor/oci/resolvconf.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package oci
22

33
import (
44
"context"
5+
"net/netip"
56
"os"
67
"path/filepath"
78

8-
"github.com/docker/docker/libnetwork/resolvconf"
9+
"github.com/moby/buildkit/executor/oci/internal/resolvconf"
910
"github.com/moby/buildkit/solver/pb"
1011
"github.com/moby/buildkit/util/flightcontrol"
1112
"github.com/moby/sys/user"
@@ -76,41 +77,40 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *user.IdentityMap
7677
return struct{}{}, nil
7778
}
7879

79-
dt, err := os.ReadFile(resolvconfPath(netMode))
80+
rc, err := resolvconf.Load(resolvconfPath(netMode))
8081
if err != nil && !errors.Is(err, os.ErrNotExist) {
8182
return struct{}{}, errors.WithStack(err)
8283
}
8384

84-
tmpPath := p + ".tmp"
8585
if dns != nil {
86-
var (
87-
dnsNameservers = dns.Nameservers
88-
dnsSearchDomains = dns.SearchDomains
89-
dnsOptions = dns.Options
90-
)
91-
if len(dns.Nameservers) == 0 {
92-
dnsNameservers = resolvconf.GetNameservers(dt, resolvconf.IP)
86+
if len(dns.Nameservers) > 0 {
87+
var ns []netip.Addr
88+
for _, addr := range dns.Nameservers {
89+
ipAddr, err := netip.ParseAddr(addr)
90+
if err != nil {
91+
return struct{}{}, errors.WithStack(errors.Wrap(err, "bad nameserver address"))
92+
}
93+
ns = append(ns, ipAddr)
94+
}
95+
rc.OverrideNameServers(ns)
9396
}
94-
if len(dns.SearchDomains) == 0 {
95-
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
97+
if len(dns.SearchDomains) > 0 {
98+
rc.OverrideSearch(dns.SearchDomains)
9699
}
97-
if len(dns.Options) == 0 {
98-
dnsOptions = resolvconf.GetOptions(dt)
100+
if len(dns.Options) > 0 {
101+
rc.OverrideOptions(dns.Options)
99102
}
103+
}
100104

101-
f, err := resolvconf.Build(tmpPath, dnsNameservers, dnsSearchDomains, dnsOptions)
102-
if err != nil {
103-
return struct{}{}, errors.WithStack(err)
104-
}
105-
dt = f.Content
105+
if netMode != pb.NetMode_HOST || len(rc.NameServers()) == 0 {
106+
rc.TransformForLegacyNw(true)
106107
}
107108

108-
if netMode != pb.NetMode_HOST || len(resolvconf.GetNameservers(dt, resolvconf.IP)) == 0 {
109-
f, err := resolvconf.FilterResolvDNS(dt, true)
110-
if err != nil {
111-
return struct{}{}, errors.WithStack(err)
112-
}
113-
dt = f.Content
109+
tmpPath := p + ".tmp"
110+
111+
dt, err := rc.Generate(false)
112+
if err != nil {
113+
return struct{}{}, errors.WithStack(err)
114114
}
115115

116116
if err := os.WriteFile(tmpPath, dt, 0644); err != nil {
@@ -124,6 +124,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *user.IdentityMap
124124
}
125125
}
126126

127+
// TODO(thaJeztah): can we avoid the write -> chown -> rename?
127128
if err := os.Rename(tmpPath, p); err != nil {
128129
return struct{}{}, errors.WithStack(err)
129130
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/moby/go-archive v0.1.0
5353
github.com/moby/locker v1.0.1
5454
github.com/moby/patternmatcher v0.6.0
55+
github.com/moby/sys/atomicwriter v0.1.0
5556
github.com/moby/sys/mountinfo v0.7.2
5657
github.com/moby/sys/reexec v0.1.0
5758
github.com/moby/sys/signal v0.7.1
@@ -160,7 +161,6 @@ require (
160161
github.com/hashicorp/errwrap v1.1.0 // indirect
161162
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
162163
github.com/kylelemons/godebug v1.1.0 // indirect
163-
github.com/moby/sys/atomicwriter v0.1.0 // indirect
164164
github.com/moby/sys/mount v0.3.4 // indirect
165165
github.com/moby/sys/sequential v0.6.0 // indirect
166166
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect

0 commit comments

Comments
 (0)