From 9e318c81ee50de9914fba367bcb20c300aac15fb Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Wed, 24 Jun 2026 16:09:46 +0000 Subject: [PATCH] kube/certs: don't advance retryCount on transient errors Failures that never reached the CA -- ctx timeouts and LocalAPI socket errors (ECONNREFUSED, ECONNRESET, EHOSTUNREACH, EPIPE, net.Error Timeout()) -- get a flat 1m retry with no retryCount advance. Escalating the schedule for them is wrong. --- kube/certs/certs.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/kube/certs/certs.go b/kube/certs/certs.go index 036c15ff9..b7d79cf8e 100644 --- a/kube/certs/certs.go +++ b/kube/certs/certs.go @@ -12,6 +12,7 @@ import ( "net" "slices" "sync" + "syscall" "time" "tailscale.com/client/local" @@ -96,6 +97,26 @@ func (cm *CertManager) EnsureCertLoops(ctx context.Context, sc *ipn.ServeConfig) return nil } +// isTransientCertErr reports whether err represents a failure that did not +// reach the CA (ctx timeout, LocalAPI socket unreachable). Such errors must +// not advance the loop's retryCount. +func isTransientCertErr(err error) bool { + switch { + case errors.Is(err, context.DeadlineExceeded), + errors.Is(err, context.Canceled), + errors.Is(err, syscall.ECONNREFUSED), + errors.Is(err, syscall.ECONNRESET), + errors.Is(err, syscall.EHOSTUNREACH), + errors.Is(err, syscall.EPIPE): + return true + } + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { + return true + } + return false +} + // retrySchedule is the wait between successive failed issuance attempts. // It follows the schedule that Let's Encrypt's rate-limit adjustment guidance // recommends ("1 minute, then 10 minutes, then 100 minutes, then once per @@ -160,17 +181,15 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { ctxT, cancel := context.WithTimeout(ctx, 30*time.Minute) _, _, err := cm.lc.CertPair(ctxT, domain) cancel() - if err != nil { - cm.logf("error refreshing certificate for %s: %v", domain, err) - } var nextInterval time.Duration - // TODO(irbekrm): distinguish between transient transport errors - // (timeout, connection reset) and genuine CA-side failures so - // the former do not advance retryCount. - if err == nil { + switch { + case err == nil: retryCount = 0 nextInterval = normalInterval - } else { + case isTransientCertErr(err): + // Never reached the CA. Don't escalate. + nextInterval = retrySchedule[0] + default: retryCount++ idx := retryCount - 1 if idx >= len(retrySchedule) { @@ -183,6 +202,8 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { if errors.As(err, &rle) && rle.RetryAfter > 0 { nextInterval = rle.RetryAfter } + } + if err != nil { cm.logf("Error refreshing certificate for %s (retry %d): %v. Will retry in %v\n", domain, retryCount, err, nextInterval) }