Skip to content

Commit 82ba182

Browse files
wdullaerafbjorklund
authored andcommitted
Make docker engine port configurable
Introduced -engine-port flag in create subcommand Extended drivers interface with a GetPort() method Updated all drivers with the new interface Updated provisioning to use GetPort() rather than parse GetURL() for the port Made driver options for setting the engine port emit a useful error message Fixes docker-archive-public#3361 Signed-off-by: Wouter Dullaert <[email protected]> (cherry picked from commit edad5f7)
1 parent 49dfaa7 commit 82ba182

File tree

37 files changed

+321
-98
lines changed

37 files changed

+321
-98
lines changed

commands/create.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ var (
7777
Usage: "Specify environment variables to set in the engine",
7878
Value: &cli.StringSlice{},
7979
},
80+
cli.IntFlag{
81+
Name: "engine-port",
82+
Usage: "Specify the port number that the engine will listen on",
83+
Value: engine.DefaultPort,
84+
EnvVar: "ENGINE_PORT_NUMBER",
85+
},
8086
cli.BoolFlag{
8187
Name: "swarm",
8288
Usage: "Configure Machine to join a Swarm cluster",
@@ -157,6 +163,7 @@ func cmdCreateInner(c CommandLine, api libmachine.API) error {
157163
rawDriver, err := json.Marshal(&drivers.BaseDriver{
158164
MachineName: name,
159165
StorePath: c.GlobalString("storage-path"),
166+
PortNumber: c.Int("engine-port"),
160167
})
161168
if err != nil {
162169
return fmt.Errorf("Error attempting to marshal bare driver data: %s", err)

commands/ls_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func TestGetHostListItems(t *testing.T) {
317317
Driver: &fakedriver.Driver{
318318
MockState: state.Running,
319319
MockIP: "active.host.com",
320+
MockPort: 2376,
320321
},
321322
},
322323
{

commands/url_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestCmdURL(t *testing.T) {
4242
Driver: &fakedriver.Driver{
4343
MockState: state.Running,
4444
MockIP: "120.0.0.1",
45+
MockPort: 2376,
4546
},
4647
},
4748
},

drivers/amazonec2/amazonec2.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const (
5050
)
5151

5252
var (
53-
dockerPort = 2376
5453
swarmPort = 3376
5554
errorNoPrivateSSHKey = errors.New("using --amazonec2-keypair-name also requires --amazonec2-ssh-keypath")
5655
errorMissingCredentials = errors.New("amazonec2 driver requires AWS credentials configured with the --amazonec2-access-key and --amazonec2-secret-key options, environment variables, ~/.aws/credentials, or an instance role")
@@ -759,7 +758,7 @@ func (d *Driver) GetURL() (string, error) {
759758
return "", nil
760759
}
761760

762-
return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, strconv.Itoa(dockerPort))), nil
761+
return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, strconv.Itoa(d.GetPort()))), nil
763762
}
764763

765764
func (d *Driver) GetIP() (string, error) {
@@ -1128,11 +1127,11 @@ func (d *Driver) configureSecurityGroupPermissions(group *ec2.SecurityGroup) ([]
11281127
})
11291128
}
11301129

