@@ -343,13 +343,26 @@ func (d *ResourceDetector) OnUpdate(oldObj, newObj interface{}) {
343
343
return
344
344
}
345
345
346
- resourceChangeByKarmada := eventfilter .ResourceChangeByKarmada (unstructuredOldObj , unstructuredNewObj )
346
+ isLazyActivation , err := d .isClaimedByLazyPolicy (unstructuredNewObj )
347
+ if err != nil {
348
+ // should never come here
349
+ 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 )
350
+ }
347
351
348
- resourceItem := ResourceItem {
349
- Obj : newRuntimeObj ,
350
- ResourceChangeByKarmada : resourceChangeByKarmada ,
352
+ if isLazyActivation {
353
+ resourceItem := ResourceItem {
354
+ Obj : newRuntimeObj ,
355
+ ResourceChangeByKarmada : eventfilter .ResourceChangeByKarmada (unstructuredOldObj , unstructuredNewObj ),
356
+ }
357
+
358
+ d .Processor .Enqueue (resourceItem )
359
+ return
351
360
}
352
361
362
+ // For non-lazy policies, it is no need to distinguish whether the change is from Karmada or not.
363
+ resourceItem := ResourceItem {
364
+ Obj : newRuntimeObj ,
365
+ }
353
366
d .Processor .Enqueue (resourceItem )
354
367
}
355
368
@@ -1208,7 +1221,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1208
1221
if err != nil {
1209
1222
return err
1210
1223
}
1211
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1224
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1212
1225
}
1213
1226
1214
1227
// check whether there are matched RT in waiting list, is so, add it to processor
@@ -1226,7 +1239,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1226
1239
1227
1240
for _ , key := range matchedKeys {
1228
1241
d .RemoveWaiting (key )
1229
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1242
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1230
1243
}
1231
1244
1232
1245
// If preemption is enabled, handle the preemption process.
@@ -1275,14 +1288,14 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1275
1288
if err != nil {
1276
1289
return err
1277
1290
}
1278
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1291
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1279
1292
}
1280
1293
for _ , crb := range clusterResourceBindings .Items {
1281
1294
resourceKey , err := helper .ConstructClusterWideKey (crb .Spec .Resource )
1282
1295
if err != nil {
1283
1296
return err
1284
1297
}
1285
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1298
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1286
1299
}
1287
1300
1288
1301
matchedKeys := d .GetMatching (policy .Spec .ResourceSelectors )
@@ -1299,7 +1312,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1299
1312
1300
1313
for _ , key := range matchedKeys {
1301
1314
d .RemoveWaiting (key )
1302
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1315
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1303
1316
}
1304
1317
1305
1318
// If preemption is enabled, handle the preemption process.
@@ -1472,3 +1485,21 @@ func (d *ResourceDetector) applyReplicaInterpretation(object *unstructured.Unstr
1472
1485
1473
1486
return nil
1474
1487
}
1488
+
1489
+ // enqueueResourceTemplateForPolicyChange enqueues a resource template key for reconciliation in response to a
1490
+ // PropagationPolicy or ClusterPropagationPolicy change. If the policy's ActivationPreference is set to Lazy,
1491
+ // the ResourceChangeByKarmada flag is set to true, indicating that the resource template is being enqueued
1492
+ // due to a policy change and should not be propagated to member clusters. For non-lazy policies, this flag
1493
+ // is omitted as the distinction is unnecessary.
1494
+ //
1495
+ // Note: Setting ResourceChangeByKarmada changes the effective queue key. Mixing both true/false for the same
1496
+ // resource may result in two different queue keys being processed concurrently, which can cause race conditions.
1497
+ // Therefore, only set ResourceChangeByKarmada in lazy activation mode.
1498
+ // For more details, see: https://github.com/karmada-io/karmada/issues/5996.
1499
+ func (d * ResourceDetector ) enqueueResourceTemplateForPolicyChange (key keys.ClusterWideKey , pref policyv1alpha1.ActivationPreference ) {
1500
+ if util .IsLazyActivationEnabled (pref ) {
1501
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key , ResourceChangeByKarmada : true })
1502
+ return
1503
+ }
1504
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key })
1505
+ }
0 commit comments