Skip to content

Commit

Permalink
Adds preferable network protocol for 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 6, 2019
1 parent 21bd2f5 commit cfb21a3
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import (
"github.com/docker/machine/libmachine/state"
)

const (
DefaultProtocol = iota
PreferIPv4
PreferIPv6
)

type Driver struct {
*drivers.BaseDriver
Boot2DockerURL string
VSwitch string
DiskSize int
MemSize int
CPU int
MacAddr string
VLanID int
DisableDynamicMemory bool
Boot2DockerURL string
VSwitch string
DiskSize int
MemSize int
CPU int
MacAddr string
VLanID int
DisableDynamicMemory bool
PreferredNetworkProtocol int
}

const (
Expand Down Expand Up @@ -99,6 +106,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "Disable dynamic memory management setting",
EnvVar: "HYPERV_DISABLE_DYNAMIC_MEMORY",
},
mcnflag.IntFlag{
Name: "hyperv-preferred-network-protocol",
Usage: "Preferred network protocol (IPv4/v6)",
EnvVar: "HYPERV_PREFERRED_NETWORK_PROTOCOL",
},
}
}

Expand Down Expand Up @@ -428,6 +440,10 @@ func (d *Driver) Kill() error {
return nil
}

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

func (d *Driver) GetIP() (string, error) {
s, err := d.GetState()
if err != nil {
Expand All @@ -437,7 +453,7 @@ func (d *Driver) GetIP() (string, error) {
return "", drivers.ErrHostIsNotRunning
}

stdout, err := cmdOut("((", "Hyper-V\\Get-VM", d.MachineName, ").networkadapters[0]).ipaddresses[0]")
stdout, err := cmdOut("((", "Hyper-V\\Get-VM", d.MachineName, ").networkadapters[0]).ipaddresses")
if err != nil {
return "", err
}
Expand All @@ -447,7 +463,33 @@ func (d *Driver) GetIP() (string, error) {
return "", fmt.Errorf("IP not found")
}

return resp[0], nil
switch d.PreferredNetworkProtocol {
case PreferIPv4:
for _, ipStr := range resp {
ip := net.ParseIP(ipStr)
if isIPv4(ipStr) && ip.To4() != nil && ip.IsGlobalUnicast() {
return ipStr, nil
}
}

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

default:
for _, ipStr := range resp {
ip := net.ParseIP(ipStr)
if ip.IsGlobalUnicast() {
return ipStr, nil
}
}
}

return "", nil
}

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

0 comments on commit cfb21a3

Please sign in to comment.