@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/fsnotify/fsnotify"
29
+ "github.com/go-logr/logr"
29
30
kerrors "k8s.io/apimachinery/pkg/util/errors"
30
31
"k8s.io/apimachinery/pkg/util/sets"
31
32
"k8s.io/apimachinery/pkg/util/wait"
@@ -47,6 +48,7 @@ type CertWatcher struct {
47
48
currentCert * tls.Certificate
48
49
watcher * fsnotify.Watcher
49
50
interval time.Duration
51
+ log logr.Logger
50
52
51
53
certPath string
52
54
keyPath string
@@ -65,6 +67,7 @@ func New(certPath, keyPath string) (*CertWatcher, error) {
65
67
certPath : certPath ,
66
68
keyPath : keyPath ,
67
69
interval : defaultWatchInterval ,
70
+ log : log .WithValues ("cert" , certPath , "key" , keyPath ),
68
71
}
69
72
70
73
// Initial read of certificate and key.
@@ -130,14 +133,14 @@ func (cw *CertWatcher) Start(ctx context.Context) error {
130
133
ticker := time .NewTicker (cw .interval )
131
134
defer ticker .Stop ()
132
135
133
- log .Info ("Starting certificate poll+watcher" , "interval" , cw .interval )
136
+ cw . log .Info ("Starting certificate poll+watcher" , "interval" , cw .interval )
134
137
for {
135
138
select {
136
139
case <- ctx .Done ():
137
140
return cw .watcher .Close ()
138
141
case <- ticker .C :
139
142
if err := cw .ReadCertificate (); err != nil {
140
- log .Error (err , "failed read certificate" )
143
+ cw . log .Error (err , "failed read certificate" )
141
144
}
142
145
}
143
146
}
@@ -160,7 +163,7 @@ func (cw *CertWatcher) Watch() {
160
163
return
161
164
}
162
165
163
- log .Error (err , "certificate watch error" )
166
+ cw . log .Error (err , "certificate watch error" )
164
167
}
165
168
}
166
169
}
@@ -174,7 +177,7 @@ func (cw *CertWatcher) updateCachedCertificate(cert *tls.Certificate, keyPEMBloc
174
177
if cw .currentCert != nil &&
175
178
bytes .Equal (cw .currentCert .Certificate [0 ], cert .Certificate [0 ]) &&
176
179
bytes .Equal (cw .cachedKeyPEMBlock , keyPEMBlock ) {
177
- log .V (7 ).Info ("certificate already cached" )
180
+ cw . log .V (7 ).Info ("certificate already cached" )
178
181
return false
179
182
}
180
183
cw .currentCert = cert
@@ -208,7 +211,7 @@ func (cw *CertWatcher) ReadCertificate() error {
208
211
return nil
209
212
}
210
213
211
- log .Info ("Updated current TLS certificate" )
214
+ cw . log .Info ("Updated current TLS certificate" )
212
215
213
216
// If a callback is registered, invoke it with the new certificate.
214
217
cw .RLock ()
@@ -229,15 +232,15 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
229
232
case event .Op .Has (fsnotify .Chmod ), event .Op .Has (fsnotify .Remove ):
230
233
// If the file was removed or renamed, re-add the watch to the previous name
231
234
if err := cw .watcher .Add (event .Name ); err != nil {
232
- log .Error (err , "error re-watching file" )
235
+ cw . log .Error (err , "error re-watching file" )
233
236
}
234
237
default :
235
238
return
236
239
}
237
240
238
- log .V (1 ).Info ("certificate event" , "event" , event )
241
+ cw . log .V (1 ).Info ("certificate event" , "event" , event )
239
242
if err := cw .ReadCertificate (); err != nil {
240
- log .Error (err , "error re-reading certificate" )
243
+ cw . log .Error (err , "error re-reading certificate" )
241
244
}
242
245
}
243
246
0 commit comments