@@ -243,18 +243,21 @@ func (e *EMLB) DeleteLoadBalancer(ctx context.Context, clusterScope *scope.Clust
243
243
// Make sure the cluster already has an EMLB ID in its packetCluster annotations, otherwise abort.
244
244
lbID, exists := packetCluster.Annotations[loadBalancerIDAnnotation]
245
245
if !exists || (lbID == "") {
246
- return fmt.Errorf("no Equinix Metal Load Balancer found in cluster's annotations")
246
+ log.Info("no Equinix Metal Load Balancer found in cluster's annotations, skipping EMLB delete")
247
+ return nil
247
248
}
248
249
249
250
// See if the EMLB has a Port ID in its packetCluster annotations.
250
251
lbPortNumber, exists := packetCluster.Annotations[loadBalancerPortNumberAnnotation]
251
252
if !exists || (lbPortNumber == "") {
252
- return fmt.Errorf("no Equinix Metal Load Balancer Port Number found in cluster's annotations")
253
+ log.Info("no Equinix Metal Load Balancer Port Number found in cluster's annotations, skipping EMLB delete")
254
+ return nil
253
255
}
254
256
255
257
log.Info("Deleting EMLB", "Cluster Metro", e.metro, "Cluster Name", clusterName, "Project ID", e.projectID, "Load Balancer ID", lbID)
256
258
257
259
// Fetch the Load Balancer object.
260
+ // Skip if 404, otherwise error
258
261
lb, err := e.getLoadBalancer(ctx, lbID)
259
262
if err != nil {
260
263
log.Error(err, "failed to load the loadbalancer object, cannot proceed with deletion")
@@ -269,6 +272,7 @@ func (e *EMLB) DeleteLoadBalancer(ctx context.Context, clusterScope *scope.Clust
269
272
}
270
273
271
274
// Get the entire listener port object.
275
+ // Skip if 404, otherwise error
272
276
lbPort, err := e.getLoadBalancerPort(ctx, lbID, int32(portNumber))
273
277
if err != nil {
274
278
log.Error(err, "failed to load the loadbalancer port object, cannot proceed with deletion")
@@ -290,6 +294,52 @@ func (e *EMLB) DeleteLoadBalancer(ctx context.Context, clusterScope *scope.Clust
290
294
return nil
291
295
}
292
296
297
+ // DeleteLoadBalancerOrigin deletes the Equinix Metal Load Balancer associated with a given ClusterScope.
298
+ func (e *EMLB) DeleteLoadBalancerOrigin(ctx context.Context, machineScope *scope.MachineScope) error {
299
+ log := ctrl.LoggerFrom(ctx)
300
+
301
+ clusterName := machineScope.Cluster.Name
302
+
303
+ // Make sure the machine has an EMLB Pool ID in its packetMachine annotations, otherwise abort.
304
+ lbPoolID, exists := machineScope.PacketMachine.Annotations[loadBalancerPoolIDAnnotation]
305
+ if !exists || (lbPoolID == "") {
306
+ return fmt.Errorf("no Equinix Metal Load Balancer Pool found in machine's annotations")
307
+ }
308
+
309
+ // Make sure the machine has an EMLB Origin ID in its packetMachine annotations, otherwise abort.
310
+ lbOriginID, exists := machineScope.PacketMachine.Annotations[loadBalancerOriginIDAnnotation]
311
+ if !exists || (lbOriginID == "") {
312
+ return fmt.Errorf("no Equinix Metal Load Balancer Origin found in machine's annotations")
313
+ }
314
+
315
+ log.Info("Deleting EMLB Origin from Pool", "Cluster Metro", e.metro, "Cluster Name", clusterName, "Project ID", e.projectID, "Pool ID", lbPoolID, "Origin ID", lbOriginID)
316
+
317
+ // Fetch the Load Balancer Pool object.
318
+ lbPool, err := e.getLoadBalancerPool(ctx, lbPoolID)
319
+ if err != nil {
320
+ log.Error(err, "failed to load the loadbalancer pool object, cannot proceed with deletion")
321
+ return err
322
+ }
323
+
324
+ resp, err := e.deleteOrigin(ctx, lbOriginID)
325
+ if err != nil {
326
+ log.Error(err, "LB Origin Delete Failed", "Pool ID", lbPool.GetId(), "Origin ID", lbOriginID, "Response Body", resp.Body)
327
+ return err
328
+ }
329
+
330
+ // TODO: Validate pool is empty
331
+
332
+ log.Info("Deleting EMLB Pool", "Cluster Metro", e.metro, "Cluster Name", clusterName, "Project ID", e.projectID, "Pool ID", lbPoolID)
333
+
334
+ resp, err = e.deletePool(ctx, lbPool.GetId())
335
+ if err != nil {
336
+ log.Error(err, "LB Pool Delete Failed", "Pool ID", lbPool.GetId(), "Response Body", resp.Body)
337
+ return err
338
+ }
339
+
340
+ return nil
341
+ }
342
+
293
343
// getLoadBalancer Returns a Load Balancer object given an id.
294
344
func (e *EMLB) getLoadBalancer(ctx context.Context, id string) (*lbaas.LoadBalancer, error) {
295
345
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
@@ -306,6 +356,14 @@ func (e *EMLB) getLoadBalancerPort(ctx context.Context, id string, portNumber in
306
356
return LoadBalancerPort, err
307
357
}
308
358
359
+ // getLoadBalancerPool Returns a Load Balancer Pool object given an id.
360
+ func (e *EMLB) getLoadBalancerPool(ctx context.Context, id string) (*lbaas.LoadBalancerPool, error) {
361
+ ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
362
+
363
+ LoadBalancerPool, _, err := e.client.PoolsApi.GetLoadBalancerPool(ctx, id).Execute()
364
+ return LoadBalancerPool, err
365
+ }
366
+
309
367
// EnsureLoadBalancerOrigin takes the devices list of IP addresses in a Load Balancer Origin Pool and ensures an origin
310
368
// for the first IPv4 address in the list exists.
311
369
func (e *EMLB) ensureLoadBalancerOrigin(ctx context.Context, originID, poolID, lbName string, deviceAddr []corev1.NodeAddress) (*lbaas.LoadBalancerPoolOrigin, error) {
@@ -459,18 +517,22 @@ func (e *EMLB) createOrigin(ctx context.Context, poolID, originName string, targ
459
517
}
460
518
461
519
func (e *EMLB) deleteLoadBalancer(ctx context.Context, lbID string) (*http.Response, error) {
520
+ ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
462
521
return e.client.LoadBalancersApi.DeleteLoadBalancer(ctx, lbID).Execute()
463
522
}
464
523
465
524
func (e *EMLB) deleteListenerPort(ctx context.Context, portID string) (*http.Response, error) {
525
+ ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
466
526
return e.client.PortsApi.DeleteLoadBalancerPort(ctx, portID).Execute()
467
527
}
468
528
469
529
func (e *EMLB) deletePool(ctx context.Context, poolID string) (*http.Response, error) {
530
+ ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
470
531
return e.client.PoolsApi.DeleteLoadBalancerPool(ctx, poolID).Execute()
471
532
}
472
533
473
534
func (e *EMLB) deleteOrigin(ctx context.Context, originID string) (*http.Response, error) {
535
+ ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
474
536
return e.client.OriginsApi.DeleteLoadBalancerOrigin(ctx, originID).Execute()
475
537
}
476
538
0 commit comments