fix host.containers.internal entry for macvlan networks

For ip/macvlan networks we cannot use the gateway as address for this
hostname. In this case the gateway is normally not on the host so we
just try to use a local ip instead.

[NO NEW TESTS NEEDED] We cannot run macvlan networks in CI.

Fixes #11351

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2022-01-11 15:30:38 +01:00
parent 0464011a8e
commit f04465bfe6
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
1 changed files with 35 additions and 18 deletions

View File

@ -2221,17 +2221,6 @@ func (c *Container) getHosts() string {
depCtr = c depCtr = c
} }
if depCtr != nil {
for _, status := range depCtr.getNetworkStatus() {
for _, netInt := range status.Interfaces {
for _, netAddress := range netInt.Subnets {
if netAddress.Gateway != nil {
hosts += fmt.Sprintf("%s host.containers.internal\n", netAddress.Gateway.String())
}
}
}
}
} else if c.config.NetMode.IsSlirp4netns() {
// getLocalIP returns the non loopback local IP of the host // getLocalIP returns the non loopback local IP of the host
getLocalIP := func() string { getLocalIP := func() string {
addrs, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
@ -2248,6 +2237,34 @@ func (c *Container) getHosts() string {
} }
return "" return ""
} }
if depCtr != nil {
host := ""
outer:
for net, status := range depCtr.getNetworkStatus() {
network, err := c.runtime.network.NetworkInspect(net)
// only add the host entry for bridge networks
// ip/macvlan gateway is normally not on the host
if err != nil || network.Driver != types.BridgeNetworkDriver {
continue
}
for _, netInt := range status.Interfaces {
for _, netAddress := range netInt.Subnets {
if netAddress.Gateway != nil {
host = fmt.Sprintf("%s host.containers.internal\n", netAddress.Gateway.String())
break outer
}
}
}
}
// if no bridge gw was found try to use a local ip
if host == "" {
if ip := getLocalIP(); ip != "" {
host = fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal")
}
}
hosts += host
} else if c.config.NetMode.IsSlirp4netns() {
if ip := getLocalIP(); ip != "" { if ip := getLocalIP(); ip != "" {
hosts += fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal") hosts += fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal")
} }