diff --git a/.changelog/246.txt b/.changelog/246.txt new file mode 100644 index 0000000..7868d44 --- /dev/null +++ b/.changelog/246.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +Fetch all IP addresses in a single stdlib call +``` diff --git a/providers/shared/network.go b/providers/shared/network.go index cd11acd..bed19f4 100644 --- a/providers/shared/network.go +++ b/providers/shared/network.go @@ -27,17 +27,20 @@ func Network() (ips, macs []string, err error) { return nil, nil, err } - ips = make([]string, 0, len(ifcs)) + // This function fetches all the addresses in a single syscall. Fetching addresses individually for each interface + // can be expensive when the host has a lot of interfaces. This usually happens when the host is doing virtualized + // networking for guests, in Kubernetes for example. + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil, nil, err + } + ips = make([]string, 0, len(addrs)) + for _, addr := range addrs { + ips = append(ips, addr.String()) + } + macs = make([]string, 0, len(ifcs)) for _, ifc := range ifcs { - addrs, err := ifc.Addrs() - if err != nil { - return nil, nil, err - } - for _, addr := range addrs { - ips = append(ips, addr.String()) - } - mac := ifc.HardwareAddr.String() if mac != "" { macs = append(macs, mac)