From 9ff53ad835482750a486f01746f79e8567e76cc0 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 22 Nov 2021 09:27:17 -0500 Subject: [PATCH] Make getLocalIP public function so Podman can use it [NO NEW TESTS NEEDED] Signed-off-by: Daniel J Walsh --- pkg/util/util.go | 18 ++++++++++++++++++ run_linux.go | 19 ++----------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index 209ad9544a8..bef7299ba98 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -2,6 +2,7 @@ package util import ( "io/ioutil" + "net" "os" "path/filepath" "strings" @@ -79,3 +80,20 @@ func DiscoverContainerfile(path string) (foundCtrFile string, err error) { return foundCtrFile, 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 "" +} diff --git a/run_linux.go b/run_linux.go index ab70092df7a..1c0eb5d4eb2 100644 --- a/run_linux.go +++ b/run_linux.go @@ -29,6 +29,7 @@ import ( "github.com/containers/buildah/pkg/overlay" "github.com/containers/buildah/pkg/parse" "github.com/containers/buildah/pkg/sshagent" + butil "github.com/containers/buildah/pkg/util" "github.com/containers/buildah/util" "github.com/containers/common/pkg/capabilities" "github.com/containers/common/pkg/chown" @@ -719,23 +720,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 := butil.LocalIP(); ip != "" { hosts.Write([]byte(fmt.Sprintf("%s %s\n", ip, "host.containers.internal"))) }