@@ -778,8 +778,22 @@ func (c *client) ReconcileLoadBalancer(
778778 return nil
779779}
780780
781+ // isLBRuleNotFound checks if an error from the CloudStack API indicates that a load balancer rule does not exist.
782+ func isLBRuleNotFound (err error ) bool {
783+ if err == nil {
784+ return false
785+ }
786+ msg := strings .ToLower (err .Error ())
787+
788+ return strings .Contains (msg , "no match found" ) ||
789+ strings .Contains (msg , "unable to find" ) ||
790+ strings .Contains (msg , "does not exist" ) ||
791+ strings .Contains (msg , "entity does not exist" )
792+ }
793+
781794// AssignVMToLoadBalancerRules assigns a VM to the load balancing rules listed in isoNet.Status.LoadBalancerRuleIDs,
782795// if not already assigned. It returns a bool indicating whether an instance was actually assigned, and an error in case an error occurred.
796+ // If a load balancer rule no longer exists in CloudStack, it is skipped.
783797func (c * client ) AssignVMToLoadBalancerRules (isoNet * infrav1.CloudStackIsolatedNetwork , instanceID string ) (bool , error ) {
784798 var assigned , found bool
785799 assigned = false
@@ -790,6 +804,14 @@ func (c *client) AssignVMToLoadBalancerRules(isoNet *infrav1.CloudStackIsolatedN
790804 lbRuleInstances , err := c .cs .LoadBalancer .ListLoadBalancerRuleInstances (
791805 c .cs .LoadBalancer .NewListLoadBalancerRuleInstancesParams (lbRuleID ))
792806 if err != nil {
807+ if isLBRuleNotFound (err ) {
808+ // The load balancer rule no longer exists in CloudStack. Skip it; the
809+ // isolated network controller will re-create it on its next reconciliation.
810+ record .Warnf (isoNet , "LoadBalancerRuleNotFound" ,
811+ "Load balancer rule %s no longer exists, skipping assignment for instance %s" , lbRuleID , instanceID )
812+
813+ continue
814+ }
793815 c .customMetrics .EvaluateErrorAndIncrementAcsReconciliationErrorCounter (err )
794816
795817 return false , err
@@ -822,13 +844,22 @@ func (c *client) AssignVMToLoadBalancerRules(isoNet *infrav1.CloudStackIsolatedN
822844
823845// RemoveVMFromLoadBalancerRules removes a VM from the load balancing rules listed in isoNet.Status.LoadBalancerRuleIDs,
824846// if not already removed. It returns a bool indicating whether an instance was actually removed, and an error in case an error occurred.
847+ // If a load balancer rule no longer exists in CloudStack, it is skipped (the VM is implicitly not assigned).
825848func (c * client ) RemoveVMFromLoadBalancerRules (isoNet * infrav1.CloudStackIsolatedNetwork , instanceID string ) (bool , error ) {
826849 for _ , lbRuleID := range isoNet .Status .LoadBalancerRuleIDs {
827850 // Check that the instance isn't already in LB rotation.
828851 found := false
829852 lbRuleInstances , err := c .cs .LoadBalancer .ListLoadBalancerRuleInstances (
830853 c .cs .LoadBalancer .NewListLoadBalancerRuleInstancesParams (lbRuleID ))
831854 if err != nil {
855+ if isLBRuleNotFound (err ) {
856+ // The load balancer rule no longer exists in CloudStack, so the VM
857+ // is implicitly not assigned to it. Skip it.
858+ record .Warnf (isoNet , "LoadBalancerRuleNotFound" ,
859+ "Load balancer rule %s no longer exists, skipping removal for instance %s" , lbRuleID , instanceID )
860+
861+ continue
862+ }
832863 c .customMetrics .EvaluateErrorAndIncrementAcsReconciliationErrorCounter (err )
833864
834865 return false , err
0 commit comments