Skip to content

Commit 17704af

Browse files
committed
fix the bad-certificate issue when using tls-auto
1 parent 1a8db92 commit 17704af

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

helper/cert/notify.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ package cert
55

66
import (
77
"context"
8+
"errors"
89
"sync"
910
"time"
1011

1112
"github.com/hashicorp/go-hclog"
1213
)
1314

15+
var CertificateValidErr = errors.New("cert still valid, continue to next round")
16+
1417
func NewNotify(ctx context.Context, newBundle chan<- Bundle, notifyOnce chan<- bool, source Source, logger hclog.Logger) *Notify {
1518
return &Notify{
1619
ctx: ctx,
@@ -62,7 +65,13 @@ func (n *Notify) Run() {
6265

6366
next, err := n.source.Certificate(n.ctx, last)
6467
if err != nil {
65-
n.logger.Warn("error loading next cert", "error", err.Error())
68+
switch err {
69+
case CertificateValidErr:
70+
n.logger.Info("valid cert", "info", err.Error())
71+
default:
72+
n.logger.Warn("error loading next cert", "error", err.Error())
73+
}
74+
6675
continue
6776
}
6877

helper/cert/source_gen.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (s *GenSource) Certificate(ctx context.Context, last *Bundle) (Bundle, erro
130130

131131
// If we have a prior cert, we wait for getting near to the expiry
132132
// (within 30 minutes arbitrarily chosen).
133+
var certValid = false
133134
if last != nil {
134135
// We have a prior certificate, let's parse it to get the expiry
135136
cert, err := parseCert(last.Cert)
@@ -142,22 +143,28 @@ func (s *GenSource) Certificate(ctx context.Context, last *Bundle) (Bundle, erro
142143
waitTime = 1 * time.Millisecond
143144
}
144145

145-
timer := time.NewTimer(waitTime)
146-
defer timer.Stop()
146+
if waitTime > 5*time.Minute {
147+
certValid = true
148+
waitTime = 5 * time.Minute
149+
}
147150

148151
select {
149152
case <-leaderCh:
150153
s.Log.Debug("got a leadership change, returning")
151154
return result, fmt.Errorf("lost leadership")
152155

153-
case <-timer.C:
156+
case <-time.After(waitTime):
154157
// Fall through, generate cert
155158

156159
case <-ctx.Done():
157160
return result, ctx.Err()
158161
}
159162
}
160163

164+
if certValid {
165+
return result, CertificateValidErr
166+
}
167+
161168
// Generate cert, set it on the result, and return
162169
cert, key, err := s.generateCert()
163170
if err != nil {

subcommand/injector/command.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ func (c *Command) certWatcher(ctx context.Context, ch <-chan cert.Bundle, client
363363
}
364364

365365
defaultLoopTime := 1 * time.Hour // update after this amount of time even if nothing has happened
366-
timer := time.NewTimer(defaultLoopTime)
367366
expBackoff := backoff.NewExponentialBackOff()
367+
interval := defaultLoopTime
368368

369369
for {
370370
select {
@@ -380,26 +380,17 @@ func (c *Command) certWatcher(ctx context.Context, ch <-chan cert.Bundle, client
380380
// Quit
381381
return
382382

383-
case <-timer.C:
383+
case <-time.After(interval):
384384
// we are told to retry or periodically update
385385
}
386386

387-
// clear the timer
388-
if !timer.Stop() {
389-
// non-blocking drain
390-
select {
391-
case <-timer.C:
392-
default:
393-
}
394-
}
395-
396387
err := c.updateCertificate(ctx, clientset, bundle, webhooksCache, leaderElector, log)
397388
if err != nil {
398389
// retry after a delay
399-
timer.Reset(expBackoff.NextBackOff())
390+
interval = expBackoff.NextBackOff()
400391
} else {
401392
expBackoff.Reset()
402-
timer.Reset(defaultLoopTime)
393+
interval = defaultLoopTime
403394
}
404395
}
405396
}

0 commit comments

Comments
 (0)