diff --git a/kube/certs/certs.go b/kube/certs/certs.go index 0432e89b4..4adfb80b8 100644 --- a/kube/certs/certs.go +++ b/kube/certs/certs.go @@ -9,6 +9,7 @@ import ( "context" "errors" "fmt" + mathrand "math/rand/v2" "net" "slices" "sync" @@ -147,7 +148,9 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { cm.logf("error waiting for cert domain %s: %v", domain, err) } - timer := time.NewTimer(0) // fire off timer immediately + // Stagger initial fire so that when several domains come online at + // once we don't stampede tailscaled's shared cert mutex. + timer := time.NewTimer(mathrand.N(initialJitter)) defer timer.Stop() retryCount := 0 for { @@ -216,6 +219,11 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { // waiting. var (not const) so tests can shorten it. var waitForCertDomainHeartbeat = 5 * time.Minute +// initialJitter is the max random delay before a cert loop's first CertPair +// call. Spreads startup load across the shared cert mutex in tailscaled. +// var (not const) so tests can shorten it. +var initialJitter = 60 * time.Second + // domains before issuing the cert for the first time. It uses the IPN bus // only as a wake-up trigger (Notify.SelfChange) and queries the current // cert domains explicitly via [LocalClient.CertDomains].