@@ -99,7 +99,7 @@ func NewEMLB(metalAPIKey, projectID, metro string) *EMLB {
99
99
return manager
100
100
}
101
101
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 .
103
103
func (e * EMLB ) ReconcileLoadBalancer (ctx context.Context , clusterScope * scope.ClusterScope ) error {
104
104
log := ctrl .LoggerFrom (ctx )
105
105
@@ -233,6 +233,63 @@ func (e *EMLB) ReconcileVIPOrigin(ctx context.Context, machineScope *scope.Machi
233
233
return nil
234
234
}
235
235
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
+ return fmt .Errorf ("no Equinix Metal Load Balancer found in cluster's annotations" )
247
+ }
248
+
249
+ // See if the EMLB has a Port ID in its packetCluster annotations.
250
+ lbPortNumber , exists := packetCluster .Annotations [loadBalancerPortNumberAnnotation ]
251
+ if ! exists || (lbPortNumber == "" ) {
252
+ return fmt .Errorf ("no Equinix Metal Load Balancer Port Number found in cluster's annotations" )
253
+ }
254
+
255
+ log .Info ("Deleting EMLB" , "Cluster Metro" , e .metro , "Cluster Name" , clusterName , "Project ID" , e .projectID , "Load Balancer ID" , lbID )
256
+
257
+ // Fetch the Load Balancer object.
258
+ lb , err := e .getLoadBalancer (ctx , lbID )
259
+ if err != nil {
260
+ log .Error (err , "failed to load the loadbalancer object, cannot proceed with deletion" )
261
+ return err
262
+ }
263
+
264
+ // Get an int version of the listener port number.
265
+ portNumber , err := strconv .ParseInt (lbPortNumber , 10 , 32 )
266
+ if err != nil {
267
+ log .Error (err , "failed to convert the loadbalancer port number to an int, cannot proceed with deletion" )
268
+ return err
269
+ }
270
+
271
+ // Get the entire listener port object.
272
+ lbPort , err := e .getLoadBalancerPort (ctx , lbID , int32 (portNumber ))
273
+ if err != nil {
274
+ log .Error (err , "failed to load the loadbalancer port object, cannot proceed with deletion" )
275
+ return err
276
+ }
277
+
278
+ resp , err := e .deleteListenerPort (ctx , lbPort .GetId ())
279
+ if err != nil {
280
+ log .Error (err , "LB Port Delete Failed" , "EMLB ID" , lb .GetId (), "Port ID" , lbPort .GetId (), "Response Body" , resp .Body )
281
+ return err
282
+ }
283
+
284
+ resp , err = e .deleteLoadBalancer (ctx , lb .GetId ())
285
+ if err != nil {
286
+ log .Error (err , "LB Delete Failed" , "EMLB ID" , lb .GetId (), "Response Body" , resp .Body )
287
+ return err
288
+ }
289
+
290
+ return nil
291
+ }
292
+
236
293
// getLoadBalancer Returns a Load Balancer object given an id.
237
294
func (e * EMLB ) getLoadBalancer (ctx context.Context , id string ) (* lbaas.LoadBalancer , error ) {
238
295
ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
@@ -401,6 +458,22 @@ func (e *EMLB) createOrigin(ctx context.Context, poolID, originName string, targ
401
458
return e .client .PoolsApi .CreateLoadBalancerPoolOrigin (ctx , poolID ).LoadBalancerPoolOriginCreate (createOriginRequest ).Execute ()
402
459
}
403
460
461
+ func (e * EMLB ) deleteLoadBalancer (ctx context.Context , lbID string ) (* http.Response , error ) {
462
+ return e .client .LoadBalancersApi .DeleteLoadBalancer (ctx , lbID ).Execute ()
463
+ }
464
+
465
+ func (e * EMLB ) deleteListenerPort (ctx context.Context , portID string ) (* http.Response , error ) {
466
+ return e .client .PortsApi .DeleteLoadBalancerPort (ctx , portID ).Execute ()
467
+ }
468
+
469
+ func (e * EMLB ) deletePool (ctx context.Context , poolID string ) (* http.Response , error ) {
470
+ return e .client .PoolsApi .DeleteLoadBalancerPool (ctx , poolID ).Execute ()
471
+ }
472
+
473
+ func (e * EMLB ) deleteOrigin (ctx context.Context , originID string ) (* http.Response , error ) {
474
+ return e .client .OriginsApi .DeleteLoadBalancerOrigin (ctx , originID ).Execute ()
475
+ }
476
+
404
477
func (e * EMLB ) updateListenerPort (ctx context.Context , poolID , lbPortID string ) (* lbaas.LoadBalancerPort , error ) {
405
478
ctx = context .WithValue (ctx , lbaas .ContextOAuth2 , e .tokenExchanger )
406
479
0 commit comments