Skip to content

Commit

Permalink
fix: update service annotation after deleting forwarding rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-github committed Jul 16, 2024
1 parent 6c7f2cd commit 7819956
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions internal/xelon/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ func (l *loadBalancers) GetLoadBalancerName(_ context.Context, _ string, service
return cloudprovider.DefaultLoadBalancerName(service)
}

func (l *loadBalancers) EnsureLoadBalancer(ctx context.Context, _ string, service *v1.Service, _ []*v1.Node) (*v1.LoadBalancerStatus, error) {
func (l *loadBalancers) EnsureLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
logger := klog.FromContext(ctx).WithValues("method", "EnsureLoadBalancer", "service", getServiceNameWithNamespace(service))

l.Lock()
defer l.Unlock()

xlb, err := l.retrieveXelonLoadBalancer(ctx, service)
if err != nil {
switch {
Expand All @@ -133,7 +130,7 @@ func (l *loadBalancers) EnsureLoadBalancer(ctx context.Context, _ string, servic
}
}

err = l.updateLoadBalancer(ctx, xlb, service)
err = l.UpdateLoadBalancer(ctx, clusterName, service, nodes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -411,20 +408,29 @@ func (l *loadBalancers) updateLoadBalancer(ctx context.Context, xlb *xelonLoadBa
logger := configureLogger(ctx, "updateLoadBalancer").WithValues(
"service", getServiceNameWithNamespace(service),
)

l.Lock()
defer l.Unlock()

patcher := newServicePatcher(l.client.k8s, service)
defer func() { _ = patcher.Patch(ctx) }()

// clean up old rules
if forwardingRuleIDs, ok := service.Annotations[serviceAnnotationLoadBalancerClusterForwardingRuleIDs]; ok && forwardingRuleIDs != "" {
logger.Info("Deleting previously specified forwarding rules", "forwarding_rules_ids", forwardingRuleIDs)
logger.Info("Deleting previously specified forwarding rules", "forwarding_rule_ids", forwardingRuleIDs)
definedForwardingRuleIDs := strings.Split(forwardingRuleIDs, ",")

for _, definedForwardingRuleID := range definedForwardingRuleIDs {
_, err := l.client.xelon.LoadBalancerClusters.DeleteForwardingRule(ctx, xlb.clusterID, xlb.virtualIPID, definedForwardingRuleID)
resp, err := l.client.xelon.LoadBalancerClusters.DeleteForwardingRule(ctx, xlb.clusterID, xlb.virtualIPID, definedForwardingRuleID)
if err != nil {
return err
if resp != nil && resp.StatusCode == http.StatusNotFound {
logger.Info("Skipping not existing forwarding rule", "forwarding_rule_id", definedForwardingRuleID)
} else {
return err
}
}
}
updateServiceAnnotation(service, serviceAnnotationLoadBalancerClusterForwardingRuleIDs, "")
}

// build forwarding rules
Expand All @@ -442,6 +448,7 @@ func (l *loadBalancers) updateLoadBalancer(ctx context.Context, xlb *xelonLoadBa
if err != nil {
return err
}
logger.Info("Created forwarding rules", "rules", rules)

var frontendRules []string
for _, rule := range rules {
Expand All @@ -450,6 +457,7 @@ func (l *loadBalancers) updateLoadBalancer(ctx context.Context, xlb *xelonLoadBa
}
}

logger.Info("Applying forwarding rules annotation", "forwarding_rules_ids", strings.Join(frontendRules, ","))
updateServiceAnnotation(service, serviceAnnotationLoadBalancerClusterForwardingRuleIDs, strings.Join(frontendRules, ","))

return nil
Expand Down

0 comments on commit 7819956

Please sign in to comment.