Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions pkg/certwatcher/certwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -47,6 +48,7 @@ type CertWatcher struct {
currentCert *tls.Certificate
watcher *fsnotify.Watcher
interval time.Duration
log logr.Logger

certPath string
keyPath string
Expand All @@ -65,6 +67,7 @@ func New(certPath, keyPath string) (*CertWatcher, error) {
certPath: certPath,
keyPath: keyPath,
interval: defaultWatchInterval,
log: log.WithValues("cert", certPath, "key", keyPath),
}

// Initial read of certificate and key.
Expand Down Expand Up @@ -130,14 +133,14 @@ func (cw *CertWatcher) Start(ctx context.Context) error {
ticker := time.NewTicker(cw.interval)
defer ticker.Stop()

log.Info("Starting certificate poll+watcher", "interval", cw.interval)
cw.log.Info("Starting certificate poll+watcher", "interval", cw.interval)
for {
select {
case <-ctx.Done():
return cw.watcher.Close()
case <-ticker.C:
if err := cw.ReadCertificate(); err != nil {
log.Error(err, "failed read certificate")
cw.log.Error(err, "failed read certificate")
}
}
}
Expand All @@ -160,7 +163,7 @@ func (cw *CertWatcher) Watch() {
return
}

log.Error(err, "certificate watch error")
cw.log.Error(err, "certificate watch error")
}
}
}
Expand All @@ -174,7 +177,7 @@ func (cw *CertWatcher) updateCachedCertificate(cert *tls.Certificate, keyPEMBloc
if cw.currentCert != nil &&
bytes.Equal(cw.currentCert.Certificate[0], cert.Certificate[0]) &&
bytes.Equal(cw.cachedKeyPEMBlock, keyPEMBlock) {
log.V(7).Info("certificate already cached")
cw.log.V(7).Info("certificate already cached")
return false
}
cw.currentCert = cert
Expand Down Expand Up @@ -208,7 +211,7 @@ func (cw *CertWatcher) ReadCertificate() error {
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a logger for CertWatcher in ~ l.68 and then use that one everywhere instead

	cw := &CertWatcher{
		certPath: certPath,
		keyPath:  keyPath,
		interval: defaultWatchInterval,
		log:      log.WithValues("cert", certPath, "key", keyPath),
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, this simplifies similar changes in the future. As you suggested, I've updated the code.


log.Info("Updated current TLS certificate")
cw.log.Info("Updated current TLS certificate")

// If a callback is registered, invoke it with the new certificate.
cw.RLock()
Expand All @@ -229,15 +232,15 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
case event.Op.Has(fsnotify.Chmod), event.Op.Has(fsnotify.Remove):
// If the file was removed or renamed, re-add the watch to the previous name
if err := cw.watcher.Add(event.Name); err != nil {
log.Error(err, "error re-watching file")
cw.log.Error(err, "error re-watching file")
}
default:
return
}

log.V(1).Info("certificate event", "event", event)
cw.log.V(1).Info("certificate event", "event", event)
if err := cw.ReadCertificate(); err != nil {
log.Error(err, "error re-reading certificate")
cw.log.Error(err, "error re-reading certificate")
}
}

Expand Down