Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit cebd848

Browse files
committed
fix: move forward even though there are errors
Signed-off-by: Chris Privitere <[email protected]>
1 parent e61b829 commit cebd848

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

internal/emlb/emlb.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,21 @@ func (e *EMLB) DeleteLoadBalancer(ctx context.Context, clusterScope *scope.Clust
243243
// Make sure the cluster already has an EMLB ID in its packetCluster annotations, otherwise abort.
244244
lbID, exists := packetCluster.Annotations[loadBalancerIDAnnotation]
245245
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
247248
}
248249

249250
// See if the EMLB has a Port ID in its packetCluster annotations.
250251
lbPortNumber, exists := packetCluster.Annotations[loadBalancerPortNumberAnnotation]
251252
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
253255
}
254256

255257
log.Info("Deleting EMLB", "Cluster Metro", e.metro, "Cluster Name", clusterName, "Project ID", e.projectID, "Load Balancer ID", lbID)
256258

257259
// Fetch the Load Balancer object.
260+
// Skip if 404, otherwise error
258261
lb, err := e.getLoadBalancer(ctx, lbID)
259262
if err != nil {
260263
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
269272
}
270273

271274
// Get the entire listener port object.
275+
// Skip if 404, otherwise error
272276
lbPort, err := e.getLoadBalancerPort(ctx, lbID, int32(portNumber))
273277
if err != nil {
274278
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
290294
return nil
291295
}
292296

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+
293343
// getLoadBalancer Returns a Load Balancer object given an id.
294344
func (e *EMLB) getLoadBalancer(ctx context.Context, id string) (*lbaas.LoadBalancer, error) {
295345
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
@@ -306,6 +356,14 @@ func (e *EMLB) getLoadBalancerPort(ctx context.Context, id string, portNumber in
306356
return LoadBalancerPort, err
307357
}
308358

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+
309367
// EnsureLoadBalancerOrigin takes the devices list of IP addresses in a Load Balancer Origin Pool and ensures an origin
310368
// for the first IPv4 address in the list exists.
311369
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
459517
}
460518

461519
func (e *EMLB) deleteLoadBalancer(ctx context.Context, lbID string) (*http.Response, error) {
520+
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
462521
return e.client.LoadBalancersApi.DeleteLoadBalancer(ctx, lbID).Execute()
463522
}
464523

465524
func (e *EMLB) deleteListenerPort(ctx context.Context, portID string) (*http.Response, error) {
525+
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
466526
return e.client.PortsApi.DeleteLoadBalancerPort(ctx, portID).Execute()
467527
}
468528

469529
func (e *EMLB) deletePool(ctx context.Context, poolID string) (*http.Response, error) {
530+
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
470531
return e.client.PoolsApi.DeleteLoadBalancerPool(ctx, poolID).Execute()
471532
}
472533

473534
func (e *EMLB) deleteOrigin(ctx context.Context, originID string) (*http.Response, error) {
535+
ctx = context.WithValue(ctx, lbaas.ContextOAuth2, e.tokenExchanger)
474536
return e.client.OriginsApi.DeleteLoadBalancerOrigin(ctx, originID).Execute()
475537
}
476538

0 commit comments

Comments
 (0)