Assign IP when network device is specified
This commit is contained in:
parent
0e5a3c273b
commit
a79ae3c546
|
@ -34,11 +34,13 @@ func (f *NetworkFingerprint) Fingerprint(cfg *config.Config, node *structs.Node)
|
|||
// newNetwork is populated and addded to the Nodes resources
|
||||
newNetwork := &structs.NetworkResource{}
|
||||
defaultDevice := ""
|
||||
ip := ""
|
||||
|
||||
// 1. Use user-defined network device
|
||||
// 2. Use first interface found in the system for non-dev mode. (dev mode uses lo by default.)
|
||||
if cfg.NetworkInterface != "" {
|
||||
defaultDevice = cfg.NetworkInterface
|
||||
ip = f.ipAddress(defaultDevice)
|
||||
} else {
|
||||
|
||||
intfs, err := net.Interfaces()
|
||||
|
@ -48,19 +50,19 @@ func (f *NetworkFingerprint) Fingerprint(cfg *config.Config, node *structs.Node)
|
|||
|
||||
for _, i := range intfs {
|
||||
if (i.Flags&net.FlagUp != 0) && (i.Flags&(net.FlagLoopback|net.FlagPointToPoint) == 0) {
|
||||
if ip := f.ipAddress(i.Name); ip != "" {
|
||||
if ip = f.ipAddress(i.Name); ip != "" {
|
||||
defaultDevice = i.Name
|
||||
node.Attributes["network.ip-address"] = ip
|
||||
newNetwork.IP = ip
|
||||
newNetwork.CIDR = newNetwork.IP + "/32"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if defaultDevice != "" {
|
||||
if (defaultDevice != "") && (ip != "") {
|
||||
newNetwork.Device = defaultDevice
|
||||
node.Attributes["network.ip-address"] = ip
|
||||
newNetwork.IP = ip
|
||||
newNetwork.CIDR = newNetwork.IP + "/32"
|
||||
} else {
|
||||
return false, fmt.Errorf("Unable to find any network interface which has IP address")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue