Skip to content

Commit ec7dbc3

Browse files
authored
support IPv6 in AWS EC2 and Digital Ocean node drivers (#351)
1 parent 5201e6f commit ec7dbc3

34 files changed

Lines changed: 726 additions & 364 deletions

commands/commands.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,15 @@ var Commands = []cli.Command{
462462

463463
func printIP(h *host.Host) func() error {
464464
return func() error {
465+
// preference: IPv4 address, then IPv6 address
465466
ip, err := h.Driver.GetIP()
466467
if err != nil {
467-
return fmt.Errorf("Error getting IP address: %s", err)
468+
log.Warnf("Error getting IPv4 address: %s", err)
469+
log.Debug("Getting IPv6 address")
470+
ip, err = h.Driver.GetIPv6()
471+
if err != nil {
472+
return fmt.Errorf("error getting IPv6 address: %s", err)
473+
}
468474
}
469475

470476
fmt.Println(ip)

commands/commands_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestPrintIPEmptyGivenLocalEngine(t *testing.T) {
138138
assert.Equal(t, "\n", stdoutGetter.Output())
139139
}
140140

141-
func TestPrintIPPrintsGivenRemoteEngine(t *testing.T) {
141+
func TestPrintIPPrintsGivenRemoteEngineIpv4(t *testing.T) {
142142
stdoutGetter := commandstest.NewStdoutGetter()
143143
defer stdoutGetter.Stop()
144144

@@ -152,6 +152,21 @@ func TestPrintIPPrintsGivenRemoteEngine(t *testing.T) {
152152
assert.NoError(t, err)
153153
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
154154
}
155+
func TestPrintIPPrintsGivenRemoteEngineIpv6(t *testing.T) {
156+
stdoutGetter := commandstest.NewStdoutGetter()
157+
defer stdoutGetter.Stop()
158+
159+
host, _ := hosttest.GetDefaultTestHost()
160+
host.Driver = &fakedriver.Driver{
161+
MockState: state.Running,
162+
MockIPv6: "2001:db8:85a3::8a2e:370:7334",
163+
}
164+
err := printIP(host)()
165+
166+
assert.NoError(t, err)
167+
assert.Equal(t, "2001:db8:85a3::8a2e:370:7334\n", stdoutGetter.Output())
168+
169+
}
155170

156171
func TestConsolidateError(t *testing.T) {
157172
cases := []struct {

commands/ls_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package commands
22

33
import (
4+
"errors"
45
"os"
56
"testing"
6-
77
"time"
88

9-
"errors"
10-
119
"github.com/rancher/machine/drivers/fakedriver"
1210
"github.com/rancher/machine/libmachine/engine"
1311
"github.com/rancher/machine/libmachine/host"
@@ -340,7 +338,7 @@ func TestGetHostListItems(t *testing.T) {
340338
version string
341339
error string
342340
}{
343-
{"bar10", state.Error, false, "Unknown", "Unable to get ip"},
341+
{"bar10", state.Error, false, "Unknown", "unable to get ip"},
344342
{"bar100", state.Stopped, false, "Unknown", ""},
345343
{"foo", state.Running, true, "v1.9", ""},
346344
}
@@ -490,7 +488,7 @@ func TestGetHostStateError(t *testing.T) {
490488
assert.Equal(t, state.Error, hostItem.State)
491489
assert.Equal(t, "Driver", hostItem.DriverName)
492490
assert.Empty(t, hostItem.URL)
493-
assert.Equal(t, "Unable to get ip", hostItem.Error)
491+
assert.Equal(t, "unable to get ip", hostItem.Error)
494492
assert.Nil(t, hostItem.SwarmOptions)
495493
}
496494

commands/scp.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"errors"
55
"fmt"
6+
"net/netip"
67
"os"
78
"os/exec"
89
"strings"
@@ -179,6 +180,13 @@ func generateLocationArg(hostInfo HostInfo, user, path string) (string, error) {
179180
if err != nil {
180181
return "", err
181182
}
183+
ipAddr, err := netip.ParseAddr(hostname)
184+
if err != nil {
185+
return "", err
186+
}
187+
if ipAddr.Is6() {
188+
hostname = fmt.Sprintf("[%s]", hostname)
189+
}
182190

183191
if user == "" {
184192
user = hostInfo.GetSSHUsername()

commands/scp_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type MockHostInfo struct {
1212
name string
1313
ip string
14+
ipv6 string
1415
sshPort int
1516
sshUsername string
1617
sshKeyPath string
@@ -21,7 +22,11 @@ func (h *MockHostInfo) GetMachineName() string {
2122
}
2223

2324
func (h *MockHostInfo) GetSSHHostname() (string, error) {
24-
return h.ip, nil
25+
// preference: IPv4 address, then IPv6 address
26+
if h.ip != "" {
27+
return h.ip, nil
28+
}
29+
return h.ipv6, nil
2530
}
2631

2732
func (h *MockHostInfo) GetSSHPort() (int, error) {

commands/version.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ func printVersion(c CommandLine, api libmachine.API, out io.Writer) error {
2828
return err
2929
}
3030

31-
if host.HostOptions.AuthOptions != nil {
31+
if host != nil && host.HostOptions != nil && host.HostOptions.AuthOptions != nil {
3232
version, err := mcndockerclient.DockerVersion(host)
3333
if err != nil {
3434
return err
3535
}
36-
37-
fmt.Fprintln(out, version)
36+
_, _ = fmt.Fprintln(out, version)
3837
} else {
39-
fmt.Fprintln(out, "Docker was not installed on machine")
38+
_, _ = fmt.Fprintln(out, "Docker was not installed on machine")
4039
}
4140

4241
return nil

commands/version_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package commands
22

33
import (
4+
"bytes"
45
"errors"
56
"testing"
67

7-
"bytes"
8-
98
"github.com/rancher/machine/commands/commandstest"
9+
"github.com/rancher/machine/libmachine/auth"
1010
"github.com/rancher/machine/libmachine/host"
1111
"github.com/rancher/machine/libmachine/libmachinetest"
1212
"github.com/rancher/machine/libmachine/mcndockerclient"
@@ -56,6 +56,9 @@ func TestCmdVersionOnHost(t *testing.T) {
5656
Hosts: []*host.Host{
5757
{
5858
Name: "machine",
59+
HostOptions: &host.Options{
60+
AuthOptions: &auth.Options{},
61+
},
5962
},
6063
},
6164
}
@@ -78,6 +81,9 @@ func TestCmdVersionFailure(t *testing.T) {
7881
Hosts: []*host.Host{
7982
{
8083
Name: "machine",
84+
HostOptions: &host.Options{
85+
AuthOptions: &auth.Options{},
86+
},
8187
},
8288
},
8389
}

0 commit comments

Comments
 (0)