Skip to content

Commit dfdd9a5

Browse files
authored
Merge pull request #6544 from zhzhuang-zju/cachemutation
detector: eliminate the mutation of the informer cache
2 parents e335bc2 + d0f7464 commit dfdd9a5

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

pkg/detector/detector.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,6 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured,
657657
}
658658

659659
// GetUnstructuredObject retrieves object by key and returned its unstructured.
660-
// Any updates to this resource template are not recommended as it may come from the informer cache.
661-
// We should abide by the principle of making a deep copy first and then modifying it.
662-
// See issue: https://github.com/karmada-io/karmada/issues/3878.
663660
func (d *ResourceDetector) GetUnstructuredObject(objectKey keys.ClusterWideKey) (*unstructured.Unstructured, error) {
664661
objectGVR, err := restmapper.GetGroupVersionResource(d.RESTMapper, objectKey.GroupVersionKind())
665662
if err != nil {
@@ -689,7 +686,8 @@ func (d *ResourceDetector) GetUnstructuredObject(objectKey keys.ClusterWideKey)
689686
return nil, err
690687
}
691688

692-
return unstructuredObj, nil
689+
// perform a deep copy to avoid modifying the cached object from informer
690+
return unstructuredObj.DeepCopy(), nil
693691
}
694692

695693
// ClaimPolicyForObject set policy identifier which the object associated with.

pkg/detector/policy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ func (d *ResourceDetector) removeResourceClaimMetadataIfNotMatched(objectReferen
270270
return false, nil
271271
}
272272

273-
object = object.DeepCopy()
274273
util.RemoveLabels(object, labels...)
275274
util.RemoveAnnotations(object, annotations...)
276275

pkg/util/helper/binding.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ func RemoveOrphanWorks(ctx context.Context, c client.Client, works []workv1alpha
238238
}
239239

240240
// FetchResourceTemplate fetches the resource template to be propagated.
241-
// Any updates to this resource template are not recommended as it may come from the informer cache.
242-
// We should abide by the principle of making a deep copy first and then modifying it.
243-
// See issue: https://github.com/karmada-io/karmada/issues/3878.
244241
func FetchResourceTemplate(
245242
ctx context.Context,
246243
dynamicClient dynamic.Interface,
@@ -266,12 +263,13 @@ func FetchResourceTemplate(
266263
// fall back to call api server in case the cache has not been synchronized yet
267264
klog.Warningf("Failed to get resource template (%s/%s/%s) from cache, Error: %v. Fall back to call api server.",
268265
resource.Kind, resource.Namespace, resource.Name, err)
269-
object, err = dynamicClient.Resource(gvr).Namespace(resource.Namespace).Get(ctx, resource.Name, metav1.GetOptions{})
266+
objectFromAPIServer, err := dynamicClient.Resource(gvr).Namespace(resource.Namespace).Get(ctx, resource.Name, metav1.GetOptions{})
270267
if err != nil {
271268
klog.Errorf("Failed to get resource template (%s/%s/%s) from api server, Error: %v",
272269
resource.Kind, resource.Namespace, resource.Name, err)
273270
return nil, err
274271
}
272+
return objectFromAPIServer, nil
275273
}
276274

277275
unstructuredObj, err := ToUnstructured(object)
@@ -280,7 +278,7 @@ func FetchResourceTemplate(
280278
return nil, err
281279
}
282280

283-
return unstructuredObj, nil
281+
return unstructuredObj.DeepCopy(), nil
284282
}
285283

286284
// FetchResourceTemplatesByLabelSelector fetches the resource templates by label selector to be propagated.

0 commit comments

Comments
 (0)