Skip to content

Commit

Permalink
fix: check network mode when choosing resolv.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
Ka0o0 committed Aug 7, 2024
1 parent 725ba18 commit 5b9c07b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions executor/oci/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ var notFirstRun bool
var lastNotEmpty bool

// overridden by tests
var resolvconfPath = resolvconf.Path
var resolvconfPath = func(netMode pb.NetMode) string {
// The implementation of resolvconf.Path checks if systemd resolved is activated and chooses the internal resolv.conf (/run/systemd/resolve/resolv.conf) in such a case - see resolvconf_path.go of libnetwork.
// This, however, can be problematic, see https://github.com/moby/buildkit/issues/2404 and is not necessary in case the networking mode is set to host since the locally (127.0.0.53) running resolved daemon is accessible from inside a host networked container.
// For details of the implementation see https://github.com/moby/buildkit/pull/5207#discussion_r1705362230.
if netMode == pb.NetMode_HOST {
return "/etc/resolv.conf"
}
return resolvconf.Path()
}

type DNSConfig struct {
Nameservers []string
Expand All @@ -44,7 +52,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
generate = true
}
if !generate {
fiMain, err := os.Stat(resolvconfPath())
fiMain, err := os.Stat(resolvconfPath(netMode))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return struct{}{}, err
Expand All @@ -63,7 +71,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
return struct{}{}, nil
}

dt, err := os.ReadFile(resolvconfPath())
dt, err := os.ReadFile(resolvconfPath(netMode))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return struct{}{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/oci/resolvconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestResolvConf(t *testing.T) {
t.Cleanup(func() {
resolvconfPath = oldResolvconfPath
})
resolvconfPath = func() string {
resolvconfPath = func(netMode pb.NetMode) string {
if tt.dt == nil {
return "no-such-file"
}
Expand Down

0 comments on commit 5b9c07b

Please sign in to comment.