Skip to content

Commit

Permalink
adding retry for get server by name (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
fevirtus authored Dec 18, 2024
1 parent 8ffa5ec commit 5074727
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions cloud-controller-manager/bizfly/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"regexp"
"strings"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -260,17 +261,31 @@ func serverByID(

func serverByName(ctx context.Context, client *gobizfly.Client, name types.NodeName) (*gobizfly.Server, error) {
klog.V(5).Infof("Looking for server name: %s", string(name))
servers, err := client.CloudServer.List(ctx, &gobizfly.ServerListOptions{})
if err != nil {
return nil, err
}

for _, server := range servers {
klog.V(5).Infof("Server %s", strings.ToLower(server.Name))
if strings.ToLower(server.Name) == string(name) {
return server, nil
for attempt := 1; attempt <= 3; attempt++ {
servers, err := client.CloudServer.List(ctx, &gobizfly.ServerListOptions{})
if err != nil {
klog.V(2).Infof("Error when getting server list, attempt %d: %v", attempt, err)
if attempt < 3 {
time.Sleep(backoff(attempt))
continue
}
return nil, fmt.Errorf("failed to list servers after %d attempts: %w", attempt, err)
}

for _, server := range servers {
if strings.EqualFold(server.Name, string(name)) {
return server, nil
}
}

klog.V(2).Infof("Server %v not found in list, attempt %d", name, attempt)

if attempt < 3 {
time.Sleep(backoff(attempt))
}
}

return nil, cloudprovider.InstanceNotFound
}

Expand Down Expand Up @@ -310,3 +325,7 @@ func serverIDFromProviderID(providerID string) (instanceID string, err error) {
}
return matches[1], nil
}

func backoff(attempt int) time.Duration {
return time.Duration(attempt*(attempt+1)) * time.Second
}

0 comments on commit 5074727

Please sign in to comment.