Use cached IP Address for amazonec2 driver

`GetIP` gets called every time docker-machine wants to send an ssh
command. For the amazonec2 driver it is not using the saved IPAddress
but calling the aws API every time which leads to an excessive amount of
IP calls which exhaust the API limit. Before sending a request for the
IP check if it's populated, the IPAddress is being populated for the
first time when the machine starts in
[instanceIpAvailable](https://gitlab.com/gitlab-org/ci-cd/docker-machine/blob/8bf0b23c8e88e14ed6cf9221b747e1ce062fc6ba/drivers/amazonec2/amazonec2.go#L543)

Reading [amazon
documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html#concepts-public-addresses) on weather a public IP can change during
the lifecycle of a machine it does not seem like it will effect
docker-machine in any way.

This seems to effect other drivers like the `google` driver, but drivers
like `digitalocean` are not effected since the use the
[`baseDriver.GetIP`](https://gitlab.com/gitlab-org/ci-cd/docker-machine/blob/8bf0b23c8e88e14ed6cf9221b747e1ce062fc6ba/libmachine/drivers/base.go#L39-44)
which does not call the API. Also on drivers like
[softlayer](https://gitlab.com/gitlab-org/ci-cd/docker-machine/blob/8bf0b23c8e88e14ed6cf9221b747e1ce062fc6ba/drivers/softlayer/driver.go#L285-302)
it is doing the same check before calling the API.

We can see significant reduction of API calls:

**Before Patch:**
Number of DescribeInstances: 34
Number of GetSSHHostname: 17

**After Patch:**
Number of DescribeInstances: 13
Number of GetSSHHostname: 17

More investigation of this issue can be found in gitlab-org/gitlab-runner#3424 (comment 231357594)

reference gitlab-org/gitlab-runner#3424
parent 8bf0b23c
Pipeline #93525631 passed with stages
in 7 minutes and 15 seconds
......@@ -18,6 +18,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/docker/machine/drivers/driverutil"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
......@@ -791,6 +792,10 @@ func (d *Driver) GetURL() (string, error) {
}
func (d *Driver) GetIP() (string, error) {
if d.IPAddress != "" {
return d.IPAddress, nil
}
inst, err := d.getInstance()
if err != nil {
return "", err
......
package amazonec2
import (
"testing"
"errors"
"reflect"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/docker/machine/commands/commandstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/docker/machine/commands/commandstest"
)
const (
......@@ -558,3 +557,31 @@ func TestBase64UserDataIsCorrectWhenFileProvided(t *testing.T) {
assert.NoError(t, ud_err)
assert.Equal(t, contentBase64, userdata)
}
func TestGetIP(t *testing.T) {
privateIPAddress := "127.0.0.1"
publicIPAddress := "192.168.1.1"
describeInstanceRecorder := fakeEC2DescribeInstance{
ReturnInstance: ec2.Instance{
PrivateIpAddress: &privateIPAddress,
PublicIpAddress: &publicIPAddress,
},
}
defer describeInstanceRecorder.AssertExpectations(t)
describeInstanceRecorder.On("DescribeInstances", mock.Anything).Once()
driver := NewCustomTestDriver(&describeInstanceRecorder)
// Called the first time
ip, err := driver.GetIP()
assert.NoError(t, err)
assert.Equal(t, publicIPAddress, ip)
// Set IP Address, to use cached version
driver.IPAddress = publicIPAddress
ip, err = driver.GetIP()
assert.NoError(t, err)
assert.Equal(t, publicIPAddress, ip)
}
......@@ -132,6 +132,27 @@ func (f *fakeEC2SecurityGroupTestRecorder) AuthorizeSecurityGroupIngress(input *
return value, err
}
type fakeEC2DescribeInstance struct {
*fakeEC2
ReturnInstance ec2.Instance
mock.Mock
}
func (f *fakeEC2DescribeInstance) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
f.Called(input)
return &ec2.DescribeInstancesOutput{
NextToken: nil,
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
&f.ReturnInstance,
},
},
},
}, nil
}
func NewTestDriver() *Driver {
driver := NewDriver("machineFoo", "path")
driver.clientFactory = func() Ec2Client {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment