MM-15325: Allowing to configure the device for detect the IP in HA clusters (#10917)

This commit is contained in:
Jesús Espino
2019-06-11 20:53:03 +02:00
committed by GitHub
parent 9445cb29f5
commit 969c032a1e
5 changed files with 37 additions and 13 deletions

View File

@@ -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)
}
}

View File

@@ -53,7 +53,7 @@ func TestClusterDiscovery(t *testing.T) {
o.Hostname = ""
o.AutoFillHostname()
o.AutoFillIpAddress()
o.AutoFillIpAddress("")
o.Hostname = ""
o.AutoFillIpAddress()
o.AutoFillIpAddress("")
}

View File

@@ -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)
}

View File

@@ -302,10 +302,30 @@ func StringFromJson(data io.Reader) string {
}
}
func GetServerIpAddress() string {
if addrs, err := net.InterfaceAddrs(); err != nil {
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 {
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() {
@@ -314,7 +334,6 @@ func GetServerIpAddress() string {
}
}
}
}
return ""
}

View File

@@ -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")
}
}