Skip to content

Commit

Permalink
Adjust kubeip GCP address listing to include random sleep for better …
Browse files Browse the repository at this point in the history
…address allocation distribution. Add max wait time constant to control sleep duration.
  • Loading branch information
alexei-led committed Mar 27, 2024
1 parent 902814f commit 376454b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/gcp/gke.tf
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,9 @@ resource "kubernetes_daemonset" "kubeip_daemonset" {
termination_grace_period_seconds = 30
priority_class_name = "system-node-critical"
container {
name = "kubeip-agent"
image = "doitintl/kubeip-agent:${var.kubeip_version}"
name = "kubeip-agent"
image = "doitintl/kubeip-agent:${var.kubeip_version}"
image_pull_policy = "Always"
env {
name = "NODE_NAME"
value_from {
Expand Down
7 changes: 7 additions & 0 deletions internal/address/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package address
import (
"context"
"fmt"
"math/rand"
"strings"
"time"

Expand All @@ -26,6 +27,7 @@ const (
accessConfigKind = "compute#accessConfig"
defaultPrefixLength = 96
maxRetries = 10 // number of retries for assigning ephemeral public IP address
maxWaitListTime = 10 // max time to wait before listing addresses
)

var (
Expand Down Expand Up @@ -221,6 +223,11 @@ func (a *gcpAssigner) Assign(ctx context.Context, instanceID, zone string, filte
return errors.Wrapf(err, "check if static public IP is already assigned to instance %s", instanceID)
}

// add random sleep to reduce the chance of multiple kubeip instances getting the same address list
waitTime := time.Duration(rand.Intn(maxWaitListTime)) * time.Second //nolint:gosec
a.logger.WithField("waitTime", waitTime).Debug("waiting before listing addresses")
time.Sleep(waitTime)

// get available reserved public IP addresses
addresses, err := a.listAddresses(filter, orderBy, reservedStatus)
if err != nil {
Expand Down

0 comments on commit 376454b

Please sign in to comment.