@@ -299,11 +299,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
299299 if err != nil {
300300 if len (dependencyFailures ) > 0 {
301301 // template must be pending, do not create it
302- pendingErr := generatePendingErr (dependencyFailures )
303- resultError = pendingErr
304- errMsg := fmt .Sprintf ("Dependencies were not satisfied: %s" , pendingErr )
305-
306- r .emitTemplatePending (instance , tIndex , tName , errMsg )
302+ r .emitTemplatePending (instance , tIndex , tName , generatePendingMsg (dependencyFailures ))
307303 tLogger .Info ("Dependencies were not satisfied for the policy template" ,
308304 "namespace" , instance .GetNamespace (),
309305 "kind" , gvk .Kind ,
@@ -378,11 +374,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
378374
379375 if len (dependencyFailures ) > 0 {
380376 // template must be pending, need to delete it and error
381- pendingErr := generatePendingErr (dependencyFailures )
382- resultError = pendingErr
383- errMsg := fmt .Sprintf ("Dependencies were not satisfied: %s" , pendingErr )
384-
385- r .emitTemplatePending (instance , tIndex , tName , errMsg )
377+ r .emitTemplatePending (instance , tIndex , tName , generatePendingMsg (dependencyFailures ))
386378 tLogger .Info ("Dependencies were not satisfied for the policy template" ,
387379 "namespace" , instance .GetNamespace (),
388380 "kind" , gvk .Kind ,
@@ -394,6 +386,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
394386 "namespace" , instance .GetNamespace (),
395387 "name" , tName ,
396388 )
389+
390+ resultError = err
397391 }
398392
399393 continue
@@ -555,20 +549,22 @@ func (r *PolicyReconciler) processDependencies(ctx context.Context, dClient dyna
555549 return dependencyFailures
556550}
557551
558- // generatePendingErr formats the list of failed dependencies into a readable error
559- func generatePendingErr (dependencyFailures []depclient.ObjectIdentifier ) error {
552+ // generatePendingMsg formats the list of failed dependencies into a readable error.
553+ // Example: `Dependencies were not satisfied: 1 is still pending (FooPolicy foo)`
554+ func generatePendingMsg (dependencyFailures []depclient.ObjectIdentifier ) string {
560555 names := make ([]string , len (dependencyFailures ))
561556 for i , dep := range dependencyFailures {
562557 names [i ] = fmt .Sprintf ("%s %s" , dep .Kind , dep .Name )
563558 }
564559
565560 nameStr := strings .Join (names , ", " )
566561
567- return fmt .Errorf (
568- "%d dependencies are still pending (%s)" ,
569- len (dependencyFailures ),
570- nameStr ,
571- )
562+ fmtStr := "Dependencies were not satisfied: %d are still pending (%s)"
563+ if len (dependencyFailures ) == 1 {
564+ fmtStr = "Dependencies were not satisfied: %d is still pending (%s)"
565+ }
566+
567+ return fmt .Sprintf (fmtStr , len (dependencyFailures ), nameStr )
572568}
573569
574570func overrideRemediationAction (instance * policiesv1.Policy , tObjectUnstructured * unstructured.Unstructured ) {
@@ -600,21 +596,29 @@ func (r *PolicyReconciler) emitTemplateError(pol *policiesv1.Policy, tIndex int,
600596 r .Recorder .Event (pol , "Warning" , "PolicyTemplateSync" , errMsg )
601597}
602598
603- // emitTemplatePending performs actions that ensure correct reporting of dependency errors in the
604- // policy framework. If the policy's status already reflects the current error , then no actions
599+ // emitTemplatePending performs actions that ensure correct reporting of pending dependencies in the
600+ // policy framework. If the policy's status already reflects the current status , then no actions
605601// are taken.
606- func (r * PolicyReconciler ) emitTemplatePending (pol * policiesv1.Policy , tIndex int , tName , errMsg string ) {
602+ func (r * PolicyReconciler ) emitTemplatePending (pol * policiesv1.Policy , tIndex int , tName , msg string ) {
603+ statusMsg := "Pending; " + msg
604+ eventType := "Warning"
605+
606+ if pol .Spec .PolicyTemplates [tIndex ].IgnorePending {
607+ statusMsg = "Compliant; " + msg + " but ignorePending is true"
608+ eventType = "Normal"
609+ }
610+
607611 // check if the error is already present in the policy status - if so, return early
608- if strings .Contains (getLatestStatusMessage (pol , tIndex ), errMsg ) {
612+ if strings .Contains (getLatestStatusMessage (pol , tIndex ), statusMsg ) {
609613 return
610614 }
611615
612616 // emit the non-compliance event
613617 policyComplianceReason := fmt .Sprintf (policyFmtStr , pol .GetNamespace (), tName )
614- r .Recorder .Event (pol , "Warning" , policyComplianceReason , "Pending; template-error; " + errMsg )
618+ r .Recorder .Event (pol , eventType , policyComplianceReason , statusMsg )
615619
616620 // emit an informational event
617- r .Recorder .Event (pol , "Warning" , "PolicyTemplateSync" , errMsg )
621+ r .Recorder .Event (pol , eventType , "PolicyTemplateSync" , statusMsg )
618622}
619623
620624// handleSyncSuccess performs common actions that should be run whenever a template is in sync,
0 commit comments