@@ -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
@@ -1263,7 +1276,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1263
1276
if err != nil {
1264
1277
return err
1265
1278
}
1266
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1279
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1267
1280
}
1268
1281
1269
1282
// check whether there are matched RT in waiting list, is so, add it to processor
@@ -1281,7 +1294,7 @@ func (d *ResourceDetector) HandlePropagationPolicyCreationOrUpdate(policy *polic
1281
1294
1282
1295
for _ , key := range matchedKeys {
1283
1296
d .RemoveWaiting (key )
1284
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1297
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1285
1298
}
1286
1299
1287
1300
// If preemption is enabled, handle the preemption process.
@@ -1330,14 +1343,14 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1330
1343
if err != nil {
1331
1344
return err
1332
1345
}
1333
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1346
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1334
1347
}
1335
1348
for _ , crb := range clusterResourceBindings .Items {
1336
1349
resourceKey , err := helper .ConstructClusterWideKey (crb .Spec .Resource )
1337
1350
if err != nil {
1338
1351
return err
1339
1352
}
1340
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : resourceKey , ResourceChangeByKarmada : true } )
1353
+ d .enqueueResourceTemplateForPolicyChange ( resourceKey , policy . Spec . ActivationPreference )
1341
1354
}
1342
1355
1343
1356
matchedKeys := d .GetMatching (policy .Spec .ResourceSelectors )
@@ -1354,7 +1367,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyCreationOrUpdate(policy
1354
1367
1355
1368
for _ , key := range matchedKeys {
1356
1369
d .RemoveWaiting (key )
1357
- d .Processor . Add (keys. ClusterWideKeyWithConfig { ClusterWideKey : key , ResourceChangeByKarmada : true } )
1370
+ d .enqueueResourceTemplateForPolicyChange ( key , policy . Spec . ActivationPreference )
1358
1371
}
1359
1372
1360
1373
// If preemption is enabled, handle the preemption process.
@@ -1497,3 +1510,21 @@ func (d *ResourceDetector) CleanupClusterResourceBindingClaimMetadata(crbName st
1497
1510
return updateErr
1498
1511
})
1499
1512
}
1513
+
1514
+ // enqueueResourceTemplateForPolicyChange enqueues a resource template key for reconciliation in response to a
1515
+ // PropagationPolicy or ClusterPropagationPolicy change. If the policy's ActivationPreference is set to Lazy,
1516
+ // the ResourceChangeByKarmada flag is set to true, indicating that the resource template is being enqueued
1517
+ // due to a policy change and should not be propagated to member clusters. For non-lazy policies, this flag
1518
+ // is omitted as the distinction is unnecessary.
1519
+ //
1520
+ // Note: Setting ResourceChangeByKarmada changes the effective queue key. Mixing both true/false for the same
1521
+ // resource may result in two different queue keys being processed concurrently, which can cause race conditions.
1522
+ // Therefore, only set ResourceChangeByKarmada in lazy activation mode.
1523
+ // For more details, see: https://github.com/karmada-io/karmada/issues/5996.
1524
+ func (d * ResourceDetector ) enqueueResourceTemplateForPolicyChange (key keys.ClusterWideKey , pref policyv1alpha1.ActivationPreference ) {
1525
+ if util .IsLazyActivationEnabled (pref ) {
1526
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key , ResourceChangeByKarmada : true })
1527
+ return
1528
+ }
1529
+ d .Processor .Add (keys.ClusterWideKeyWithConfig {ClusterWideKey : key })
1530
+ }
0 commit comments