Skip to content

Commit

Permalink
Make getLocalIP public function so Podman can use it
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Nov 22, 2021
1 parent 78a1d08 commit 9ff53ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
18 changes: 18 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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 ""
}
19 changes: 2 additions & 17 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")))
}

Expand Down

0 comments on commit 9ff53ad

Please sign in to comment.