@@ -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
@@ -1205,7 +1218,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1205
1218
if err != nil {
1206
1219
return err
1207
1220
}
1208
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1221
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1209
1222
}
1210
1223
1211
1224
// check whether there are matched RT in waiting list, is so, add it to processor
@@ -1223,7 +1236,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1223
1236
1224
1237
for _ , key := range matchedKeys {
1225
1238
d .RemoveWaiting (key )
1226
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1239
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1227
1240
}
1228
1241
1229
1242
// If preemption is enabled, handle the preemption process.
@@ -1272,14 +1285,14 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1272
1285
if err != nil {
1273
1286
return err
1274
1287
}
1275
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1288
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1276
1289
}
1277
1290
for _ , crb := range clusterResourceBindings .Items {
1278
1291
resourceKey , err := helper .ConstructClusterWideKey (crb .Spec .Resource )
1279
1292
if err != nil {
1280
1293
return err
1281
1294
}
1282
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1295
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1283
1296
}
1284
1297
1285
1298
matchedKeys := d .GetMatching (policy .Spec .ResourceSelectors )
@@ -1296,7 +1309,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
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.
@@ -1439,3 +1452,21 @@ func (d *ResourceDetector) CleanupClusterResourceBindingClaimMetadata(crbName st
1439
1452
return updateErr
1440
1453
})
1441
1454
}
1455
+
1456
+ // enqueueResourceTemplateForPolicyChange enqueues a resource template key for reconciliation in response to a
1457
+ // PropagationPolicy or ClusterPropagationPolicy change. If the policy's ActivationPreference is set to Lazy,
1458
+ // the ResourceChangeByKarmada flag is set to true, indicating that the resource template is being enqueued
1459
+ // due to a policy change and should not be propagated to member clusters. For non-lazy policies, this flag
1460
+ // is omitted as the distinction is unnecessary.
1461
+ //
1462
+ // Note: Setting ResourceChangeByKarmada changes the effective queue key. Mixing both true/false for the same
1463
+ // resource may result in two different queue keys being processed concurrently, which can cause race conditions.
1464
+ // Therefore, only set ResourceChangeByKarmada in lazy activation mode.
1465
+ // For more details, see: https://github.com/karmada-io/karmada/issues/5996.
1466
+ func (d * ResourceDetector ) enqueueResourceTemplateForPolicyChange (key keys.ClusterWideKey , pref policyv1alpha1.ActivationPreference ) {
1467
+ if util .IsLazyActivationEnabled (pref ) {
1468
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key , ResourceChangeByKarmada : true })
1469
+ return
1470
+ }
1471
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key })
1472
+ }
0 commit comments