Skip to content

Commit 82fde84

Browse files
Merge pull request #128 from okokes-akamai/cleanup_short_circuit
Short circuit LB cleanup if no annotations present
2 parents 71304e6 + 520f92e commit 82fde84

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

cloud/linode/fake_linode_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func newFake(t *testing.T) *fakeAPI {
4242
}
4343
}
4444

45+
func (f *fakeAPI) ResetRequests() {
46+
f.requests = make(map[fakeRequest]struct{})
47+
}
48+
4549
func (f *fakeAPI) recordRequest(r *http.Request) {
4650
bodyBytes, _ := ioutil.ReadAll(r.Body)
4751
r.Body.Close()

cloud/linode/loadbalancers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ func (l *loadbalancers) getNodeBalancerByStatus(ctx context.Context, service *v1
140140
// LoadBalancer status; if they are different (because of an updated NodeBalancerID
141141
// annotation), the old one is deleted.
142142
func (l *loadbalancers) cleanupOldNodeBalancer(ctx context.Context, service *v1.Service) error {
143+
// unless there's an annotation, we can never get a past and current NB to differ,
144+
// because they're looked up the same way
145+
if _, ok := getServiceAnnotation(service, annLinodeNodeBalancerID); !ok {
146+
return nil
147+
}
148+
143149
previousNB, err := l.getNodeBalancerByStatus(ctx, service)
144150
switch err.(type) {
145151
case nil:

cloud/linode/loadbalancers_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ func TestCCMLoadBalancers(t *testing.T) {
166166
name: "makeLoadBalancerStatus",
167167
f: testMakeLoadBalancerStatus,
168168
},
169+
{
170+
name: "Cleanup does not call the API unless Service annotated",
171+
f: testCleanupDoesntCall,
172+
},
169173
}
170174

171175
for _, tc := range testCases {
@@ -1444,6 +1448,54 @@ func testMakeLoadBalancerStatus(t *testing.T, client *linodego.Client, _ *fakeAP
14441448
}
14451449
}
14461450

1451+
func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
1452+
region := "us-west"
1453+
nb1, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region})
1454+
if err != nil {
1455+
t.Fatal(err)
1456+
}
1457+
nb2, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region})
1458+
if err != nil {
1459+
t.Fatal(err)
1460+
}
1461+
1462+
svc := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test"}}
1463+
svcAnn := &v1.Service{
1464+
ObjectMeta: metav1.ObjectMeta{
1465+
Name: "test",
1466+
Annotations: map[string]string{annLinodeNodeBalancerID: strconv.Itoa(nb2.ID)},
1467+
},
1468+
}
1469+
svc.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb1)
1470+
svcAnn.Status.LoadBalancer = *makeLoadBalancerStatus(svcAnn, nb1)
1471+
lb := &loadbalancers{client, region, nil}
1472+
1473+
fakeAPI.ResetRequests()
1474+
t.Run("non-annotated service shouldn't call the API during cleanup", func(t *testing.T) {
1475+
if err := lb.cleanupOldNodeBalancer(context.TODO(), svc); err != nil {
1476+
t.Fatal(err)
1477+
}
1478+
if len(fakeAPI.requests) != 0 {
1479+
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1480+
}
1481+
})
1482+
1483+
fakeAPI.ResetRequests()
1484+
t.Run("annotated service calls the API to load said NB", func(t *testing.T) {
1485+
if err := lb.cleanupOldNodeBalancer(context.TODO(), svcAnn); err != nil {
1486+
t.Fatal(err)
1487+
}
1488+
expectedRequests := map[fakeRequest]struct{}{
1489+
{Path: "/nodebalancers", Body: "", Method: "GET"}: {},
1490+
{Path: fmt.Sprintf("/nodebalancers/%v", nb2.ID), Body: "", Method: "GET"}: {},
1491+
{Path: fmt.Sprintf("/nodebalancers/%v", nb1.ID), Body: "", Method: "DELETE"}: {},
1492+
}
1493+
if !reflect.DeepEqual(fakeAPI.requests, expectedRequests) {
1494+
t.Fatalf("expected requests %#v, got %#v instead", expectedRequests, fakeAPI.requests)
1495+
}
1496+
})
1497+
}
1498+
14471499
func testGetNodeBalancerForServiceIDDoesNotExist(t *testing.T, client *linodego.Client, _ *fakeAPI) {
14481500
lb := &loadbalancers{client, "us-west", nil}
14491501
bogusNodeBalancerID := "123456"

0 commit comments

Comments
 (0)