@@ -124,6 +124,9 @@ type Config struct {
124
124
// allows one to setup monitoring and alerting of unexpected rotation
125
125
// behavior and track trends in rotation frequency.
126
126
CertificateRotation Histogram
127
+ // CertifcateRenewFailure will record a metric that keeps track of
128
+ // certificate renewal failures.
129
+ CertificateRenewFailure Counter
127
130
}
128
131
129
132
// Store is responsible for getting and updating the current certificate.
@@ -154,6 +157,11 @@ type Histogram interface {
154
157
Observe (float64 )
155
158
}
156
159
160
+ // Counter will wrap a counter with labels
161
+ type Counter interface {
162
+ Inc ()
163
+ }
164
+
157
165
// NoCertKeyError indicates there is no cert/key currently available.
158
166
type NoCertKeyError string
159
167
@@ -177,8 +185,9 @@ type manager struct {
177
185
178
186
certStore Store
179
187
180
- certificateExpiration Gauge
181
- certificateRotation Histogram
188
+ certificateExpiration Gauge
189
+ certificateRotation Histogram
190
+ certificateRenewFailure Counter
182
191
183
192
// the following variables must only be accessed under certAccessLock
184
193
certAccessLock sync.RWMutex
@@ -213,17 +222,18 @@ func NewManager(config *Config) (Manager, error) {
213
222
}
214
223
215
224
m := manager {
216
- stopCh : make (chan struct {}),
217
- clientFn : config .ClientFn ,
218
- getTemplate : getTemplate ,
219
- dynamicTemplate : config .GetTemplate != nil ,
220
- usages : config .Usages ,
221
- certStore : config .CertificateStore ,
222
- cert : cert ,
223
- forceRotation : forceRotation ,
224
- certificateExpiration : config .CertificateExpiration ,
225
- certificateRotation : config .CertificateRotation ,
226
- now : time .Now ,
225
+ stopCh : make (chan struct {}),
226
+ clientFn : config .ClientFn ,
227
+ getTemplate : getTemplate ,
228
+ dynamicTemplate : config .GetTemplate != nil ,
229
+ usages : config .Usages ,
230
+ certStore : config .CertificateStore ,
231
+ cert : cert ,
232
+ forceRotation : forceRotation ,
233
+ certificateExpiration : config .CertificateExpiration ,
234
+ certificateRotation : config .CertificateRotation ,
235
+ certificateRenewFailure : config .CertificateRenewFailure ,
236
+ now : time .Now ,
227
237
}
228
238
229
239
return & m , nil
@@ -404,13 +414,19 @@ func (m *manager) rotateCerts() (bool, error) {
404
414
template , csrPEM , keyPEM , privateKey , err := m .generateCSR ()
405
415
if err != nil {
406
416
utilruntime .HandleError (fmt .Errorf ("Unable to generate a certificate signing request: %v" , err ))
417
+ if m .certificateRenewFailure != nil {
418
+ m .certificateRenewFailure .Inc ()
419
+ }
407
420
return false , nil
408
421
}
409
422
410
423
// request the client each time
411
424
client , err := m .getClient ()
412
425
if err != nil {
413
426
utilruntime .HandleError (fmt .Errorf ("Unable to load a client to request certificates: %v" , err ))
427
+ if m .certificateRenewFailure != nil {
428
+ m .certificateRenewFailure .Inc ()
429
+ }
414
430
return false , nil
415
431
}
416
432
@@ -419,6 +435,9 @@ func (m *manager) rotateCerts() (bool, error) {
419
435
req , err := csr .RequestCertificate (client , csrPEM , "" , m .usages , privateKey )
420
436
if err != nil {
421
437
utilruntime .HandleError (fmt .Errorf ("Failed while requesting a signed certificate from the master: %v" , err ))
438
+ if m .certificateRenewFailure != nil {
439
+ m .certificateRenewFailure .Inc ()
440
+ }
422
441
return false , m .updateServerError (err )
423
442
}
424
443
@@ -433,12 +452,18 @@ func (m *manager) rotateCerts() (bool, error) {
433
452
crtPEM , err := csr .WaitForCertificate (ctx , client , req )
434
453
if err != nil {
435
454
utilruntime .HandleError (fmt .Errorf ("certificate request was not signed: %v" , err ))
455
+ if m .certificateRenewFailure != nil {
456
+ m .certificateRenewFailure .Inc ()
457
+ }
436
458
return false , nil
437
459
}
438
460
439
461
cert , err := m .certStore .Update (crtPEM , keyPEM )
440
462
if err != nil {
441
463
utilruntime .HandleError (fmt .Errorf ("Unable to store the new cert/key pair: %v" , err ))
464
+ if m .certificateRenewFailure != nil {
465
+ m .certificateRenewFailure .Inc ()
466
+ }
442
467
return false , nil
443
468
}
444
469
0 commit comments