2017-12-11 02:18:18 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-12-11 02:18:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-06-10 23:35:03 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-12-11 02:18:18 +00:00
|
|
|
"github.com/docker/libnetwork/resolvconf"
|
2019-06-06 01:46:52 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-12-11 02:18:18 +00:00
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2020-04-19 05:17:47 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-12-11 02:18:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var g flightcontrol.Group
|
|
|
|
var notFirstRun bool
|
|
|
|
var lastNotEmpty bool
|
|
|
|
|
2019-10-10 23:15:25 +00:00
|
|
|
// overridden by tests
|
|
|
|
var resolvconfGet = resolvconf.Get
|
|
|
|
|
2019-06-06 01:46:52 +00:00
|
|
|
type DNSConfig struct {
|
|
|
|
Nameservers []string
|
|
|
|
Options []string
|
|
|
|
SearchDomains []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.IdentityMapping, dns *DNSConfig) (string, error) {
|
2017-12-11 02:18:18 +00:00
|
|
|
p := filepath.Join(stateDir, "resolv.conf")
|
|
|
|
_, err := g.Do(ctx, stateDir, func(ctx context.Context) (interface{}, error) {
|
|
|
|
generate := !notFirstRun
|
|
|
|
notFirstRun = true
|
|
|
|
|
|
|
|
if !generate {
|
|
|
|
fi, err := os.Stat(p)
|
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2017-12-11 02:18:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
generate = true
|
|
|
|
}
|
|
|
|
if !generate {
|
2019-05-31 20:37:40 +00:00
|
|
|
fiMain, err := os.Stat(resolvconf.Path())
|
2017-12-11 02:18:18 +00:00
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2017-12-11 02:18:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lastNotEmpty {
|
|
|
|
generate = true
|
|
|
|
lastNotEmpty = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fi.ModTime().Before(fiMain.ModTime()) {
|
|
|
|
generate = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !generate {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var dt []byte
|
2019-10-10 23:15:25 +00:00
|
|
|
f, err := resolvconfGet()
|
2017-12-11 02:18:18 +00:00
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2017-12-11 02:18:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dt = f.Content
|
|
|
|
}
|
|
|
|
|
2019-06-06 01:46:52 +00:00
|
|
|
if dns != nil {
|
|
|
|
var (
|
|
|
|
dnsNameservers = resolvconf.GetNameservers(dt, types.IP)
|
|
|
|
dnsSearchDomains = resolvconf.GetSearchDomains(dt)
|
|
|
|
dnsOptions = resolvconf.GetOptions(dt)
|
|
|
|
)
|
|
|
|
if len(dns.Nameservers) > 0 {
|
|
|
|
dnsNameservers = dns.Nameservers
|
|
|
|
}
|
|
|
|
if len(dns.SearchDomains) > 0 {
|
|
|
|
dnsSearchDomains = dns.SearchDomains
|
|
|
|
}
|
|
|
|
if len(dns.Options) > 0 {
|
|
|
|
dnsOptions = dns.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = resolvconf.Build(p+".tmp", dnsNameservers, dnsSearchDomains, dnsOptions)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-10-10 23:16:09 +00:00
|
|
|
dt = f.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = resolvconf.FilterResolvDNS(dt, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-12-11 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:35:03 +00:00
|
|
|
tmpPath := p + ".tmp"
|
|
|
|
if err := ioutil.WriteFile(tmpPath, f.Content, 0644); err != nil {
|
2017-12-11 02:18:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-06-10 23:35:03 +00:00
|
|
|
if idmap != nil {
|
|
|
|
root := idmap.RootPair()
|
|
|
|
if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmpPath, p); err != nil {
|
2017-12-11 02:18:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|