-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #941 from hashicorp/f-interface-name
Use an interface name to find the bind addr for rpc, http and serf components
- Loading branch information
Showing
5 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"time" | ||
) | ||
|
||
// Returns a random stagger interval between 0 and the duration | ||
func randomStagger(intv time.Duration) time.Duration { | ||
return time.Duration(uint64(rand.Int63()) % uint64(intv)) | ||
} | ||
|
||
// IpOfDevice returns a routable ip addr of a device | ||
func ipOfDevice(name string) (net.IP, error) { | ||
intf, err := net.InterfaceByName(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addrs, err := intf.Addrs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(addrs) == 0 { | ||
return nil, fmt.Errorf("no ips were detected on the interface: %v", name) | ||
} | ||
|
||
// Iterating through the IPs configured for that device and returning the | ||
// the first ipv4 address configured. If no ipv4 addresses are configured, | ||
// we return the first ipv6 addr if any ipv6 addr is configured. | ||
var ipv6Addrs []net.IP | ||
for _, addr := range addrs { | ||
var ip net.IP | ||
switch v := (addr).(type) { | ||
case *net.IPNet: | ||
ip = v.IP | ||
if ip.To4() != nil { | ||
return ip, nil | ||
} | ||
if ip.To16() != nil { | ||
ipv6Addrs = append(ipv6Addrs, ip) | ||
continue | ||
} | ||
case *net.IPAddr: | ||
continue | ||
} | ||
} | ||
if len(ipv6Addrs) > 0 { | ||
return ipv6Addrs[0], nil | ||
} | ||
return nil, fmt.Errorf("no ips were detected on the interface: %v", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters