From ecb5f5d3338e98321dc8b3fc91f8f847488ceb34 Mon Sep 17 00:00:00 2001 From: Michael Weibel Date: Fri, 28 Aug 2026 16:39:54 +0200 Subject: [PATCH] Add pre-create duplicate check and reduce sleep Before creating a load balancer, list existing ones and skip creation if a LB with the same name already exists. This prevents duplicate creation even if the race window is hit. Reduce the reconciliation sleep from 5-7.5s to 500ms. With the pre-check in place, the long delay is no longer required for correctness. --- pkg/cloudscale_ccm/reconcile.go | 5 +---- pkg/internal/actions/actions.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/cloudscale_ccm/reconcile.go b/pkg/cloudscale_ccm/reconcile.go index ef7596a..8eb85de 100644 --- a/pkg/cloudscale_ccm/reconcile.go +++ b/pkg/cloudscale_ccm/reconcile.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "math/rand" "slices" "strings" "time" @@ -749,9 +748,7 @@ func reconcileLbState( break } - // Wait between 5-7.5 seconds between state fetches - // #nosec G404 - wait := time.Duration(5000+rand.Intn(2500)) * time.Millisecond + wait := 500 * time.Millisecond select { case <-ctx.Done(): diff --git a/pkg/internal/actions/actions.go b/pkg/internal/actions/actions.go index 62ca4fb..02b1965 100644 --- a/pkg/internal/actions/actions.go +++ b/pkg/internal/actions/actions.go @@ -9,6 +9,7 @@ import ( "time" "github.com/cloudscale-ch/cloudscale-go-sdk/v6" + "k8s.io/klog/v2" ) type Action interface { @@ -50,6 +51,18 @@ func (a *CreateLbAction) Label() string { func (a *CreateLbAction) Run( ctx context.Context, client *cloudscale.Client) (Control, error) { + existing, err := client.LoadBalancers.List(ctx) + if err == nil { + for _, lb := range existing { + if lb.Name == a.lb.Name { + klog.InfoS("lb already exists, skipping create", + "name", a.lb.Name, "uuid", lb.UUID) + + return Refresh, nil + } + } + } + addrs := make([]cloudscale.VIPAddressRequest, 0, len(a.lb.VIPAddresses)) for _, addr := range a.lb.VIPAddresses { addrs = append(addrs, cloudscale.VIPAddressRequest{ @@ -58,7 +71,7 @@ func (a *CreateLbAction) Run( }) } - _, err := client.LoadBalancers.Create(ctx, &cloudscale.LoadBalancerRequest{ + _, err = client.LoadBalancers.Create(ctx, &cloudscale.LoadBalancerRequest{ Name: a.lb.Name, Flavor: a.lb.Flavor.Slug, VIPAddresses: &addrs,