1131-
if !hasPorts[fmt.Sprintf("%d/tcp", dockerPort)] {
1130+
if !hasPorts[fmt.Sprintf("%d/tcp", d.GetPort())] {
11321131
perms = append(perms, &ec2.IpPermission{
11331132
IpProtocol: aws.String("tcp"),
1134-
FromPort: aws.Int64(int64(dockerPort)),
1135-
ToPort: aws.Int64(int64(dockerPort)),
1133+
FromPort: aws.Int64(int64(d.GetPort())),
1134+
ToPort: aws.Int64(int64(d.GetPort())),
11361135
IpRanges: []*ec2.IpRange{{CidrIp: aws.String(ipRange)}},
11371136
})
11381137
}

drivers/amazonec2/amazonec2_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
const (
2121
testSSHPort = int64(22)
22-
testDockerPort = int64(2376)
22+
testDockerPort = int64(12345)
2323
testSwarmPort = int64(3376)
2424
)
2525

@@ -32,7 +32,7 @@ var (
3232
)
3333

3434
func TestConfigureSecurityGroupPermissionsEmpty(t *testing.T) {
35-
driver := NewTestDriver()
35+
driver := NewTestDriver(int(testDockerPort))
3636

3737
perms, err := driver.configureSecurityGroupPermissions(securityGroup)
3838

@@ -41,7 +41,7 @@ func TestConfigureSecurityGroupPermissionsEmpty(t *testing.T) {
4141
}
4242

4343
func TestConfigureSecurityGroupPermissionsSshOnly(t *testing.T) {
44-
driver := NewTestDriver()
44+
driver := NewTestDriver(int(testDockerPort))
4545
group := securityGroup
4646
group.IpPermissions = []*ec2.IpPermission{
4747
{
@@ -59,7 +59,7 @@ func TestConfigureSecurityGroupPermissionsSshOnly(t *testing.T) {
5959
}
6060

6161
func TestConfigureSecurityGroupPermissionsDockerOnly(t *testing.T) {
62-
driver := NewTestDriver()
62+
driver := NewTestDriver(int(testDockerPort))
6363
group := securityGroup
6464
group.IpPermissions = []*ec2.IpPermission{
6565
{
@@ -77,7 +77,7 @@ func TestConfigureSecurityGroupPermissionsDockerOnly(t *testing.T) {
7777
}
7878

7979
func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) {
80-
driver := NewTestDriver()
80+
driver := NewTestDriver(int(testDockerPort))
8181
group := securityGroup
8282
group.IpPermissions = []*ec2.IpPermission{
8383
{
@@ -99,7 +99,7 @@ func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) {
9999
}
100100

101101
func TestConfigureSecurityGroupPermissionsOpenPorts(t *testing.T) {
102-
driver := NewTestDriver()
102+
driver := NewTestDriver(int(testDockerPort))
103103
driver.OpenPorts = []string{"8888/tcp", "8080/udp", "9090"}
104104
perms, err := driver.configureSecurityGroupPermissions(&ec2.SecurityGroup{})
105105

@@ -114,7 +114,7 @@ func TestConfigureSecurityGroupPermissionsOpenPorts(t *testing.T) {
114114
}
115115

116116
func TestConfigureSecurityGroupPermissionsOpenPortsSkipExisting(t *testing.T) {
117-
driver := NewTestDriver()
117+
driver := NewTestDriver(int(testDockerPort))
118118
group := securityGroup
119119
group.IpPermissions = []*ec2.IpPermission{
120120
{
@@ -137,7 +137,7 @@ func TestConfigureSecurityGroupPermissionsOpenPortsSkipExisting(t *testing.T) {
137137
}
138138

139139
func TestConfigureSecurityGroupPermissionsInvalidOpenPorts(t *testing.T) {
140-
driver := NewTestDriver()
140+
driver := NewTestDriver(int(testDockerPort))
141141
driver.OpenPorts = []string{"2222/tcp", "abc1"}
142142
perms, err := driver.configureSecurityGroupPermissions(&ec2.SecurityGroup{})
143143

@@ -146,7 +146,7 @@ func TestConfigureSecurityGroupPermissionsInvalidOpenPorts(t *testing.T) {
146146
}
147147

148148
func TestConfigureSecurityGroupPermissionsWithSwarm(t *testing.T) {
149-
driver := NewTestDriver()
149+
driver := NewTestDriver(int(testDockerPort))
150150
driver.SwarmMaster = true
151151
group := securityGroup
152152
group.IpPermissions = []*ec2.IpPermission{
@@ -219,7 +219,7 @@ func TestDefaultVPCIsMissing(t *testing.T) {
219219
}
220220

221221
func TestGetRegionZoneForDefaultEndpoint(t *testing.T) {
222-
driver := NewCustomTestDriver(&fakeEC2WithLogin{})
222+
driver := NewCustomTestDriver(int(testDockerPort), &fakeEC2WithLogin{})
223223
driver.awsCredentialsFactory = NewValidAwsCredentials
224224
options := &commandstest.FakeFlagger{
225225
Data: map[string]interface{}{
@@ -238,7 +238,7 @@ func TestGetRegionZoneForDefaultEndpoint(t *testing.T) {
238238
}
239239

240240
func TestGetRegionZoneForCustomEndpoint(t *testing.T) {
241-
driver := NewCustomTestDriver(&fakeEC2WithLogin{})
241+
driver := NewCustomTestDriver(int(testDockerPort), &fakeEC2WithLogin{})
242242
driver.awsCredentialsFactory = NewValidAwsCredentials
243243
options := &commandstest.FakeFlagger{
244244
Data: map[string]interface{}{
@@ -272,7 +272,7 @@ func TestDescribeAccountAttributeFails(t *testing.T) {
272272
}
273273

274274
func TestAwsCredentialsAreRequired(t *testing.T) {
275-
driver := NewTestDriver()
275+
driver := NewTestDriver(int(testDockerPort))
276276
driver.awsCredentialsFactory = NewErrorAwsCredentials
277277

278278
options := &commandstest.FakeFlagger{
@@ -288,7 +288,7 @@ func TestAwsCredentialsAreRequired(t *testing.T) {
288288
}
289289

290290
func TestValidAwsCredentialsAreAccepted(t *testing.T) {
291-
driver := NewCustomTestDriver(&fakeEC2WithLogin{})
291+
driver := NewCustomTestDriver(int(testDockerPort), &fakeEC2WithLogin{})
292292
driver.awsCredentialsFactory = NewValidAwsCredentials
293293
options := &commandstest.FakeFlagger{
294294
Data: map[string]interface{}{
@@ -303,7 +303,7 @@ func TestValidAwsCredentialsAreAccepted(t *testing.T) {
303303
}
304304

305305
func TestEndpointIsMandatoryWhenSSLDisabled(t *testing.T) {
306-
driver := NewTestDriver()
306+
driver := NewTestDriver(int(testDockerPort))
307307
driver.awsCredentialsFactory = NewValidAwsCredentials
308308
options := &commandstest.FakeFlagger{
309309
Data: map[string]interface{}{
@@ -402,7 +402,7 @@ func ipPermission(port int64) *ec2.IpPermission {
402402
func TestConfigureSecurityGroupsEmpty(t *testing.T) {
403403
recorder := fakeEC2SecurityGroupTestRecorder{}
404404

405-
driver := NewCustomTestDriver(&recorder)
405+
driver := NewCustomTestDriver(int(testDockerPort), &recorder)
406406
err := driver.configureSecurityGroups([]string{})
407407

408408
assert.Nil(t, err)
@@ -457,7 +457,7 @@ func TestConfigureSecurityGroupsMixed(t *testing.T) {
457457
}).Return(
458458
&ec2.AuthorizeSecurityGroupIngressOutput{}, nil)
459459

460-
driver := NewCustomTestDriver(&recorder)
460+
driver := NewCustomTestDriver(int(testDockerPort), &recorder)
461461
err := driver.configureSecurityGroups(groups)
462462

463463
assert.Nil(t, err)
@@ -472,15 +472,15 @@ func TestConfigureSecurityGroupsErrLookupExist(t *testing.T) {
472472
recorder.On("DescribeSecurityGroups", mock.MatchedBy(matchGroupLookup(groups))).Return(
473473
nil, lookupExistErr)
474474

475-
driver := NewCustomTestDriver(&recorder)
475+
driver := NewCustomTestDriver(int(testDockerPort), &recorder)
476476
err := driver.configureSecurityGroups(groups)
477477

478478
assert.Exactly(t, lookupExistErr, err)
479479
recorder.AssertExpectations(t)
480480
}
481481

482482
func TestBase64UserDataIsEmptyIfNoFileProvided(t *testing.T) {
483-
driver := NewTestDriver()
483+
driver := NewTestDriver(int(testDockerPort))
484484

485485
userdata, err := driver.Base64UserData()
486486

@@ -495,7 +495,7 @@ func TestBase64UserDataGeneratesErrorIfFileNotFound(t *testing.T) {
495495
defer os.RemoveAll(dir)
496496
userdata_path := filepath.Join(dir, "does-not-exist.yml")
497497

498-
driver := NewTestDriver()
498+
driver := NewTestDriver(int(testDockerPort))
499499
driver.UserDataFile = userdata_path
500500

501501
_, ud_err := driver.Base64UserData()
@@ -516,7 +516,7 @@ func TestBase64UserDataIsCorrectWhenFileProvided(t *testing.T) {
516516
err = ioutil.WriteFile(userdata_path, content, 0666)
517517
assert.NoError(t, err, "Unable to create temporary userdata file.")
518518

519-
driver := NewTestDriver()
519+
driver := NewTestDriver(int(testDockerPort))
520520
driver.UserDataFile = userdata_path
521521

522522
userdata, ud_err := driver.Base64UserData()

drivers/amazonec2/stub_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,18 @@ func (f *fakeEC2SecurityGroupTestRecorder) AuthorizeSecurityGroupIngress(input *
132132
return value, err
133133
}
134134

135-
func NewTestDriver() *Driver {
135+
func NewTestDriver(dockerPort int) *Driver {
136136
driver := NewDriver("machineFoo", "path")
137+
driver.PortNumber = dockerPort
137138
driver.clientFactory = func() Ec2Client {
138139
return &fakeEC2{}
139140
}
140141
return driver
141142
}
142143

143-
func NewCustomTestDriver(ec2Client Ec2Client) *Driver {
144+
func NewCustomTestDriver(dockerPort int, ec2Client Ec2Client) *Driver {
144145
driver := NewDriver("machineFoo", "path")
146+
driver.PortNumber = dockerPort
145147
driver.clientFactory = func() Ec2Client {
146148
return ec2Client
147149
}

drivers/azure/azure.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/url"
1010
"os"
11+
"strconv"
1112

1213
"github.com/docker/machine/drivers/azure/azureutil"
1314
"github.com/docker/machine/libmachine/drivers"
@@ -24,7 +25,6 @@ const (
2425
defaultAzureSize = "Standard_A2"
2526
defaultAzureLocation = "westus"
2627
defaultSSHUser = "docker-user" // 'root' not allowed on Azure
27-
defaultDockerPort = 2376
2828
defaultAzureImage = "canonical:UbuntuServer:16.04.0-LTS:latest"
2929
defaultAzureVNet = "docker-machine-vnet"
3030
defaultAzureSubnet = "docker-machine"
@@ -74,7 +74,6 @@ type Driver struct {
7474
SubscriptionID string
7575
ResourceGroup string
7676

77-
DockerPort int
7877
Location string
7978
Size string
8079
Image string
@@ -139,12 +138,6 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
139138
EnvVar: "AZURE_SSH_USER",
140139
Value: defaultSSHUser,
141140
},
142-
mcnflag.IntFlag{
143-
Name: flAzureDockerPort,
144-
Usage: "Port number for Docker engine",
145-
EnvVar: "AZURE_DOCKER_PORT",
146-
Value: defaultDockerPort,
147-
},
148141
mcnflag.StringFlag{
149142
Name: flAzureLocation,
150143
Usage: "Azure region to create the virtual machine",
@@ -233,6 +226,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
233226
Usage: "Azure Service Principal Account password (optional, browser auth is used if not specified)",
234227
EnvVar: "AZURE_CLIENT_SECRET",
235228
},
229+
// DEPRECATED: remove in a future version
230+
mcnflag.IntFlag{
231+
Name: flAzureDockerPort,
232+
Usage: "Port number for Docker engine",
233+
EnvVar: "AZURE_DOCKER_PORT",
234+
},
236235
}
237236
}
238237

@@ -273,7 +272,6 @@ func (d *Driver) SetConfigFromFlags(fl drivers.DriverOptions) error {
273272
d.UsePrivateIP = fl.Bool(flAzureUsePrivateIP)
274273
d.NoPublicIP = fl.Bool(flAzureNoPublicIP)
275274
d.StaticPublicIP = fl.Bool(flAzureStaticPublicIP)
276-
d.DockerPort = fl.Int(flAzureDockerPort)
277275
d.DNSLabel = fl.String(flAzureDNSLabel)
278276
d.CustomDataFile = fl.String(flAzureCustomData)
279277

@@ -284,6 +282,10 @@ func (d *Driver) SetConfigFromFlags(fl drivers.DriverOptions) error {
284282
d.BaseDriver.SSHPort = sshPort
285283
d.SetSwarmConfigFromFlags(fl)
286284

285+
if fl.Int(flAzureDockerPort) != 0 {
286+
return fmt.Errorf("-%s has been deprecated in favor of: -engine-port", flAzureDockerPort)
287+
}
288+
287289
log.Debug("Set configuration from flags.")
288290
return nil
289291
}
@@ -473,7 +475,7 @@ func (d *Driver) GetURL() (string, error) {
473475
}
474476
u := (&url.URL{
475477
Scheme: "tcp",
476-
Host: net.JoinHostPort(ip, fmt.Sprintf("%d", d.DockerPort)),
478+
Host: net.JoinHostPort(ip, strconv.Itoa(d.GetPort())),
477479
}).String()
478480
log.Debugf("Machine URL is resolved to: %s", u)
479481
return u, nil

drivers/azure/azure_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package azure
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/machine/libmachine/drivers"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGenericDockerPortDeprecationError(t *testing.T) {
11+
driver := NewDriver("default", "path")
12+
13+
checkFlags := &drivers.CheckDriverOptions{
14+
FlagsValues: map[string]interface{}{
15+
"azure-subscription-id": "abcdef",
16+
"azure-docker-port": 12345,
17+
},
18+
CreateFlags: driver.GetCreateFlags(),
19+
}
20+
21+
err := driver.SetConfigFromFlags(checkFlags)
22+
23+
assert.EqualError(
24+
t,
25+
err,
26+
"-azure-docker-port has been deprecated in favor of: -engine-port",
27+
"SetConfigFromFlags should throw an error when generic-docker-port is set",
28+
)
29+
}

drivers/azure/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ func (d *Driver) getSecurityRules(extraPorts []string) (*[]network.SecurityRule,
109109
}
110110
}
111111

112-
log.Debugf("Docker port is configured as %d", d.DockerPort)
112+
log.Debugf("Docker port is configured as %d", d.GetPort())
113113

114114
// Base ports to be opened for any machine
115115
rl := []network.SecurityRule{
116116
mkRule(100, "SSHAllowAny", "Allow ssh from public Internet", "*", fmt.Sprintf("%d", d.BaseDriver.SSHPort), network.TCP),
117-
mkRule(300, "DockerAllowAny", "Allow docker engine access (TLS-protected)", "*", fmt.Sprintf("%d", d.DockerPort), network.TCP),
117+
mkRule(300, "DockerAllowAny", "Allow docker engine access (TLS-protected)", "*", fmt.Sprintf("%d", d.GetPort()), network.TCP),
118118
}
119119

120120
// Open swarm port if configured

0 commit comments

Comments
 (0)