summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2022-01-11 14:21:58 -0500
committerGitHub <noreply@github.com>2022-01-11 14:21:58 -0500
commitc4142ce0cfff792092bf420950b1985058cc241c (patch)
treec7f547f3519e192e8190c1410f1233e61bc48e89
parent4db1affda6267fc664941691fa4c724f953448ba (diff)
parentf04465bfe688e91f6097ec04e4af93b7d78a6908 (diff)
downloadpodman-c4142ce0cfff792092bf420950b1985058cc241c.tar.gz
podman-c4142ce0cfff792092bf420950b1985058cc241c.tar.bz2
podman-c4142ce0cfff792092bf420950b1985058cc241c.zip
Merge pull request #12807 from Luap99/macvlan-hostip
fix host.containers.internal entry for macvlan networks
-rw-r--r--libpod/container_internal_linux.go51
1 files changed, 34 insertions, 17 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
index 7745646b6..28d961e4b 100644
--- a/libpod/container_internal_linux.go
+++ b/libpod/container_internal_linux.go
@@ -2221,33 +2221,50 @@ func (c *Container) getHosts() string {
depCtr = c
}
+ // getLocalIP returns the non loopback local IP of the host
+ getLocalIP := func() string {
+ addrs, err := net.InterfaceAddrs()
+ if err != nil {
+ return ""
+ }
+ for _, address := range addrs {
+ // check the address type and if it is not a loopback the display it
+ if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
+ if ipnet.IP.To4() != nil {
+ return ipnet.IP.String()
+ }
+ }
+ }
+ return ""
+ }
+
if depCtr != nil {
- for _, status := range depCtr.getNetworkStatus() {
+ 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 {
- hosts += fmt.Sprintf("%s host.containers.internal\n", netAddress.Gateway.String())
+ host = fmt.Sprintf("%s host.containers.internal\n", netAddress.Gateway.String())
+ break outer
}
}
}
}
- } else if c.config.NetMode.IsSlirp4netns() {
- // getLocalIP returns the non loopback local IP of the host
- getLocalIP := func() string {
- addrs, err := net.InterfaceAddrs()
- if err != nil {
- return ""
- }
- for _, address := range addrs {
- // check the address type and if it is not a loopback the display it
- if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
- if ipnet.IP.To4() != nil {
- return ipnet.IP.String()
- }
- }
+ // 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")
}
- return ""
}
+ hosts += host
+ } else if c.config.NetMode.IsSlirp4netns() {
if ip := getLocalIP(); ip != "" {
hosts += fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal")
}