Skip to content

Commit 860a66f

Browse files
fix: remove firewall device by device ID not entity ID (#214)
* fix: remove firewall device by device ID not entity ID * update tests --------- Co-authored-by: Ryan Lonergan <[email protected]>
1 parent 4ad57e6 commit 860a66f

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

cloud/linode/firewall/firewalls.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,17 @@ func (l *LinodeClient) UpdateNodeBalancerFirewall(
260260
klog.Errorf("Found more than one firewall attached to nodebalancer: %d, firewall IDs: %v", nb.ID, firewalls)
261261
return ErrTooManyNBFirewalls
262262
}
263-
264-
err = l.Client.DeleteFirewallDevice(ctx, firewalls[0].ID, nb.ID)
263+
deviceID, deviceExists, err := l.getNodeBalancerDeviceID(ctx, firewalls[0].ID, nb.ID)
265264
if err != nil {
266265
return err
267266
}
267+
if deviceExists {
268+
err = l.Client.DeleteFirewallDevice(ctx, firewalls[0].ID, deviceID)
269+
if err != nil {
270+
return err
271+
}
272+
}
273+
268274
// once we delete the device, we should see if there's anything attached to that firewall
269275
devices, err := l.Client.ListFirewallDevices(ctx, firewalls[0].ID, &linodego.ListOptions{})
270276
if err != nil {

cloud/linode/loadbalancers_test.go

+99-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ func TestCCMLoadBalancers(t *testing.T) {
188188
},
189189
{
190190
name: "Update Load Balancer - Delete Firewall ID",
191-
f: testUpdateLoadBalancerDeleteFirewall,
191+
f: testUpdateLoadBalancerDeleteFirewallRemoveID,
192+
},
193+
{
194+
name: "Update Load Balancer - Delete Firewall ACL",
195+
f: testUpdateLoadBalancerDeleteFirewallRemoveACL,
192196
},
193197
{
194198
name: "Update Load Balancer - Update Firewall ACL",
@@ -1290,6 +1294,99 @@ func testUpdateLoadBalancerAddNewFirewallACL(t *testing.T, client *linodego.Clie
12901294
}
12911295
}
12921296

1297+
func testUpdateLoadBalancerDeleteFirewallRemoveACL(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
1298+
svc := &v1.Service{
1299+
ObjectMeta: metav1.ObjectMeta{
1300+
Name: randString(),
1301+
UID: "foobar123",
1302+
},
1303+
Spec: v1.ServiceSpec{
1304+
Ports: []v1.ServicePort{
1305+
{
1306+
Name: randString(),
1307+
Protocol: "TCP",
1308+
Port: int32(80),
1309+
NodePort: int32(30000),
1310+
},
1311+
},
1312+
},
1313+
}
1314+
1315+
nodes := []*v1.Node{
1316+
{
1317+
Status: v1.NodeStatus{
1318+
Addresses: []v1.NodeAddress{
1319+
{
1320+
Type: v1.NodeInternalIP,
1321+
Address: "127.0.0.1",
1322+
},
1323+
},
1324+
},
1325+
},
1326+
}
1327+
1328+
lb := newLoadbalancers(client, "us-west").(*loadbalancers)
1329+
fakeClientset := fake.NewSimpleClientset()
1330+
lb.kubeClient = fakeClientset
1331+
1332+
svc.ObjectMeta.SetAnnotations(map[string]string{
1333+
annotations.AnnLinodeCloudFirewallACL: `{
1334+
"allowList": {
1335+
"ipv4": ["2.2.2.2"]
1336+
}
1337+
}`,
1338+
})
1339+
1340+
defer func() {
1341+
_ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc)
1342+
}()
1343+
lbStatus, err := lb.EnsureLoadBalancer(context.TODO(), "linodelb", svc, nodes)
1344+
if err != nil {
1345+
t.Errorf("EnsureLoadBalancer returned an error: %s", err)
1346+
}
1347+
svc.Status.LoadBalancer = *lbStatus
1348+
stubService(fakeClientset, svc)
1349+
1350+
nb, err := lb.getNodeBalancerByStatus(context.TODO(), svc)
1351+
if err != nil {
1352+
t.Fatalf("failed to get NodeBalancer via status: %s", err)
1353+
}
1354+
1355+
firewalls, err := lb.client.ListNodeBalancerFirewalls(context.TODO(), nb.ID, &linodego.ListOptions{})
1356+
if err != nil {
1357+
t.Fatalf("Failed to list nodeBalancer firewalls %s", err)
1358+
}
1359+
1360+
if len(firewalls) == 0 {
1361+
t.Fatalf("No firewalls attached")
1362+
}
1363+
1364+
if firewalls[0].Rules.InboundPolicy != "DROP" {
1365+
t.Errorf("expected DROP inbound policy, got %s", firewalls[0].Rules.InboundPolicy)
1366+
}
1367+
1368+
fwIPs := firewalls[0].Rules.Inbound[0].Addresses.IPv4
1369+
if fwIPs == nil {
1370+
t.Errorf("expected IP, got %v", fwIPs)
1371+
}
1372+
1373+
svc.ObjectMeta.SetAnnotations(map[string]string{})
1374+
1375+
err = lb.UpdateLoadBalancer(context.TODO(), "linodelb", svc, nodes)
1376+
if err != nil {
1377+
t.Errorf("UpdateLoadBalancer returned an error: %s", err)
1378+
}
1379+
1380+
firewallsNew, err := lb.client.ListNodeBalancerFirewalls(context.TODO(), nb.ID, &linodego.ListOptions{})
1381+
if err != nil {
1382+
t.Fatalf("failed to List Firewalls %s", err)
1383+
}
1384+
1385+
if len(firewallsNew) != 0 {
1386+
t.Fatalf("firewall's %d still attached", firewallsNew[0].ID)
1387+
}
1388+
}
1389+
12931390
func testUpdateLoadBalancerUpdateFirewallRemoveACLaddID(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
12941391
svc := &v1.Service{
12951392
ObjectMeta: metav1.ObjectMeta{
@@ -1810,7 +1907,7 @@ func testUpdateLoadBalancerUpdateFirewall(t *testing.T, client *linodego.Client,
18101907
}
18111908
}
18121909

1813-
func testUpdateLoadBalancerDeleteFirewall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
1910+
func testUpdateLoadBalancerDeleteFirewallRemoveID(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
18141911
firewallCreateOpts := linodego.FirewallCreateOptions{
18151912
Label: "test",
18161913
Rules: linodego.FirewallRuleSet{Inbound: []linodego.FirewallRule{{

0 commit comments

Comments
 (0)