Skip to content

Commit ffdccf5

Browse files
committed
feat: supprot disable deepcopy on list funcion
Signed-off-by: qiankunli <[email protected]>
1 parent d991225 commit ffdccf5

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

pkg/cache/cache_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,19 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
17351735
})
17361736
})
17371737
})
1738+
Describe("use UnsafeDisableDeepCopy list options", func() {
1739+
It("should be able to change object in informer cache", func() {
1740+
By("listing pods")
1741+
out := corev1.PodList{}
1742+
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
1743+
out.Items[0].Labels["UnsafeDisableDeepCopy"] = "true"
1744+
1745+
By("verifying that the returned pods were changed")
1746+
out2 := corev1.PodList{}
1747+
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
1748+
Expect(out2.Items[0].Labels["UnsafeDisableDeepCopy"]).To(Equal("true"))
1749+
})
1750+
})
17381751
})
17391752
}
17401753

pkg/cache/internal/cache_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
161161
}
162162

163163
var outObj runtime.Object
164-
if c.disableDeepCopy {
164+
if c.disableDeepCopy || (listOpts.UnsafeDisableDeepCopy != nil && *listOpts.UnsafeDisableDeepCopy) {
165165
// skip deep copy which might be unsafe
166166
// you must DeepCopy any object before mutating it outside
167167
outObj = obj

pkg/client/options.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ type ListOptions struct {
417417
// it has expired. This field is not supported if watch is true in the Raw ListOptions.
418418
Continue string
419419

420+
// UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
421+
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
422+
// otherwise you will mutate the object in the cache.
423+
// +optional
424+
UnsafeDisableDeepCopy *bool
425+
420426
// Raw represents raw ListOptions, as passed to the API server. Note
421427
// that these may not be respected by all implementations of interface,
422428
// and the LabelSelector, FieldSelector, Limit and Continue fields are ignored.
@@ -445,6 +451,9 @@ func (o *ListOptions) ApplyToList(lo *ListOptions) {
445451
if o.Continue != "" {
446452
lo.Continue = o.Continue
447453
}
454+
if o.UnsafeDisableDeepCopy != nil {
455+
lo.UnsafeDisableDeepCopy = o.UnsafeDisableDeepCopy
456+
}
448457
}
449458

450459
// AsListOptions returns these options as a flattened metav1.ListOptions.
@@ -587,6 +596,25 @@ func (l Limit) ApplyToList(opts *ListOptions) {
587596
opts.Limit = int64(l)
588597
}
589598

599+
// UnsafeDisableDeepCopyOption indicates not to deep copy objects during list objects.
600+
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
601+
// otherwise you will mutate the object in the cache.
602+
type UnsafeDisableDeepCopyOption bool
603+
604+
// ApplyToList applies this configuration to the given an List options.
605+
func (d UnsafeDisableDeepCopyOption) ApplyToList(opts *ListOptions) {
606+
definitelyTrue := true
607+
definitelyFalse := false
608+
if d {
609+
opts.UnsafeDisableDeepCopy = &definitelyTrue
610+
} else {
611+
opts.UnsafeDisableDeepCopy = &definitelyFalse
612+
}
613+
}
614+
615+
// UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
616+
const UnsafeDisableDeepCopy = UnsafeDisableDeepCopyOption(true)
617+
590618
// Continue sets a continuation token to retrieve chunks of results when using limit.
591619
// Continue does not implement DeleteAllOfOption interface because the server
592620
// does not support setting it for deletecollection operations.

0 commit comments

Comments
 (0)