libpod: make use of new pasta option from c/common

pasta added a new --map-guest-addr to option that maps a to the actual
host ip. This is exactly what we need for host.containers.internal
entry. So we now make use of this option by default but still have to
keep the exclude fallback because the option is very new and some
users/distros will not have it yet.

This also fixes an issue where the --dns-forward ip were not used when
using the bridge network mode, only useful when not using aardvark-dns
as this used the proper ips there already from the rootless netns
resolv.conf file.

Fixes #19213

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2024-08-28 14:10:08 +02:00
parent 2f858675b3
commit a1e6603133
3 changed files with 44 additions and 20 deletions

View File

@ -2139,11 +2139,13 @@ func (c *Container) addResolvConf() error {
if len(networkNameServers) == 0 || networkBackend != string(types.Netavark) {
keepHostServers = true
}
// first add the nameservers from the networks status
nameservers = networkNameServers
// pasta and slirp4netns have a built in DNS forwarder.
nameservers = c.addSpecialDNS(nameservers)
if len(networkNameServers) > 0 {
// add the nameservers from the networks status
nameservers = networkNameServers
} else {
// pasta and slirp4netns have a built in DNS forwarder.
nameservers = c.addSpecialDNS(nameservers)
}
}
// Set DNS search domains
@ -2306,8 +2308,13 @@ func (c *Container) addHosts() error {
}
var exclude []net.IP
var preferIP string
if c.pastaResult != nil {
exclude = c.pastaResult.IPAddresses
if len(c.pastaResult.MapGuestAddrIPs) > 0 {
// we used --map-guest-addr to setup pasta so prefer this address
preferIP = c.pastaResult.MapGuestAddrIPs[0]
}
} else if c.config.NetMode.IsBridge() {
// When running rootless we have to check the rootless netns ip addresses
// to not assign a ip that is already used in the rootless netns as it would
@ -2316,16 +2323,27 @@ func (c *Container) addHosts() error {
info, err := c.runtime.network.RootlessNetnsInfo()
if err == nil {
exclude = info.IPAddresses
if len(info.MapGuestIps) > 0 {
// we used --map-guest-addr to setup pasta so prefer this address
preferIP = info.MapGuestIps[0]
}
}
}
hostContainersInternalIP := etchosts.GetHostContainersInternalIP(etchosts.HostContainersInternalOptions{
Conf: c.runtime.config,
NetStatus: c.state.NetworkStatus,
NetworkInterface: c.runtime.network,
Exclude: exclude,
PreferIP: preferIP,
})
return etchosts.New(&etchosts.Params{
BaseFile: baseHostFile,
ExtraHosts: c.config.HostAdd,
ContainerIPs: containerIPsEntries,
HostContainersInternalIP: etchosts.GetHostContainersInternalIPExcluding(
c.runtime.config, c.state.NetworkStatus, c.runtime.network, exclude),
TargetFile: targetFile,
BaseFile: baseHostFile,
ExtraHosts: c.config.HostAdd,
ContainerIPs: containerIPsEntries,
HostContainersInternalIP: hostContainersInternalIP,
TargetFile: targetFile,
})
}