Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all ip addresses in a single stdlib call #246

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/246.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Fetch all IP addresses in a single stdlib call
```
21 changes: 12 additions & 9 deletions providers/shared/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading