Skip to content

Commit 59682cb

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

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

pkg/cache/cache_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"reflect"
2323
"sort"
2424
"strconv"
25+
"strings"
2526

2627
. "github.com/onsi/ginkgo/v2"
2728
. "github.com/onsi/gomega"
@@ -1735,6 +1736,29 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
17351736
})
17361737
})
17371738
})
1739+
Describe("use UnsafeDisableDeepCopy list options", func() {
1740+
It("should be able to change object in informer cache", func() {
1741+
By("listing pods")
1742+
out := corev1.PodList{}
1743+
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
1744+
for _, item := range out.Items {
1745+
if strings.Compare(item.Name, "test-pod-3") == 0 { // test-pod-3 has labels
1746+
item.Labels["UnsafeDisableDeepCopy"] = "true"
1747+
break
1748+
}
1749+
}
1750+
1751+
By("verifying that the returned pods were changed")
1752+
out2 := corev1.PodList{}
1753+
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
1754+
for _, item := range out2.Items {
1755+
if strings.Compare(item.Name, "test-pod-3") == 0 {
1756+
Expect(item.Labels["UnsafeDisableDeepCopy"]).To(Equal("true"))
1757+
break
1758+
}
1759+
}
1760+
})
1761+
})
17381762
})
17391763
}
17401764

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)