mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix: ipv6-only mode compatibility fixes (#23031)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -31,6 +31,7 @@ import (
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/util/procutils"
|
||||
)
|
||||
@@ -358,6 +359,25 @@ func (hf HostsFile) String() string {
|
||||
return ret
|
||||
}
|
||||
|
||||
func FormatHostsFile(content string, ips []string, hostname, hostdomain string) string {
|
||||
hf := make(HostsFile, 0)
|
||||
hf.Parse(content)
|
||||
hf.Add("127.0.0.1", "localhost")
|
||||
isV6 := false
|
||||
for _, ip := range ips {
|
||||
if regutils.MatchIP6Addr(ip) {
|
||||
isV6 = true
|
||||
}
|
||||
}
|
||||
if isV6 {
|
||||
hf.Add("::1", "localhost", "ip6-localhost", "ip6-loopback")
|
||||
}
|
||||
for _, ip := range ips {
|
||||
hf.Add(ip, hostdomain, hostname)
|
||||
}
|
||||
return hf.String()
|
||||
}
|
||||
|
||||
func FsFormatToDiskType(fsFormat string) string {
|
||||
switch {
|
||||
case fsFormat == "swap":
|
||||
|
||||
+36
-11
@@ -22,36 +22,54 @@ import (
|
||||
type SNicInfo struct {
|
||||
IpAddr string
|
||||
Gateway string
|
||||
|
||||
Ip6Addr string
|
||||
Gateway6 string
|
||||
|
||||
MacAddr string
|
||||
|
||||
IsDefault bool
|
||||
}
|
||||
|
||||
type SNicInfoList []SNicInfo
|
||||
|
||||
func (nics SNicInfoList) Add(ip, mac, gw string) SNicInfoList {
|
||||
func (nics SNicInfoList) Add(mac, ip, gw, ip6, gw6 string, isDefault bool) SNicInfoList {
|
||||
return append(nics, SNicInfo{
|
||||
IpAddr: ip,
|
||||
MacAddr: mac,
|
||||
Gateway: gw,
|
||||
|
||||
Ip6Addr: ip6,
|
||||
Gateway6: gw6,
|
||||
|
||||
IsDefault: isDefault,
|
||||
})
|
||||
}
|
||||
|
||||
func (nics SNicInfoList) FindDefaultNicMac() (string, int) {
|
||||
var intMac, exitMac string
|
||||
var intIdx, exitIdx int
|
||||
var intMac, exitMac, defMac string
|
||||
var intIdx, exitIdx, defIdx int
|
||||
for i, nic := range nics {
|
||||
if len(nic.IpAddr) == 0 {
|
||||
if len(nic.IpAddr) == 0 && len(nic.Ip6Addr) == 0 {
|
||||
continue
|
||||
}
|
||||
if len(nic.Gateway) == 0 {
|
||||
if len(nic.IpAddr) > 0 && len(nic.Gateway) == 0 {
|
||||
continue
|
||||
}
|
||||
addr, err := netutils.NewIPV4Addr(nic.IpAddr)
|
||||
if err != nil {
|
||||
log.Errorf("NewIPV4Addr %s fail %s", nic.IpAddr, err)
|
||||
if len(nic.Ip6Addr) > 0 && len(nic.Gateway6) == 0 {
|
||||
continue
|
||||
}
|
||||
if len(exitMac) == 0 || len(intMac) == 0 {
|
||||
isExit := netutils.IsExitAddress(addr)
|
||||
|
||||
var isExit bool
|
||||
if len(nic.IpAddr) > 0 {
|
||||
addr, err := netutils.NewIPV4Addr(nic.IpAddr)
|
||||
if err != nil {
|
||||
log.Errorf("NewIPV4Addr %s fail %s", nic.IpAddr, err)
|
||||
continue
|
||||
}
|
||||
isExit = netutils.IsExitAddress(addr)
|
||||
}
|
||||
if len(exitMac) == 0 || len(intMac) == 0 || len(defMac) == 0 {
|
||||
if len(exitMac) == 0 && isExit {
|
||||
exitMac = nic.MacAddr
|
||||
exitIdx = i
|
||||
@@ -60,13 +78,20 @@ func (nics SNicInfoList) FindDefaultNicMac() (string, int) {
|
||||
intMac = nic.MacAddr
|
||||
intIdx = i
|
||||
}
|
||||
} else if len(exitMac) > 0 && len(intMac) > 0 {
|
||||
if len(defMac) == 0 && nic.IsDefault && !isExit {
|
||||
defMac = nic.MacAddr
|
||||
defIdx = i
|
||||
}
|
||||
} else if len(exitMac) > 0 && len(intMac) > 0 && len(defMac) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(exitMac) > 0 {
|
||||
return exitMac, exitIdx
|
||||
}
|
||||
if len(defMac) > 0 {
|
||||
return defMac, defIdx
|
||||
}
|
||||
if len(intMac) > 0 {
|
||||
return intMac, intIdx
|
||||
}
|
||||
|
||||
@@ -26,40 +26,64 @@ func TestFindDefaultNic(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
nics: [][]string{
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1"},
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
|
||||
},
|
||||
mac: "00:22:00:00:00:01",
|
||||
index: 0,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1"},
|
||||
{"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1"},
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
|
||||
{"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "false"},
|
||||
},
|
||||
mac: "00:22:00:00:00:01",
|
||||
index: 0,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1"},
|
||||
{"202.168.202.147", "00:22:00:00:00:02", "202.168.202.1"},
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
|
||||
{"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "true"},
|
||||
},
|
||||
mac: "00:22:00:00:00:02",
|
||||
index: 1,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"202.168.202.147", "00:22:00:00:00:01", "202.168.202.1"},
|
||||
{"192.168.202.147", "00:22:00:00:00:02", "192.168.202.1"},
|
||||
{"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
|
||||
{"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "3ffe:3200:ff::1", "true"},
|
||||
},
|
||||
mac: "00:22:00:00:00:02",
|
||||
index: 1,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
|
||||
{"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "", "true"},
|
||||
},
|
||||
mac: "00:22:00:00:00:01",
|
||||
index: 0,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1"},
|
||||
{"", "00:22:00:00:00:02", ""},
|
||||
{"202.168.202.147", "00:22:00:00:00:03", "202.168.202.1"},
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
|
||||
{"202.168.202.147", "00:22:00:00:00:02", "202.168.202.1", "", "", "false"},
|
||||
},
|
||||
mac: "00:22:00:00:00:02",
|
||||
index: 1,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"202.168.202.147", "00:22:00:00:00:01", "202.168.202.1", "", "", "false"},
|
||||
{"192.168.202.147", "00:22:00:00:00:02", "192.168.202.1", "", "", "false"},
|
||||
},
|
||||
mac: "00:22:00:00:00:01",
|
||||
index: 0,
|
||||
},
|
||||
{
|
||||
nics: [][]string{
|
||||
{"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
|
||||
{"", "00:22:00:00:00:02", "", "", "", "false"},
|
||||
{"202.168.202.147", "00:22:00:00:00:03", "202.168.202.1", "", "", "false"},
|
||||
},
|
||||
mac: "00:22:00:00:00:03",
|
||||
index: 2,
|
||||
@@ -68,7 +92,7 @@ func TestFindDefaultNic(t *testing.T) {
|
||||
for _, c := range cases {
|
||||
nics := SNicInfoList{}
|
||||
for _, n := range c.nics {
|
||||
nics = nics.Add(n[0], n[1], n[2])
|
||||
nics = nics.Add(n[1], n[0], n[2], n[3], n[4], n[5] == "true")
|
||||
}
|
||||
|
||||
gotMac, gotIdx := nics.FindDefaultNicMac()
|
||||
|
||||
@@ -125,7 +125,7 @@ func NormalizeDbHost(db string) (string, error) {
|
||||
if len(addrs) == 0 {
|
||||
return "", fmt.Errorf("dns lookup (%s) returned empty result", host)
|
||||
}
|
||||
return "tcp:" + addrs[0] + ":" + port, nil
|
||||
return "tcp:" + net.JoinHostPort(addrs[0], port), nil
|
||||
}
|
||||
}
|
||||
return db, nil
|
||||
|
||||
Reference in New Issue
Block a user