Skip to content

Commit

Permalink
Prefer IPv4 over v6 and global unicast IP by default in Hyper-V driver
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongcheng Lao <Zhongcheng.Lao@microsoft.com>
  • Loading branch information
laozc committed Nov 15, 2019
1 parent d3b4e7c commit d615d62
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
DefaultProtocol = iota
Default = iota
PreferIPv4
PreferIPv6
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewDriver(hostName, storePath string) *Driver {
MemSize: defaultMemory,
CPU: defaultCPU,
DisableDynamicMemory: defaultDisableDynamicMemory,
PreferredNetworkProtocol: DefaultProtocol,
PreferredNetworkProtocol: Default,
WaitTimeoutInSeconds: defaultWaitTimeoutInSeconds,
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func (d *Driver) Kill() error {
}

func isIPv4(address string) bool {
return strings.Count(address, ":") < 2
return strings.Count(address, ":") < 1
}

func (d *Driver) GetIP() (string, error) {
Expand Down Expand Up @@ -504,12 +504,23 @@ func (d *Driver) GetIP() (string, error) {
}

default:
var preferredIP string
for _, ipStr := range resp {
ip := net.ParseIP(ipStr)
if ip.IsGlobalUnicast() {
return ipStr, nil
if preferredIP == "" {
preferredIP = ipStr
}
if isIPv4(ipStr) && ip.To4() != nil {
preferredIP = ipStr
break
}
}
}

if preferredIP != "" {
return preferredIP, nil
}
}

return "", nil
Expand Down

0 comments on commit d615d62

Please sign in to comment.