forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add host_containers_internal_ip to containers.conf
Set the ip for the host.containers.internal entry in the containers /etc/hosts file. This can be set to "none" to disable adding this entry. By default it will automatically choose the host ip. Also add a function to get the correct host.containers.internal ip. This should be used by podman and buildah and then passed to the New() function. Ref containers/podman#13224 Signed-off-by: Paul Holzinger <[email protected]>
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package etchosts | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/containers/common/libnetwork/types" | ||
"github.com/containers/common/libnetwork/util" | ||
"github.com/containers/common/pkg/config" | ||
) | ||
|
||
// GetHostContainersInternalIP return the host.containers.internal ip | ||
// if netStatus is not nil then networkInterface also must be non nil otherwise this function panics | ||
func GetHostContainersInternalIP(conf *config.Config, netStatus map[string]types.StatusBlock, networkInterface types.ContainerNetwork) string { | ||
switch conf.Containers.HostContainersInternalIP { | ||
case "": | ||
// if empty (default) we will automatically choose one below | ||
// if machine we let the gvproxy dns server handle the dns name so do not add it | ||
if conf.Engine.MachineEnabled { | ||
return "" | ||
} | ||
case "none": | ||
return "" | ||
default: | ||
return conf.Containers.HostContainersInternalIP | ||
} | ||
ip := "" | ||
for net, status := range netStatus { | ||
network, err := networkInterface.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 { | ||
if util.IsIPv4(netAddress.Gateway) { | ||
return netAddress.Gateway.String() | ||
} | ||
// ipv6 address but keep looking since we prefer to use ipv4 | ||
ip = netAddress.Gateway.String() | ||
} | ||
} | ||
} | ||
} | ||
if ip != "" { | ||
return ip | ||
} | ||
return getLocalIP() | ||
} | ||
|
||
// getLocalIP returns the non loopback local IP of the host | ||
func getLocalIP() string { | ||
addrs, err := net.InterfaceAddrs() | ||
if err != nil { | ||
return "" | ||
} | ||
ip := "" | ||
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.IsGlobalUnicast() { | ||
if util.IsIPv4(ipnet.IP) { | ||
return ipnet.IP.String() | ||
} | ||
// if ipv6 we keep looking for an ipv4 address | ||
ip = ipnet.IP.String() | ||
} | ||
} | ||
return ip | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters