@@ -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+
14471495func testGetNodeBalancerForServiceIDDoesNotExist (t * testing.T , client * linodego.Client , _ * fakeAPI ) {
14481496 lb := & loadbalancers {client , "us-west" , nil }
14491497 bogusNodeBalancerID := "123456"
0 commit comments