Use cached IP for google driver

`GetIP` gets called every time `GetSSHHostname` is called which is
called to run every ssh command to the machine. `GetIP` is sending a
request to the API every time we request it.

Check if the in-memory IP is available before sending a request to the
API, and save it at the end as well if it's not present.

Adding tests is quite difficult here since the client for accessing the
Google API is created within `GetIP` so it's not something that can be
mocked easily. To make it testable we require a lot of changes to the
google driver which will be riskier the not adding tests.

Below are some metrics on how many times we used to call the API for the
IP address when creating and removing a machine:

**Before Patch:**
Number of `GetIP` Calls: 21
Number of `Instance.Get` Calls: 21

**After Patch:**
Number of `GetIP` Calls: 21
Number of `Instance.Get` Calls: 1
parent 220f80f7
Pipeline #93723259 passed with stages
in 8 minutes and 58 seconds
......@@ -379,6 +379,10 @@ func (d *Driver) GetURL() (string, error) {
// GetIP returns the IP address of the GCE instance.
func (d *Driver) GetIP() (string, error) {
if d.IPAddress != "" {
return d.IPAddress, nil
c, err := newComputeUtil(d)
if err != nil {
return "", err
......@@ -392,6 +396,8 @@ func (d *Driver) GetIP() (string, error) {
return "", drivers.ErrHostIsNotRunning
d.IPAddress = ip
return ip, nil
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment