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 <[email protected]>
  • Loading branch information
laozc committed Nov 15, 2019
1 parent 345c295 commit 422f294
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 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 All @@ -486,33 +486,56 @@ func (d *Driver) GetIP() (string, error) {
return "", fmt.Errorf("IP not found")
}

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

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

default:
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 == "" {
preferredIP = resp[0]
}
}

return "", nil
return preferredIP, nil
}

func (d *Driver) publicSSHKeyPath() string {
Expand Down

0 comments on commit 422f294

Please sign in to comment.