@@ -99,7 +99,7 @@ func NewEMLB(metalAPIKey, projectID, metro string) *EMLB {
9999 return manager
100100}
101101
102- // ReconcileLoadBalancer creates a new Equinix Metal Load Balancer.
102+ // ReconcileLoadBalancer creates a new Equinix Metal Load Balancer and associates it with the given ClusterScope .
103103func (e * EMLB ) ReconcileLoadBalancer (ctx context.Context , clusterScope * scope.ClusterScope ) error {
104104 log := ctrl .LoggerFrom (ctx )
105105
@@ -156,7 +156,7 @@ func (e *EMLB) ReconcileVIPOrigin(ctx context.Context, machineScope *scope.Machi
156156 }
157157
158158 // Fetch the Load Balancer object.
159- lb , err := e .getLoadBalancer (ctx , lbID )
159+ lb , _ , err := e .getLoadBalancer (ctx , lbID )
160160 if err != nil {
161161 return err
162162 }
@@ -233,12 +233,65 @@ func (e *EMLB) ReconcileVIPOrigin(ctx context.Context, machineScope *scope.Machi
233233 return nil
234234}
235235
236+ // DeleteLoadBalancer deletes the Equinix Metal Load Balancer associated with a given ClusterScope.
237+ func (e * EMLB ) DeleteLoadBalancer (ctx context.Context , clusterScope * scope.ClusterScope ) error {
238+ log := ctrl .LoggerFrom (ctx )
239+
240+ packetCluster := clusterScope .PacketCluster
241+ clusterName := packetCluster .Name
242+
243+ // Make sure the cluster already has an EMLB ID in its packetCluster annotations, otherwise abort.
244+ lbID , exists := packetCluster .Annotations [loadBalancerIDAnnotation ]
245+ if ! exists || (lbID == "" ) {
246+ log .Info ("no Equinix Metal Load Balancer found in cluster's annotations, skipping EMLB delete" )
247+ return nil
248+ }
249+
250+ log .Info ("Deleting EMLB" , "Cluster Metro" , e .metro , "Cluster Name" , clusterName , "Project ID" , e .projectID , "Load Balancer ID" , lbID )
251+
252+ resp , err := e .deleteLoadBalancer (ctx , lbID )
253+ if err != nil {
254+ if resp .StatusCode == http .StatusNotFound {
255+ return nil
256+ }
257+ log .Error (err , "LB Delete Failed" , "EMLB ID" , lbID , "Response Body" , resp .Body )
258+ }
259+
260+ return err
261+ }
262+
263+ // DeleteLoadBalancerOrigin deletes the Equinix Metal Load Balancer associated with a given ClusterScope.
264+ func (e * EMLB ) DeleteLoadBalancerOrigin (ctx context.Context , machineScope * scope.MachineScope ) error {
265+ // Initially, we're creating a single pool per origin, logic below needs to be updated if we move to a shared load balancer pool model.
266+ log := ctrl .LoggerFrom (ctx )
267+
268+ clusterName := machineScope .Cluster .Name
269+
270+ // Make sure the machine has an EMLB Pool ID in its packetMachine annotations, otherwise abort.
271+ lbPoolID , exists := machineScope .PacketMachine .Annotations [loadBalancerPoolIDAnnotation ]
272+ if ! exists || (lbPoolID == "" ) {
273+ return fmt .Errorf ("no Equinix Metal Load Balancer Pool found in machine's annotations" )
274+ }
275+
276+ log .Info ("Deleting EMLB Pool" , "Cluster Metro" , e .metro , "Cluster Name" , clusterName , "Project ID" , e .projectID , "Pool ID" , lbPoolID )
277+
278+ resp , err := e .deletePool (ctx , lbPoolID )
279+ if err != nil {
280+ if resp .StatusCode != http .StatusNotFound {
281+ return nil
282+ }
283+ log .Error (err , "LB Pool Delete Failed" , "Pool ID" , lbPoolID , "Response Body" , resp .Body )
284+ }
285+
286+ return err
287+ }
288+
236289// getLoadBalancer Returns a Load Balancer object given an id.
237- func (e * EMLB ) getLoadBalancer (ctx context.Context , id string ) (* lbaas.LoadBalancer , error ) {
290+ func (e * EMLB ) getLoadBalancer (ctx context.Context , id string ) (* lbaas.LoadBalancer , * http. Response , error ) {
238291 ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
239292
240- LoadBalancer , _ , err := e .client .LoadBalancersApi .GetLoadBalancer (ctx , id ).Execute ()
241- return LoadBalancer , err
293+ LoadBalancer , resp , err := e .client .LoadBalancersApi .GetLoadBalancer (ctx , id ).Execute ()
294+ return LoadBalancer , resp , err
242295}
243296
244297// getLoadBalancerPort Returns a Load Balancer Port object given an id.
@@ -350,7 +403,7 @@ func (e *EMLB) ensureLoadBalancer(ctx context.Context, lbID, lbname string, port
350403 }
351404
352405 // Regardless of whether we just created it, fetch the loadbalancer object.
353- lb , err := e .getLoadBalancer (ctx , lbID )
406+ lb , _ , err := e .getLoadBalancer (ctx , lbID )
354407 if err != nil {
355408 return nil , nil , err
356409 }
@@ -401,6 +454,16 @@ func (e *EMLB) createOrigin(ctx context.Context, poolID, originName string, targ
401454 return e .client .PoolsApi .CreateLoadBalancerPoolOrigin (ctx , poolID ).LoadBalancerPoolOriginCreate (createOriginRequest ).Execute ()
402455}
403456
457+ func (e * EMLB ) deleteLoadBalancer (ctx context.Context , lbID string ) (* http.Response , error ) {
458+ ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
459+ return e .client .LoadBalancersApi .DeleteLoadBalancer (ctx , lbID ).Execute ()
460+ }
461+
462+ func (e * EMLB ) deletePool (ctx context.Context , poolID string ) (* http.Response , error ) {
463+ ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
464+ return e .client .PoolsApi .DeleteLoadBalancerPool (ctx , poolID ).Execute ()
465+ }
466+
404467func (e * EMLB ) updateListenerPort (ctx context.Context , poolID , lbPortID string ) (* lbaas.LoadBalancerPort , error ) {
405468 ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
406469
0 commit comments