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