@@ -1408,8 +1408,16 @@ func (r *PolicyReconciler) emitTemplateEvent(
14081408 ctx context.Context , pol * policiesv1.Policy , tIndex int , tName string , clusterScoped bool ,
14091409 eventType string , compliance policiesv1.ComplianceState , msg string ,
14101410) error {
1411- // check if the error is already present in the policy status - if so, return early
1412- if strings .Contains (getLatestStatusMessage (pol , tIndex ), string (compliance )+ "; " + msg ) {
1411+ // refresh the policy to get the latest status, this helps prevent duplicates
1412+ refreshed := & policiesv1.Policy {}
1413+
1414+ err := r .Get (ctx , types.NamespacedName {Namespace : pol .GetNamespace (), Name : pol .GetName ()}, refreshed )
1415+ if err != nil {
1416+ return err
1417+ }
1418+
1419+ // check if the event is already present in the policy status - if so, return early
1420+ if strings .Contains (getLatestStatusMessage (refreshed , tIndex ), string (compliance )+ "; " + msg ) {
14131421 return nil
14141422 }
14151423
@@ -1442,7 +1450,7 @@ func (r *PolicyReconciler) emitTemplateEvent(
14421450
14431451 instanceUnstructured := & unstructured.Unstructured {}
14441452
1445- err : = instanceUnstructured .UnmarshalJSON (pol .Spec .PolicyTemplates [tIndex ].ObjectDefinition .Raw )
1453+ err = instanceUnstructured .UnmarshalJSON (pol .Spec .PolicyTemplates [tIndex ].ObjectDefinition .Raw )
14461454 if err == nil {
14471455 if pol .Annotations [utils .ParentDBIDAnnotation ] != "" {
14481456 annotations := instanceUnstructured .GetAnnotations ()
0 commit comments