From 11a46aac5392fd79b155851d12da3443ce7d2af2 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Wed, 28 Oct 2015 21:16:59 +0900 Subject: [PATCH] Assign IP when network device is specified --- client/fingerprint/network_unix.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/client/fingerprint/network_unix.go b/client/fingerprint/network_unix.go index 020c99516156..9a422ec01ba7 100644 --- a/client/fingerprint/network_unix.go +++ b/client/fingerprint/network_unix.go @@ -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") }