@@ -10,6 +10,7 @@ import (
1010 "strings"
1111
1212 "github.com/go-logr/logr"
13+ "github.com/prometheus/client_golang/prometheus"
1314 depclient "github.com/stolostron/kubernetes-dependency-watches/client"
1415 "k8s.io/apimachinery/pkg/api/equality"
1516 "k8s.io/apimachinery/pkg/api/errors"
@@ -89,12 +90,17 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
8990 // Return and don't requeue
9091 reqLogger .Info ("Policy not found, may have been deleted, reconciliation completed" )
9192
93+ _ = policyUserErrorsCounter .DeletePartialMatch (prometheus.Labels {"policy" : request .Name })
94+ _ = policySystemErrorsCounter .DeletePartialMatch (prometheus.Labels {"policy" : request .Name })
95+
9296 return reconcile.Result {}, nil
9397 }
9498
9599 // Error reading the object - requeue the request.
96100 reqLogger .Error (err , "Failed to get the policy, will requeue the request" )
97101
102+ policySystemErrorsCounter .WithLabelValues (request .Name , "" , "get-error" ).Inc ()
103+
98104 return reconcile.Result {}, err
99105 }
100106
@@ -110,6 +116,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
110116 if err != nil {
111117 reqLogger .Error (err , "Failed to create restmapper" )
112118
119+ policySystemErrorsCounter .WithLabelValues (instance .Name , "" , "get-error" ).Inc ()
120+
113121 return reconcile.Result {}, err
114122 }
115123
@@ -120,6 +128,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
120128 if err != nil {
121129 reqLogger .Error (err , "Failed to create dynamic client" )
122130
131+ policySystemErrorsCounter .WithLabelValues (instance .Name , "" , "client-error" ).Inc ()
132+
123133 return reconcile.Result {}, err
124134 }
125135 } else {
@@ -153,6 +163,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
153163
154164 reqLogger .Error (err , "Failed to decode the policy dependencies" , "policy" , instance .GetName ())
155165
166+ policyUserErrorsCounter .WithLabelValues (instance .Name , "" , "dependency-error" ).Inc ()
167+
156168 continue
157169 }
158170
@@ -196,13 +208,15 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
196208 if ok && existingDep != dep .Compliance {
197209 // dependency conflict, fire error
198210 resultError = fmt .Errorf ("dependency on %s has conflicting compliance states" , dep .Name )
199- errMsg := fmt .Sprintf ("Failed to decode policy template with err: %s" , err )
211+ errMsg := fmt .Sprintf ("Failed to decode policy template with err: %s" , resultError )
200212
201213 r .emitTemplateError (instance , tIndex , fmt .Sprintf ("[template %v]" , tIndex ), errMsg )
202214 reqLogger .Error (resultError , "Failed to decode the policy template" , "templateIndex" , tIndex )
203215
204216 depConflictErr = true
205217
218+ policyUserErrorsCounter .WithLabelValues (instance .Name , "" , "dependency-error" ).Inc ()
219+
206220 break
207221 }
208222
@@ -223,6 +237,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
223237 r .emitTemplateError (instance , tIndex , fmt .Sprintf ("[template %v]" , tIndex ), errMsg )
224238 reqLogger .Error (resultError , "Failed to decode the policy template" , "templateIndex" , tIndex )
225239
240+ policyUserErrorsCounter .WithLabelValues (instance .Name , "" , "format-error" ).Inc ()
241+
226242 continue
227243 }
228244
@@ -238,6 +254,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
238254 r .emitTemplateError (instance , tIndex , fmt .Sprintf ("[template %v]" , tIndex ), errMsg )
239255 reqLogger .Error (resultError , "Failed to process the policy template" , "templateIndex" , tIndex )
240256
257+ policyUserErrorsCounter .WithLabelValues (instance .Name , "" , "format-error" ).Inc ()
258+
241259 continue
242260 }
243261
@@ -260,6 +278,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
260278 "kind" , gvk .Kind ,
261279 )
262280
281+ policyUserErrorsCounter .WithLabelValues (instance .Name , tName , "crd-error" ).Inc ()
282+
263283 continue
264284 }
265285
@@ -274,6 +294,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
274294 r .emitTemplateError (instance , tIndex , tName , errMsg )
275295 tLogger .Error (resultError , "Failed to process the policy template" )
276296
297+ policyUserErrorsCounter .WithLabelValues (instance .Name , tName , "format-error" ).Inc ()
298+
277299 continue
278300 }
279301 }
@@ -292,6 +314,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
292314 r .emitTemplateError (instance , tIndex , tName , errMsg )
293315 tLogger .Error (resultError , "Failed to unmarshal the policy template" )
294316
317+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "unmarshal-error" ).Inc ()
318+
295319 continue
296320 }
297321
@@ -344,6 +368,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
344368 r .emitTemplateError (instance , tIndex , tName , errMsg )
345369 tLogger .Error (resultError , "Failed to create policy template" )
346370
371+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "create-error" ).Inc ()
372+
347373 continue
348374 }
349375
@@ -354,6 +380,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
354380 if err != nil {
355381 resultError = err
356382 tLogger .Error (resultError , "Error after creating template (will requeue)" )
383+
384+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "patch-error" ).Inc ()
357385 }
358386
359387 continue
@@ -368,6 +396,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
368396 "kind" , gvk .Kind ,
369397 )
370398
399+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "get-error" ).Inc ()
400+
371401 continue
372402 }
373403 }
@@ -386,6 +416,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
386416 "namespace" , instance .GetNamespace (),
387417 "name" , tName ,
388418 )
419+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "delete-error" ).Inc ()
389420
390421 resultError = err
391422 }
@@ -406,6 +437,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
406437 r .emitTemplateError (instance , tIndex , tName , errMsg )
407438 tLogger .Error (resultError , "Failed to create the policy template" )
408439
440+ policyUserErrorsCounter .WithLabelValues (instance .Name , tName , "format-error" ).Inc ()
441+
409442 continue
410443 }
411444
@@ -435,6 +468,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
435468 r .emitTemplateError (instance , tIndex , tName , errMsg )
436469 tLogger .Error (err , "Failed to update the policy template" )
437470
471+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "patch-error" ).Inc ()
472+
438473 continue
439474 }
440475
@@ -444,6 +479,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
444479 if err != nil {
445480 resultError = err
446481 tLogger .Error (resultError , "Error after updating template (will requeue)" )
482+
483+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "patch-error" ).Inc ()
447484 }
448485
449486 tLogger .Info ("Existing object has been updated" )
@@ -452,6 +489,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
452489 if err != nil {
453490 resultError = err
454491 tLogger .Error (resultError , "Error after confirming template matches (will requeue)" )
492+
493+ policySystemErrorsCounter .WithLabelValues (instance .Name , tName , "patch-error" ).Inc ()
455494 }
456495
457496 tLogger .Info ("Existing object matches the policy template" )
@@ -480,6 +519,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
480519 if err != nil {
481520 resultError = err
482521 reqLogger .Error (resultError , "Error updating dependency watcher" )
522+
523+ policySystemErrorsCounter .WithLabelValues (instance .Name , "" , "client-error" ).Inc ()
483524 }
484525
485526 reqLogger .Info ("Completed the reconciliation" )
0 commit comments