From 1d741379083685c45e2484e6a6946e39776b814a Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 22 Nov 2021 09:27:17 -0500 Subject: [PATCH] Make LocalIP public function so Podman can use it [NO NEW TESTS NEEDED] Signed-off-by: Daniel J Walsh --- run_linux.go | 18 +----------------- util/util.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/run_linux.go b/run_linux.go index ab70092df7a..5a20c0ab8f4 100644 --- a/run_linux.go +++ b/run_linux.go @@ -719,23 +719,7 @@ func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownO hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s %s\n", b.Container, hostname))) hosts.Write([]byte(fmt.Sprintf("::1 %s %s\n", b.Container, hostname))) - // 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 ip := getLocalIP(); ip != "" { + if ip := util.LocalIP(); ip != "" { hosts.Write([]byte(fmt.Sprintf("%s %s\n", ip, "host.containers.internal"))) } diff --git a/util/util.go b/util/util.go index c6f022d4ed7..7024a821f08 100644 --- a/util/util.go +++ b/util/util.go @@ -3,6 +3,7 @@ package util import ( "fmt" "io" + "net" "net/url" "os" "path/filepath" @@ -486,3 +487,20 @@ func VerifyTagName(imageSpec string) (types.ImageReference, error) { } return ref, nil } + +// LocalIP returns the non loopback local IP of the host +func LocalIP() 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 "" +}