@@ -337,13 +337,26 @@ func (d *ResourceDetector) OnUpdate(oldObj, newObj interface{}) {
337
337
return
338
338
}
339
339
340
- resourceChangeByKarmada := eventfilter .ResourceChangeByKarmada (unstructuredOldObj , unstructuredNewObj )
340
+ isLazyActivation , err := d .isClaimedByLazyPolicy (unstructuredNewObj )
341
+ if err != nil {
342
+ // should never come here
343
+ klog .Errorf ("Failed to check if the object (kind=%s, %s/%s) is bound by lazy policy. err: %v" , unstructuredNewObj .GetKind (), unstructuredNewObj .GetNamespace (), unstructuredNewObj .GetName (), err )
344
+ }
341
345
342
- resourceItem := ResourceItem {
343
- Obj : newRuntimeObj ,
344
- ResourceChangeByKarmada : resourceChangeByKarmada ,
346
+ if isLazyActivation {
347
+ resourceItem := ResourceItem {
348
+ Obj : newRuntimeObj ,
349
+ ResourceChangeByKarmada : eventfilter .ResourceChangeByKarmada (unstructuredOldObj , unstructuredNewObj ),
350
+ }
351
+
352
+ d .Processor .Enqueue (resourceItem )
353
+ return
345
354
}
346
355
356
+ // For non-lazy policies, it is no need to distinguish whether the change is from Karmada or not.
357
+ resourceItem := ResourceItem {
358
+ Obj : newRuntimeObj ,
359
+ }
347
360
d .Processor .Enqueue (resourceItem )
348
361
}
349
362
@@ -1278,7 +1291,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1278
1291
if err != nil {
1279
1292
return err
1280
1293
}
1281
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1294
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1282
1295
}
1283
1296
1284
1297
// check whether there are matched RT in waiting list, is so, add it to processor
@@ -1296,7 +1309,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1296
1309
1297
1310
for _ , key := range matchedKeys {
1298
1311
d .RemoveWaiting (key )
1299
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1312
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1300
1313
}
1301
1314
1302
1315
// If preemption is enabled, handle the preemption process.
@@ -1345,14 +1358,14 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1345
1358
if err != nil {
1346
1359
return err
1347
1360
}
1348
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1361
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1349
1362
}
1350
1363
for _ , crb := range clusterResourceBindings .Items {
1351
1364
resourceKey , err := helper .ConstructClusterWideKey (crb .Spec .Resource )
1352
1365
if err != nil {
1353
1366
return err
1354
1367
}
1355
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1368
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1356
1369
}
1357
1370
1358
1371
matchedKeys := d .GetMatching (policy .Spec .ResourceSelectors )
@@ -1369,7 +1382,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1369
1382
1370
1383
for _ , key := range matchedKeys {
1371
1384
d .RemoveWaiting (key )
1372
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1385
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1373
1386
}
1374
1387
1375
1388
// If preemption is enabled, handle the preemption process.
@@ -1512,3 +1525,21 @@ func (d *ResourceDetector) CleanupClusterResourceBindingClaimMetadata(crbName st
1512
1525
return updateErr
1513
1526
})
1514
1527
}
1528
+
1529
+ // enqueueResourceTemplateForPolicyChange enqueues a resource template key for reconciliation in response to a
1530
+ // PropagationPolicy or ClusterPropagationPolicy change. If the policy's ActivationPreference is set to Lazy,
1531
+ // the ResourceChangeByKarmada flag is set to true, indicating that the resource template is being enqueued
1532
+ // due to a policy change and should not be propagated to member clusters. For non-lazy policies, this flag
1533
+ // is omitted as the distinction is unnecessary.
1534
+ //
1535
+ // Note: Setting ResourceChangeByKarmada changes the effective queue key. Mixing both true/false for the same
1536
+ // resource may result in two different queue keys being processed concurrently, which can cause race conditions.
1537
+ // Therefore, only set ResourceChangeByKarmada in lazy activation mode.
1538
+ // For more details, see: https://github.com/karmada-io/karmada/issues/5996.
1539
+ func (d * ResourceDetector ) enqueueResourceTemplateForPolicyChange (key keys.ClusterWideKey , pref policyv1alpha1.ActivationPreference ) {
1540
+ if util .IsLazyActivationEnabled (pref ) {
1541
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key , ResourceChangeByKarmada : true })
1542
+ return
1543
+ }
1544
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key })
1545
+ }
0 commit comments