Skip to content

Commit 6e1f733

Browse files
committed
Prefer IPv4 over v6 and global unicast IP by default in Hyper-V driver
Signed-off-by: Zhongcheng Lao <Zhongcheng.Lao@microsoft.com>
1 parent d3b4e7c commit 6e1f733

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

drivers/hyperv/hyperv.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
DefaultProtocol = iota
21+
Default = iota
2222
PreferIPv4
2323
PreferIPv6
2424
)
@@ -58,7 +58,7 @@ func NewDriver(hostName, storePath string) *Driver {
5858
MemSize: defaultMemory,
5959
CPU: defaultCPU,
6060
DisableDynamicMemory: defaultDisableDynamicMemory,
61-
PreferredNetworkProtocol: DefaultProtocol,
61+
PreferredNetworkProtocol: Default,
6262
WaitTimeoutInSeconds: defaultWaitTimeoutInSeconds,
6363
}
6464
}
@@ -464,7 +464,7 @@ func (d *Driver) Kill() error {
464464
}
465465

466466
func isIPv4(address string) bool {
467-
return strings.Count(address, ":") < 2
467+
return strings.Count(address, ":") < 1
468468
}
469469

470470
func (d *Driver) GetIP() (string, error) {
@@ -486,33 +486,56 @@ func (d *Driver) GetIP() (string, error) {
486486
return "", fmt.Errorf("IP not found")
487487
}
488488

489+
var preferredIP string
489490
switch d.PreferredNetworkProtocol {
490491
case PreferIPv4:
491492
for _, ipStr := range resp {
492493
ip := net.ParseIP(ipStr)
493-
if isIPv4(ipStr) && ip.To4() != nil && ip.IsGlobalUnicast() {
494-
return ipStr, nil
494+
if isIPv4(ipStr) && ip.To4() != nil {
495+
if preferredIP == "" {
496+
preferredIP = ipStr
497+
}
498+
if ip.IsGlobalUnicast() {
499+
preferredIP = ipStr
500+
break
501+
}
495502
}
496503
}
497504

498505
case PreferIPv6:
499506
for _, ipStr := range resp {
500507
ip := net.ParseIP(ipStr)
501-
if !isIPv4(ipStr) && ip.IsGlobalUnicast() {
502-
return ipStr, nil
508+
if !isIPv4(ipStr) {
509+
if preferredIP == "" {
510+
preferredIP = ipStr
511+
}
512+
if ip.IsGlobalUnicast() {
513+
preferredIP = ipStr
514+
break
515+
}
503516
}
504517
}
505518

506519
default:
507520
for _, ipStr := range resp {
508521
ip := net.ParseIP(ipStr)
509522
if ip.IsGlobalUnicast() {
510-
return ipStr, nil
523+
if preferredIP == "" {
524+
preferredIP = ipStr
525+
}
526+
if isIPv4(ipStr) && ip.To4() != nil {
527+
preferredIP = ipStr
528+
break
529+
}
511530
}
512531
}
532+
533+
if preferredIP == "" {
534+
preferredIP = resp[0]
535+
}
513536
}
514537

515-
return "", nil
538+
return preferredIP, nil
516539
}
517540

518541
func (d *Driver) publicSSHKeyPath() string {

0 commit comments

Comments
 (0)