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

Этот коммит содержится в:
Jesús Espino
2019-06-11 20:53:03 +02:00
коммит произвёл GitHub
родитель 9445cb29f5
Коммит 969c032a1e
5 изменённых файлов: 37 добавлений и 13 удалений

Просмотреть файл

@@ -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 // attempt to set the hostname to the first non-local IP address
if len(o.Hostname) == 0 { if len(o.Hostname) == 0 {
o.Hostname = GetServerIpAddress() o.Hostname = GetServerIpAddress(iface)
} }
} }

Просмотреть файл

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

Просмотреть файл

@@ -672,6 +672,7 @@ type ClusterSettings struct {
Enable *bool `restricted:"true"` Enable *bool `restricted:"true"`
ClusterName *string `restricted:"true"` ClusterName *string `restricted:"true"`
OverrideHostname *string `restricted:"true"` OverrideHostname *string `restricted:"true"`
NetworkInterface *string `restricted:"true"`
UseIpAddress *bool `restricted:"true"` UseIpAddress *bool `restricted:"true"`
UseExperimentalGossip *bool `restricted:"true"` UseExperimentalGossip *bool `restricted:"true"`
ReadOnlyConfig *bool `restricted:"true"` ReadOnlyConfig *bool `restricted:"true"`
@@ -695,6 +696,10 @@ func (s *ClusterSettings) SetDefaults() {
s.OverrideHostname = NewString("") s.OverrideHostname = NewString("")
} }
if s.NetworkInterface == nil {
s.NetworkInterface = NewString("")
}
if s.UseIpAddress == nil { if s.UseIpAddress == nil {
s.UseIpAddress = NewBool(true) s.UseIpAddress = NewBool(true)
} }

Просмотреть файл

@@ -302,16 +302,35 @@ func StringFromJson(data io.Reader) string {
} }
} }
func GetServerIpAddress() string { func GetServerIpAddress(iface string) string {
if addrs, err := net.InterfaceAddrs(); err != nil { var addrs []net.Addr
return "" if len(iface) == 0 {
var err error
addrs, err = net.InterfaceAddrs()
if err != nil {
return ""
}
} else { } else {
for _, addr := range addrs { interfaces, err := net.Interfaces()
if err != nil {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() && !ip.IP.IsLinkLocalMulticast() { return ""
if ip.IP.To4() != nil { }
return ip.IP.String() 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()
} }
} }
} }

Просмотреть файл

@@ -357,7 +357,7 @@ func TestIsValidAlphaNum(t *testing.T) {
} }
func TestGetServerIpAddress(t *testing.T) { func TestGetServerIpAddress(t *testing.T) {
if len(GetServerIpAddress()) == 0 { if len(GetServerIpAddress("")) == 0 {
t.Fatal("Should find local ip address") t.Fatal("Should find local ip address")
} }
} }