@@ -50,7 +50,6 @@ type Propagator struct {
50
50
client.Client
51
51
Scheme * runtime.Scheme
52
52
Recorder record.EventRecorder
53
- DynamicWatcher k8sdepwatches.DynamicWatcher
54
53
RootPolicyLocks * sync.Map
55
54
ReplicatedPolicyUpdates chan event.GenericEvent
56
55
}
@@ -182,12 +181,12 @@ func (r *RootPolicyReconciler) getAllClusterDecisions(
182
181
decisions = make (map [appsv1.PlacementDecision ]policiesv1.BindingOverrides )
183
182
184
183
// Process all placement bindings without subFilter
185
- for _ , pb := range pbList .Items {
184
+ for i , pb := range pbList .Items {
186
185
if pb .SubFilter == policiesv1 .Restricted {
187
186
continue
188
187
}
189
188
190
- plcDecisions , plcPlacements , err := r .getPolicyPlacementDecisions (instance , & pb )
189
+ plcDecisions , plcPlacements , err := r .getPolicyPlacementDecisions (instance , & pbList . Items [ i ] )
191
190
if err != nil {
192
191
return nil , nil , err
193
192
}
@@ -226,14 +225,14 @@ func (r *RootPolicyReconciler) getAllClusterDecisions(
226
225
}
227
226
228
227
// Process all placement bindings with subFilter:restricted
229
- for _ , pb := range pbList .Items {
228
+ for i , pb := range pbList .Items {
230
229
if pb .SubFilter != policiesv1 .Restricted {
231
230
continue
232
231
}
233
232
234
233
foundInDecisions := false
235
234
236
- plcDecisions , plcPlacements , err := r .getPolicyPlacementDecisions (instance , & pb )
235
+ plcDecisions , plcPlacements , err := r .getPolicyPlacementDecisions (instance , & pbList . Items [ i ] )
237
236
if err != nil {
238
237
return nil , nil , err
239
238
}
@@ -267,9 +266,8 @@ func (r *RootPolicyReconciler) getAllClusterDecisions(
267
266
return decisions , placements , nil
268
267
}
269
268
270
- // handleDecisions identifies all managed clusters which should have a replicated policy, and sends
271
- // events to the replicated policy reconciler for them to be created or updated.
272
- func (r * RootPolicyReconciler ) handleDecisions (
269
+ // getDecisions identifies all managed clusters which should have a replicated policy
270
+ func (r * RootPolicyReconciler ) getDecisions (
273
271
instance * policiesv1.Policy ,
274
272
) (
275
273
[]* policiesv1.Placement , decisionSet , error ,
@@ -299,25 +297,6 @@ func (r *RootPolicyReconciler) handleDecisions(
299
297
decisions [dec ] = true
300
298
}
301
299
302
- log .Info ("Sending reconcile events to replicated policies" , "decisionsCount" , len (allClusterDecisions ))
303
-
304
- for decision := range allClusterDecisions {
305
- simpleObj := & GuttedObject {
306
- TypeMeta : metav1.TypeMeta {
307
- Kind : policiesv1 .Kind ,
308
- APIVersion : policiesv1 .GroupVersion .String (),
309
- },
310
- ObjectMeta : metav1.ObjectMeta {
311
- Name : common .FullNameForPolicy (instance ),
312
- Namespace : decision .ClusterNamespace ,
313
- },
314
- }
315
-
316
- log .V (2 ).Info ("Sending reconcile for replicated policy" , "replicatedPolicyName" , simpleObj .GetName ())
317
-
318
- r .ReplicatedPolicyUpdates <- event.GenericEvent {Object : simpleObj }
319
- }
320
-
321
300
return placements , decisions , nil
322
301
}
323
302
@@ -326,11 +305,11 @@ func (r *RootPolicyReconciler) handleDecisions(
326
305
// decisions, then it's considered stale and an event is sent to the replicated policy reconciler
327
306
// so the policy will be removed.
328
307
func (r * RootPolicyReconciler ) cleanUpOrphanedRplPolicies (
329
- instance * policiesv1.Policy , allDecisions decisionSet ,
308
+ instance * policiesv1.Policy , originalCPCS [] * policiesv1. CompliancePerClusterStatus , allDecisions decisionSet ,
330
309
) error {
331
310
log := log .WithValues ("policyName" , instance .GetName (), "policyNamespace" , instance .GetNamespace ())
332
311
333
- for _ , cluster := range instance . Status . Status {
312
+ for _ , cluster := range originalCPCS {
334
313
key := appsv1.PlacementDecision {
335
314
ClusterName : cluster .ClusterNamespace ,
336
315
ClusterNamespace : cluster .ClusterNamespace ,
@@ -388,32 +367,30 @@ func (r *RootPolicyReconciler) handleRootPolicy(instance *policiesv1.Policy) err
388
367
}
389
368
}
390
369
391
- placements , decisions , err := r .handleDecisions (instance )
370
+ placements , decisions , err := r .getDecisions (instance )
392
371
if err != nil {
393
372
log .Info ("Failed to get any placement decisions. Giving up on the request." )
394
373
395
374
return errors .New ("could not get the placement decisions" )
396
375
}
397
376
398
- err = r .cleanUpOrphanedRplPolicies (instance , decisions )
399
- if err != nil {
400
- log .Error (err , "Failed to delete orphaned replicated policies" )
401
-
402
- return err
403
- }
404
-
405
377
log .V (1 ).Info ("Updating the root policy status" )
406
378
407
379
cpcs , cpcsErr := r .calculatePerClusterStatus (instance , decisions )
408
380
if cpcsErr != nil {
409
- log .Error (cpcsErr , "Failed to get at least one replicated policy" )
381
+ // If there is a new replicated policy, then its lookup is expected to fail - it hasn't been created yet.
382
+ log .Error (cpcsErr , "Failed to get at least one replicated policy, but that may be expected. Ignoring." )
410
383
}
411
384
412
385
err = r .Get (context .TODO (), types.NamespacedName {Namespace : instance .Namespace , Name : instance .Name }, instance )
413
386
if err != nil {
414
387
log .Error (err , "Failed to refresh the cached policy. Will use existing policy." )
415
388
}
416
389
390
+ // make a copy of the original status
391
+ originalCPCS := make ([]* policiesv1.CompliancePerClusterStatus , len (instance .Status .Status ))
392
+ copy (originalCPCS , instance .Status .Status )
393
+
417
394
instance .Status .Status = cpcs
418
395
instance .Status .ComplianceState = CalculateRootCompliance (cpcs )
419
396
instance .Status .Placement = placements
@@ -423,7 +400,33 @@ func (r *RootPolicyReconciler) handleRootPolicy(instance *policiesv1.Policy) err
423
400
return err
424
401
}
425
402
426
- return cpcsErr
403
+ log .Info ("Sending reconcile events to replicated policies" , "decisionsCount" , len (decisions ))
404
+
405
+ for decision := range decisions {
406
+ simpleObj := & GuttedObject {
407
+ TypeMeta : metav1.TypeMeta {
408
+ Kind : policiesv1 .Kind ,
409
+ APIVersion : policiesv1 .GroupVersion .String (),
410
+ },
411
+ ObjectMeta : metav1.ObjectMeta {
412
+ Name : common .FullNameForPolicy (instance ),
413
+ Namespace : decision .ClusterNamespace ,
414
+ },
415
+ }
416
+
417
+ log .V (2 ).Info ("Sending reconcile for replicated policy" , "replicatedPolicyName" , simpleObj .GetName ())
418
+
419
+ r .ReplicatedPolicyUpdates <- event.GenericEvent {Object : simpleObj }
420
+ }
421
+
422
+ err = r .cleanUpOrphanedRplPolicies (instance , originalCPCS , decisions )
423
+ if err != nil {
424
+ log .Error (err , "Failed to delete orphaned replicated policies" )
425
+
426
+ return err
427
+ }
428
+
429
+ return nil
427
430
}
428
431
429
432
// a helper to quickly check if there are any templates in any of the policy templates
0 commit comments