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

fix: check network mode when choosing resolv.conf #5207

Merged
merged 1 commit into from
Sep 2, 2024

Conversation

Ka0o0
Copy link
Contributor

@Ka0o0 Ka0o0 commented Aug 1, 2024

Hi,

this fixes #2404 . Instead of always checking for systemd-resolved it's sufficient to do so only if the run does not happen within the host networking mode.

I was a bit unsure what happens here and if it might get problematic in consecutive builds with different networking modes. Maybe someone can explain this to me.

BR Kai

@github-actions github-actions bot added area/testing area/dependencies Pull requests that update a dependency file area/executor labels Aug 1, 2024
// resolver it's also possible to use nameservers on the host's loopback
// interface. Once legacy networking is removed, this can always return
// defaultPath.
func Path() string {
func Path(netMode pb.NetMode) string {
Copy link
Member

Choose a reason for hiding this comment

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

Changes in vendor/github.com/docker/docker should be first done in https://github.com/moby/moby repo and vendored back here.

cc @robmry @thaJeztah

Copy link

Choose a reason for hiding this comment

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

We need to figure out a way for build containers to use something similar to the internal resolver used by custom bridge networks (127.0.0.11) - it proxies DNS requests from the container, making upstream requests from the host's network namespace when necessary.

(At the moment, we use addresses from the host's resolv.conf, but they may not work in the container's network namespace - including nameservers with localhost addresses like systemd's 127.0.0.53, or IPv6 nameservers in an IPv4-only build container. If there are no nameservers the container can use, we fall back to Google's DNS.)

Once that's done, we'll be able to completely get rid of the code that looks at /run/systemd/resolve/resolv.conf (and the Google DNS fallbacks, and the default-bridge network would be able to use the internal resolver, making it more like other networks).

However, once that's done, the build container will still need a special-case for host networking. (It just needs a copy of the host's resolv.conf in that case. There's no need for a DNS proxy, because there's no container namespace to escape from).

So, for this fix, it might be best to deal with that special case in executor/oci/resolvconf.go now - for host networking, just don't call resolvconf.Path and use /etc/resolv.conf? (Then, no need to update vendored code.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea. I've refactored the code and tested it which seems to work the same.

@github-actions github-actions bot removed the area/dependencies Pull requests that update a dependency file label Aug 7, 2024
@Ka0o0 Ka0o0 requested a review from robmry August 9, 2024 12:39
Copy link

@robmry robmry left a comment

Choose a reason for hiding this comment

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

That's simplified things a bit - thank you!

The commits will need squashing, and could you update the commit message to explain the change? (Just something like "In host networking mode, unconditionally use "/etc/resolv.conf" rather than systemd's config".)

I was a bit unsure what happens here and if it might get problematic in consecutive builds with different networking modes. Maybe someone can explain this to me.

Looks like it's ok because it already had to deal with removing/not-removing localhost nameservers, so the output path depends on netMode ...

p := filepath.Join(stateDir, "resolv.conf")
if netMode == pb.NetMode_HOST {
p = filepath.Join(stateDir, "resolv-host.conf")
}

... and it's covered by TestRegenerateResolvconfToRemoveLocalDNS / TestRegenerateResolvconfToAddLocalDNS. But, that needs checking by someone who knows this code better than me.

(Not directly related to this change - but lastNotEmpty is never set to true, so it doesn't do anything. Probably not causing any issues, but I'm not sure what the intention was. I think if the input file is removed, unlikely as that is, an old output file will be left as-is rather than getting cleared - maybe it was something to do with that?)

Update: "I think if the input file is removed, unlikely as that is, an old output file will be left as-is rather than getting cleared" ... oh, not that - because an empty path is returned in that case. Maybe lastNotEmpty could be removed. (Not needed as part of this change though.)

executor/oci/resolvconf.go Outdated Show resolved Hide resolved
@@ -111,7 +111,7 @@ func TestResolvConf(t *testing.T) {
t.Cleanup(func() {
resolvconfPath = oldResolvconfPath
})
resolvconfPath = func() string {
resolvconfPath = func(netMode pb.NetMode) string {
Copy link

Choose a reason for hiding this comment

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

It's perhaps worth checking this is called with the expected netMode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I've had to move the whole block into the iterator over the network modes. Can you please check if that's okay like this?

Signed-off-by: Kai Takac <kai.takac@gmail.com>
@Ka0o0 Ka0o0 requested a review from robmry August 12, 2024 07:20
Copy link

@robmry robmry left a comment

Choose a reason for hiding this comment

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

LGTM!

@crazy-max crazy-max added this to the v0.16.0 milestone Aug 12, 2024
@Ka0o0
Copy link
Contributor Author

Ka0o0 commented Aug 12, 2024

Cool, thanks for the review!

@crazy-max crazy-max merged commit d09677c into moby:master Sep 2, 2024
79 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support different DNS resolver if host network is used
3 participants