Skip to content

Commit

Permalink
Release reserved GCE IP address after ensure completes.
Browse files Browse the repository at this point in the history
Previous behavior was to release the ip only upon success, but it should be released
on failure as well. Otherwise, an IP address will be unnecessarily consumed due to an
in-error service.
  • Loading branch information
prameshj committed Jan 6, 2021
1 parent ff7724e commit 77a5a3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Expand Up @@ -158,6 +158,12 @@ func (g *Cloud) ensureInternalLoadBalancer(clusterName, clusterID string, svc *v
return nil, err
}
klog.V(2).Infof("ensureInternalLoadBalancer(%v): reserved IP %q for the forwarding rule", loadBalancerName, ipToUse)
defer func() {
// Release the address if all resources were created successfully, or if we error out.
if err := addrMgr.ReleaseAddress(); err != nil {
klog.Errorf("ensureInternalLoadBalancer: failed to release address reservation, possibly causing an orphan: %v", err)
}
}()
}

// Ensure firewall rules if necessary
Expand Down Expand Up @@ -197,13 +203,6 @@ func (g *Cloud) ensureInternalLoadBalancer(clusterName, clusterID string, svc *v
g.clearPreviousInternalResources(svc, loadBalancerName, existingBackendService, backendServiceName, hcName)
}

if addrMgr != nil {
// Now that the controller knows the forwarding rule exists, we can release the address.
if err := addrMgr.ReleaseAddress(); err != nil {
klog.Errorf("ensureInternalLoadBalancer: failed to release address reservation, possibly causing an orphan: %v", err)
}
}

// Get the most recent forwarding rule for the address.
updatedFwdRule, err := g.GetRegionForwardingRule(loadBalancerName, g.region)
if err != nil {
Expand Down
Expand Up @@ -950,6 +950,11 @@ func TestEnsureInternalLoadBalancerErrors(t *testing.T) {
)
assert.Error(t, err, "Should return an error when "+desc)
assert.Nil(t, status, "Should not return a status when "+desc)

// ensure that the temporarily reserved IP address is released upon sync errors
ip, err := gce.GetRegionAddress(gce.GetLoadBalancerName(context.TODO(), params.clusterName, params.service), gce.region)
require.Error(t, err)
assert.Nil(t, ip)
})
}
}
Expand Down
Expand Up @@ -247,6 +247,11 @@ func assertInternalLbResources(t *testing.T, gce *Cloud, apiService *v1.Service,
assert.Equal(t, backendServiceLink, fwdRule.BackendService)
// if no Subnetwork specified, defaults to the GCE NetworkURL
assert.Equal(t, gce.NetworkURL(), fwdRule.Subnetwork)

// Check that the IP address has been released. IP is only reserved until ensure function exits.
ip, err := gce.GetRegionAddress(lbName, gce.region)
require.Error(t, err)
assert.Nil(t, ip)
}

func assertInternalLbResourcesDeleted(t *testing.T, gce *Cloud, apiService *v1.Service, vals TestClusterValues, firewallsDeleted bool) {
Expand Down Expand Up @@ -285,6 +290,11 @@ func assertInternalLbResourcesDeleted(t *testing.T, gce *Cloud, apiService *v1.S
healthcheck, err := gce.GetHealthCheck(hcName)
require.Error(t, err)
assert.Nil(t, healthcheck)

// Check that the IP address has been released
ip, err := gce.GetRegionAddress(lbName, gce.region)
require.Error(t, err)
assert.Nil(t, ip)
}

func checkEvent(t *testing.T, recorder *record.FakeRecorder, expected string, shouldMatch bool) bool {
Expand Down

0 comments on commit 77a5a3a

Please sign in to comment.