@@ -435,12 +435,12 @@ func (r *PolicyReconciler) handleDecisions(
435
435
// decisions. If the cluster exists in the status but doesn't exist in the input placement
436
436
// decisions, then it's considered stale and will be removed.
437
437
func (r * PolicyReconciler ) cleanUpOrphanedRplPolicies (
438
- instance * policiesv1.Policy , oldStatus [] * policiesv1. CompliancePerClusterStatus , allDecisions decisionSet ,
438
+ instance * policiesv1.Policy , allDecisions decisionSet ,
439
439
) error {
440
440
log := log .WithValues ("policyName" , instance .GetName (), "policyNamespace" , instance .GetNamespace ())
441
441
successful := true
442
442
443
- for _ , cluster := range oldStatus {
443
+ for _ , cluster := range instance . Status . Status {
444
444
key := appsv1.PlacementDecision {
445
445
ClusterName : cluster .ClusterNamespace ,
446
446
ClusterNamespace : cluster .ClusterNamespace ,
@@ -527,28 +527,31 @@ func (r *PolicyReconciler) handleRootPolicy(instance *policiesv1.Policy) error {
527
527
return errors .New ("c" + msg [1 :])
528
528
}
529
529
530
- cpcs , _ := r .calculatePerClusterStatus (instance , allDecisions , failedClusters )
530
+ // Clean up before the status update in case the status update fails
531
+ err = r .cleanUpOrphanedRplPolicies (instance , allDecisions )
532
+ if err != nil {
533
+ log .Error (err , "Failed to delete orphaned replicated policies" )
531
534
532
- oldStatus := instance .Status .Status
533
- instance .Status .Status = cpcs
534
- instance .Status .ComplianceState = CalculateRootCompliance (cpcs )
535
+ return err
536
+ }
537
+
538
+ log .V (1 ).Info ("Updating the root policy status" )
539
+
540
+ cpcs , _ := r .calculatePerClusterStatus (instance , allDecisions , failedClusters )
535
541
536
- // looped through all pb, update status.placement
542
+ // loop through all pb, update status.placement
537
543
sort .Slice (placements , func (i , j int ) bool {
538
544
return placements [i ].PlacementBinding < placements [j ].PlacementBinding
539
545
})
540
546
541
- instance .Status .Placement = placements
542
-
543
- // Clean up before the status update in case the status update fails
544
- err = r .cleanUpOrphanedRplPolicies (instance , oldStatus , allDecisions )
547
+ err = r .Get (context .TODO (), types.NamespacedName {Namespace : instance .Namespace , Name : instance .Name }, instance )
545
548
if err != nil {
546
- log .Error (err , "Failed to delete orphaned replicated policies" )
547
-
548
- return err
549
+ log .Error (err , "Failed to refresh the cached policy. Will use existing policy." )
549
550
}
550
551
551
- log .V (1 ).Info ("Updating the root policy status" )
552
+ instance .Status .Status = cpcs
553
+ instance .Status .ComplianceState = CalculateRootCompliance (cpcs )
554
+ instance .Status .Placement = placements
552
555
553
556
err = r .Status ().Update (context .TODO (), instance , & client.UpdateOptions {})
554
557
if err != nil {
0 commit comments