Skip to content

Commit d615d62

Browse files
committed
Prefer IPv4 over v6 and global unicast IP by default in Hyper-V driver
Signed-off-by: Zhongcheng Lao <[email protected]>
1 parent d3b4e7c commit d615d62

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

drivers/hyperv/hyperv.go

Lines changed: 15 additions & 4 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) {
@@ -504,12 +504,23 @@ func (d *Driver) GetIP() (string, error) {
504504
}
505505

506506
default:
507+
var preferredIP string
507508
for _, ipStr := range resp {
508509
ip := net.ParseIP(ipStr)
509510
if ip.IsGlobalUnicast() {
510-
return ipStr, nil
511+
if preferredIP == "" {
512+
preferredIP = ipStr
513+
}
514+
if isIPv4(ipStr) && ip.To4() != nil {
515+
preferredIP = ipStr
516+
break
517+
}
511518
}
512519
}
520+
521+
if preferredIP != "" {
522+
return preferredIP, nil
523+
}
513524
}
514525

515526
return "", nil

0 commit comments

Comments
 (0)