Skip to content

Commit 826015c

Browse files
committed
Short circuit NB cleanup when service not annotated
1 parent 71304e6 commit 826015c

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

cloud/linode/loadbalancers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ 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+
// TODO(okokes): just copy pasted from elsewhere, encapsulate it somehow
146+
rawID, _ := getServiceAnnotation(service, annLinodeNodeBalancerID)
147+
id, idErr := strconv.Atoi(rawID)
148+
hasIDAnn := idErr == nil && id != 0
149+
if !hasIDAnn {
150+
return nil
151+
}
152+
143153
previousNB, err := l.getNodeBalancerByStatus(ctx, service)
144154
switch err.(type) {
145155
case nil:

cloud/linode/loadbalancers_test.go

Lines changed: 48 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 all the API unless Service annotated",
171+
f: testCleanupDoesntCall,
172+
},
169173
}
170174

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

1451+
func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
1452+
ipv4 := "192.168.0.1"
1453+
hostname := "nb-192-168-0-1.newark.nodebalancer.linode.com"
1454+
nb := &linodego.NodeBalancer{
1455+
IPv4: &ipv4,
1456+
Hostname: &hostname,
1457+
}
1458+
1459+
svc := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test"}}
1460+
svcAnn := &v1.Service{
1461+
ObjectMeta: metav1.ObjectMeta{
1462+
Name: "test",
1463+
Annotations: map[string]string{annLinodeNodeBalancerID: "12345"},
1464+
},
1465+
}
1466+
svc.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb)
1467+
svcAnn.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb)
1468+
lb := &loadbalancers{client, "us-west", nil}
1469+
1470+
t.Run("non-annotated service shouldn't call the API during cleanup", func(t *testing.T) {
1471+
if err := lb.cleanupOldNodeBalancer(context.TODO(), svc); err != nil {
1472+
t.Fatal(err)
1473+
}
1474+
if len(fakeAPI.requests) != 0 {
1475+
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1476+
}
1477+
})
1478+
1479+
// TODO(okokes): note that we're polluting fakeAPI between these subtests (not an issue per-se here, but not ideal)
1480+
t.Run("annotated service calls the API to load said NB", func(t *testing.T) {
1481+
if err := lb.cleanupOldNodeBalancer(context.TODO(), svcAnn); err != nil {
1482+
t.Fatal(err)
1483+
}
1484+
if len(fakeAPI.requests) != 1 {
1485+
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1486+
}
1487+
// TODO(okokes): we need to change this to `/v4/nodebalancer` once we upgrade to linodego v1
1488+
if !fakeAPI.didRequestOccur("GET", "/nodebalancers", "") {
1489+
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1490+
}
1491+
// TODO(okokes): check for DELETE on the old NB
1492+
})
1493+
}
1494+
14471495
func testGetNodeBalancerForServiceIDDoesNotExist(t *testing.T, client *linodego.Client, _ *fakeAPI) {
14481496
lb := &loadbalancers{client, "us-west", nil}
14491497
bogusNodeBalancerID := "123456"

0 commit comments

Comments
 (0)