Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #275 : use host dns with slirp4netns support in rootless mode #374

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions cmd/nerdctl/run.go
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/netutil/nettype"
"github.com/containerd/nerdctl/pkg/portutil"
"github.com/containerd/nerdctl/pkg/resolvconf"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/taskutil"
Expand Down Expand Up @@ -111,7 +112,6 @@ var runCommand = &cli.Command{
&cli.StringSliceFlag{
Name: "dns",
Usage: "Set custom DNS servers",
Value: cli.NewStringSlice("8.8.8.8", "1.1.1.1"),
},
&cli.StringSliceFlag{
Name: "publish",
Expand Down Expand Up @@ -432,7 +432,28 @@ func runAction(clicontext *cli.Context) error {
}

resolvConfPath := filepath.Join(stateDir, "resolv.conf")
if err := dnsutil.WriteResolvConfFile(resolvConfPath, strutil.DedupeStrSlice(clicontext.StringSlice("dns"))); err != nil {
conf, err := resolvconf.Get()
if err != nil {
return err
}
slirp4Dns := []string{}
if rootlessutil.IsRootlessChild() {
slirp4Dns, err = dnsutil.GetSlirp4netnsDns()
if err != nil {
return err
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, but I guess we do not need to detect the slirp4netns DNS.

We can just use /etc/resolv.conf (/run/systemd/resolve/resolv.conf) for both rootful and rootless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, in case of rootless we run in the mount ns of rootlesskit thus having the /etc/resolv.conf containing the slirp dns right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess we can merge this right now and revisit later.

conf, err = resolvconf.FilterResolvDNS(conf.Content, true)
if err != nil {
return err
}
searchDomains := resolvconf.GetSearchDomains(conf.Content)
dnsOptions := resolvconf.GetOptions(conf.Content)
nameServers := strutil.DedupeStrSlice(clicontext.StringSlice("dns"))
if len(nameServers) == 0 {
nameServers = resolvconf.GetNameservers(conf.Content, resolvconf.IPv4)
}
if _, err := resolvconf.Build(resolvConfPath, append(slirp4Dns, nameServers...), searchDomains, dnsOptions); err != nil {
return err
}
// the content of /etc/hosts is created in OCI Hook
Expand Down
30 changes: 15 additions & 15 deletions pkg/dnsutil/dnsutil.go
Expand Up @@ -17,25 +17,25 @@
package dnsutil

import (
"bytes"
"io/ioutil"
"net"
"context"

"github.com/pkg/errors"
"github.com/containerd/nerdctl/pkg/rootlessutil"
)

func WriteResolvConfFile(path string, dns []string) error {
var b bytes.Buffer
if _, err := b.Write([]byte("search localdomain\n")); err != nil {
return err
func GetSlirp4netnsDns() ([]string, error) {
var dns []string
rkClient, err := rootlessutil.NewRootlessKitClient()
if err != nil {
return dns, err
}
for _, entry := range dns {
if net.ParseIP(entry) == nil {
return errors.Errorf("invalid dns %q", entry)
}
if _, err := b.Write([]byte("nameserver " + entry + "\n")); err != nil {
return err
info, err := rkClient.Info(context.TODO())
if err != nil {
return dns, err
}
if info != nil && info.NetworkDriver != nil {
for _, dnsIp := range info.NetworkDriver.DNS {
dns = append(dns, dnsIp.String())
}
}
return ioutil.WriteFile(path, b.Bytes(), 0644)
return dns, nil
}