@@ -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
@@ -1228,7 +1241,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1228
1241
if err != nil {
1229
1242
return err
1230
1243
}
1231
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1244
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1232
1245
}
1233
1246
1234
1247
// check whether there are matched RT in waiting list, is so, add it to processor
@@ -1246,7 +1259,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1246
1259
1247
1260
for _ , key := range matchedKeys {
1248
1261
d .RemoveWaiting (key )
1249
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1262
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1250
1263
}
1251
1264
1252
1265
// If preemption is enabled, handle the preemption process.
@@ -1295,14 +1308,14 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1295
1308
if err != nil {
1296
1309
return err
1297
1310
}
1298
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1311
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1299
1312
}
1300
1313
for _ , crb := range clusterResourceBindings .Items {
1301
1314
resourceKey , err := helper .ConstructClusterWideKey (crb .Spec .Resource )
1302
1315
if err != nil {
1303
1316
return err
1304
1317
}
1305
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1318
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1306
1319
}
1307
1320
1308
1321
matchedKeys := d .GetMatching (policy .Spec .ResourceSelectors )
@@ -1319,7 +1332,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1319
1332
1320
1333
for _ , key := range matchedKeys {
1321
1334
d .RemoveWaiting (key )
1322
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1335
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1323
1336
}
1324
1337
1325
1338
// If preemption is enabled, handle the preemption process.
@@ -1462,3 +1475,21 @@ func (d *ResourceDetector) CleanupClusterResourceBindingClaimMetadata(crbName st
1462
1475
return updateErr
1463
1476
})
1464
1477
}
1478
+
1479
+ // enqueueResourceTemplateForPolicyChange enqueues a resource template key for reconciliation in response to a
1480
+ // PropagationPolicy or ClusterPropagationPolicy change. If the policy's ActivationPreference is set to Lazy,
1481
+ // the ResourceChangeByKarmada flag is set to true, indicating that the resource template is being enqueued
1482
+ // due to a policy change and should not be propagated to member clusters. For non-lazy policies, this flag
1483
+ // is omitted as the distinction is unnecessary.
1484
+ //
1485
+ // Note: Setting ResourceChangeByKarmada changes the effective queue key. Mixing both true/false for the same
1486
+ // resource may result in two different queue keys being processed concurrently, which can cause race conditions.
1487
+ // Therefore, only set ResourceChangeByKarmada in lazy activation mode.
1488
+ // For more details, see: https://github.com/karmada-io/karmada/issues/5996.
1489
+ func (d * ResourceDetector ) enqueueResourceTemplateForPolicyChange (key keys.ClusterWideKey , pref policyv1alpha1.ActivationPreference ) {
1490
+ if util .IsLazyActivationEnabled (pref ) {
1491
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key , ResourceChangeByKarmada : true })
1492
+ return
1493
+ }
1494
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key })
1495
+ }
0 commit comments