Skip to content

Commit f04dadd

Browse files
committed
executor/oci: GetResolvConf(): simplify handling of resolv.conf
We only need the content here, not the checksum, so simplifying the code by just using os.ReadFile(), using resolvconf.Path() for the location. Also reversing the logic for custom options; The existing code was always parsing the host's resolv.conf to read the nameservers, searchdomain and options, but those options were only needed if they were not configured in DNSConfig. This patch reverses the logic to only parse the resolv.conf if no options are present in the passed DNSConfig. There's some more optimisations to make (but changes in libnetwork are needed for that); resolvconf.FilterResolvDNS() still parse the resolv.conf file, even if we just generated it (in which case we're generating a resolv.conf, after which we're parsing the generated file again to update it, which is not ideal). Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent f2c41e1 commit f04dadd

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

executor/oci/resolvconf.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var notFirstRun bool
1616
var lastNotEmpty bool
1717

1818
// overridden by tests
19-
var resolvconfGet = resolvconf.Get
19+
var resolvconfPath = resolvconf.Path
2020

2121
type DNSConfig struct {
2222
Nameservers []string
@@ -39,7 +39,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
3939
generate = true
4040
}
4141
if !generate {
42-
fiMain, err := os.Stat(resolvconf.Path())
42+
fiMain, err := os.Stat(resolvconfPath())
4343
if err != nil {
4444
if !errors.Is(err, os.ErrNotExist) {
4545
return nil, err
@@ -60,33 +60,30 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
6060
return "", nil
6161
}
6262

63-
var dt []byte
64-
f, err := resolvconfGet()
65-
if err != nil {
66-
if !errors.Is(err, os.ErrNotExist) {
67-
return "", err
68-
}
69-
} else {
70-
dt = f.Content
63+
dt, err := os.ReadFile(resolvconfPath())
64+
if err != nil && !errors.Is(err, os.ErrNotExist) {
65+
return "", err
7166
}
7267

68+
var f *resolvconf.File
69+
tmpPath := p + ".tmp"
7370
if dns != nil {
7471
var (
75-
dnsNameservers = resolvconf.GetNameservers(dt, resolvconf.IP)
76-
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
77-
dnsOptions = resolvconf.GetOptions(dt)
72+
dnsNameservers = dns.Nameservers
73+
dnsSearchDomains = dns.SearchDomains
74+
dnsOptions = dns.Options
7875
)
79-
if len(dns.Nameservers) > 0 {
80-
dnsNameservers = dns.Nameservers
76+
if len(dns.Nameservers) == 0 {
77+
dnsNameservers = resolvconf.GetNameservers(dt, resolvconf.IP)
8178
}
82-
if len(dns.SearchDomains) > 0 {
83-
dnsSearchDomains = dns.SearchDomains
79+
if len(dns.SearchDomains) == 0 {
80+
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
8481
}
85-
if len(dns.Options) > 0 {
86-
dnsOptions = dns.Options
82+
if len(dns.Options) == 0 {
83+
dnsOptions = resolvconf.GetOptions(dt)
8784
}
8885

89-
f, err = resolvconf.Build(p+".tmp", dnsNameservers, dnsSearchDomains, dnsOptions)
86+
f, err = resolvconf.Build(tmpPath, dnsNameservers, dnsSearchDomains, dnsOptions)
9087
if err != nil {
9188
return "", err
9289
}
@@ -98,7 +95,6 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
9895
return "", err
9996
}
10097

101-
tmpPath := p + ".tmp"
10298
if err := os.WriteFile(tmpPath, f.Content, 0644); err != nil {
10399
return "", err
104100
}

executor/oci/resolvconf_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import (
55
"os"
66
"testing"
77

8-
"github.com/docker/docker/libnetwork/resolvconf"
98
"github.com/stretchr/testify/require"
109
)
1110

1211
// TestResolvConfNotExist modifies a global variable
1312
// It must not run in parallel.
1413
func TestResolvConfNotExist(t *testing.T) {
15-
oldResolvconfGet := resolvconfGet
16-
defer func() {
17-
resolvconfGet = oldResolvconfGet
18-
}()
19-
resolvconfGet = func() (*resolvconf.File, error) {
20-
return nil, os.ErrNotExist
14+
oldResolvconfPath := resolvconfPath
15+
t.Cleanup(func() {
16+
resolvconfPath = oldResolvconfPath
17+
})
18+
resolvconfPath = func() string {
19+
return "no-such-file"
2120
}
2221

2322
defaultResolvConf := `

0 commit comments

Comments
 (0)