diff --git a/model/cluster_discovery.go b/model/cluster_discovery.go index 796d3dda04..42361f80b1 100644 --- a/model/cluster_discovery.go +++ b/model/cluster_discovery.go @@ -46,10 +46,10 @@ func (o *ClusterDiscovery) AutoFillHostname() { } } -func (o *ClusterDiscovery) AutoFillIpAddress() { +func (o *ClusterDiscovery) AutoFillIpAddress(iface string) { // attempt to set the hostname to the first non-local IP address if len(o.Hostname) == 0 { - o.Hostname = GetServerIpAddress() + o.Hostname = GetServerIpAddress(iface) } } diff --git a/model/cluster_discovery_test.go b/model/cluster_discovery_test.go index bfbdbd303d..012f710639 100644 --- a/model/cluster_discovery_test.go +++ b/model/cluster_discovery_test.go @@ -53,7 +53,7 @@ func TestClusterDiscovery(t *testing.T) { o.Hostname = "" o.AutoFillHostname() - o.AutoFillIpAddress() + o.AutoFillIpAddress("") o.Hostname = "" - o.AutoFillIpAddress() + o.AutoFillIpAddress("") } diff --git a/model/config.go b/model/config.go index ae6dfb62f3..f114900b8d 100644 --- a/model/config.go +++ b/model/config.go @@ -672,6 +672,7 @@ type ClusterSettings struct { Enable *bool `restricted:"true"` ClusterName *string `restricted:"true"` OverrideHostname *string `restricted:"true"` + NetworkInterface *string `restricted:"true"` UseIpAddress *bool `restricted:"true"` UseExperimentalGossip *bool `restricted:"true"` ReadOnlyConfig *bool `restricted:"true"` @@ -695,6 +696,10 @@ func (s *ClusterSettings) SetDefaults() { s.OverrideHostname = NewString("") } + if s.NetworkInterface == nil { + s.NetworkInterface = NewString("") + } + if s.UseIpAddress == nil { s.UseIpAddress = NewBool(true) } diff --git a/model/utils.go b/model/utils.go index 8293966fbe..039e756c82 100644 --- a/model/utils.go +++ b/model/utils.go @@ -302,16 +302,35 @@ func StringFromJson(data io.Reader) string { } } -func GetServerIpAddress() string { - if addrs, err := net.InterfaceAddrs(); err != nil { - return "" +func GetServerIpAddress(iface string) string { + var addrs []net.Addr + if len(iface) == 0 { + var err error + addrs, err = net.InterfaceAddrs() + if err != nil { + return "" + } } else { - for _, addr := range addrs { - - if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() && !ip.IP.IsLinkLocalMulticast() { - if ip.IP.To4() != nil { - return ip.IP.String() + interfaces, err := net.Interfaces() + if err != nil { + return "" + } + for _, i := range interfaces { + if i.Name == iface { + addrs, err = i.Addrs() + if err != nil { + return "" } + break + } + } + } + + for _, addr := range addrs { + + if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() && !ip.IP.IsLinkLocalMulticast() { + if ip.IP.To4() != nil { + return ip.IP.String() } } } diff --git a/model/utils_test.go b/model/utils_test.go index 0b64fdf02b..22dc72859a 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -357,7 +357,7 @@ func TestIsValidAlphaNum(t *testing.T) { } func TestGetServerIpAddress(t *testing.T) { - if len(GetServerIpAddress()) == 0 { + if len(GetServerIpAddress("")) == 0 { t.Fatal("Should find local ip address") } }