From 859053034a5cafeb584fc63bcd256ff6b8e9ae0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Mon, 28 Oct 2024 19:27:32 +0100 Subject: [PATCH 1/3] Fetch all ip addresses in a single syscall on Linux --- providers/shared/network.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/providers/shared/network.go b/providers/shared/network.go index cd11acd6..9b7bf6cb 100644 --- a/providers/shared/network.go +++ b/providers/shared/network.go @@ -29,15 +29,19 @@ func Network() (ips, macs []string, err error) { ips = make([]string, 0, len(ifcs)) 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()) - } + // 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 + } + for _, addr := range addrs { + ips = append(ips, addr.String()) + } + + for _, ifc := range ifcs { mac := ifc.HardwareAddr.String() if mac != "" { macs = append(macs, mac) From 33f83fe45d4f46c9333b72a67a2389ac2d5c558c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 29 Oct 2024 13:30:17 +0100 Subject: [PATCH 2/3] Add changelog entry --- .changelog/246.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/246.txt diff --git a/.changelog/246.txt b/.changelog/246.txt new file mode 100644 index 00000000..b58c4196 --- /dev/null +++ b/.changelog/246.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +Fetch all ip addresses in a single stdlib call +``` From 377cd35854ac4131739dde36bdcc4e300d1105c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 29 Oct 2024 17:31:00 +0100 Subject: [PATCH 3/3] Code Review fixes --- .changelog/246.txt | 2 +- providers/shared/network.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.changelog/246.txt b/.changelog/246.txt index b58c4196..7868d441 100644 --- a/.changelog/246.txt +++ b/.changelog/246.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -Fetch all ip addresses in a single stdlib call +Fetch all IP addresses in a single stdlib call ``` diff --git a/providers/shared/network.go b/providers/shared/network.go index 9b7bf6cb..bed19f4e 100644 --- a/providers/shared/network.go +++ b/providers/shared/network.go @@ -27,9 +27,6 @@ func Network() (ips, macs []string, err error) { return nil, nil, err } - ips = make([]string, 0, len(ifcs)) - macs = 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. @@ -37,10 +34,12 @@ func Network() (ips, macs []string, err error) { 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 { mac := ifc.HardwareAddr.String() if mac != "" {