Skip to content

Commit

Permalink
Merge pull request #3142 from thaJeztah/simplify_resolvconf
Browse files Browse the repository at this point in the history
executor/oci: GetResolvConf(): simplify handling of resolv.conf
  • Loading branch information
tonistiigi authored Jan 6, 2023
2 parents 10aa3b3 + f04dadd commit 30cd3b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
38 changes: 17 additions & 21 deletions executor/oci/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var notFirstRun bool
var lastNotEmpty bool

// overridden by tests
var resolvconfGet = resolvconf.Get
var resolvconfPath = resolvconf.Path

type DNSConfig struct {
Nameservers []string
Expand All @@ -39,7 +39,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
generate = true
}
if !generate {
fiMain, err := os.Stat(resolvconf.Path())
fiMain, err := os.Stat(resolvconfPath())
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
Expand All @@ -60,33 +60,30 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
return "", nil
}

var dt []byte
f, err := resolvconfGet()
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return "", err
}
} else {
dt = f.Content
dt, err := os.ReadFile(resolvconfPath())
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", err
}

var f *resolvconf.File
tmpPath := p + ".tmp"
if dns != nil {
var (
dnsNameservers = resolvconf.GetNameservers(dt, resolvconf.IP)
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
dnsOptions = resolvconf.GetOptions(dt)
dnsNameservers = dns.Nameservers
dnsSearchDomains = dns.SearchDomains
dnsOptions = dns.Options
)
if len(dns.Nameservers) > 0 {
dnsNameservers = dns.Nameservers
if len(dns.Nameservers) == 0 {
dnsNameservers = resolvconf.GetNameservers(dt, resolvconf.IP)
}
if len(dns.SearchDomains) > 0 {
dnsSearchDomains = dns.SearchDomains
if len(dns.SearchDomains) == 0 {
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
}
if len(dns.Options) > 0 {
dnsOptions = dns.Options
if len(dns.Options) == 0 {
dnsOptions = resolvconf.GetOptions(dt)
}

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

tmpPath := p + ".tmp"
if err := os.WriteFile(tmpPath, f.Content, 0644); err != nil {
return "", err
}
Expand Down
13 changes: 6 additions & 7 deletions executor/oci/resolvconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"os"
"testing"

"github.com/docker/docker/libnetwork/resolvconf"
"github.com/stretchr/testify/require"
)

// TestResolvConfNotExist modifies a global variable
// It must not run in parallel.
func TestResolvConfNotExist(t *testing.T) {
oldResolvconfGet := resolvconfGet
defer func() {
resolvconfGet = oldResolvconfGet
}()
resolvconfGet = func() (*resolvconf.File, error) {
return nil, os.ErrNotExist
oldResolvconfPath := resolvconfPath
t.Cleanup(func() {
resolvconfPath = oldResolvconfPath
})
resolvconfPath = func() string {
return "no-such-file"
}

defaultResolvConf := `
Expand Down

0 comments on commit 30cd3b4

Please sign in to comment.