Skip to content

Commit

Permalink
feat: supprot disable deepcopy on list funcion
Browse files Browse the repository at this point in the history
Signed-off-by: qiankunli <qiankun.li@qq.com>
  • Loading branch information
qiankunli committed Dec 6, 2022
1 parent d991225 commit d04e5a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/cache/cache_test.go
Expand Up @@ -1735,6 +1735,19 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
})
})
})
Describe("use UnsafeDisableDeepCopy list options", func() {
It("should be able to change object in informer cache", func() {
By("listing pods")
out := corev1.PodList{}
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
out.Items[0].Labels["UnsafeDisableDeepCopy"] = "true"

By("verifying that the returned pods were changed")
out2 := corev1.PodList{}
Expect(informerCache.List(context.Background(), &out, client.UnsafeDisableDeepCopy)).To(Succeed())
Expect(out2.Items[0].Labels["UnsafeDisableDeepCopy"]).To(Equal("true"))
})
})
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/internal/cache_reader.go
Expand Up @@ -161,7 +161,7 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
}

var outObj runtime.Object
if c.disableDeepCopy {
if c.disableDeepCopy || *listOpts.UnsafeDisableDeepCopy {
// skip deep copy which might be unsafe
// you must DeepCopy any object before mutating it outside
outObj = obj
Expand Down
28 changes: 28 additions & 0 deletions pkg/client/options.go
Expand Up @@ -417,6 +417,12 @@ type ListOptions struct {
// it has expired. This field is not supported if watch is true in the Raw ListOptions.
Continue string

// UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
// otherwise you will mutate the object in the cache.
// +optional
UnsafeDisableDeepCopy *bool

// Raw represents raw ListOptions, as passed to the API server. Note
// that these may not be respected by all implementations of interface,
// and the LabelSelector, FieldSelector, Limit and Continue fields are ignored.
Expand Down Expand Up @@ -445,6 +451,9 @@ func (o *ListOptions) ApplyToList(lo *ListOptions) {
if o.Continue != "" {
lo.Continue = o.Continue
}
if o.UnsafeDisableDeepCopy != nil {
lo.UnsafeDisableDeepCopy = o.UnsafeDisableDeepCopy
}
}

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

// UnsafeDisableDeepCopyOption indicates not to deep copy objects during list objects.
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
// otherwise you will mutate the object in the cache.
type UnsafeDisableDeepCopyOption bool

// ApplyToList applies this configuration to the given an List options.
func (d UnsafeDisableDeepCopyOption) ApplyToList(opts *ListOptions) {
definitelyTrue := true
definitelyFalse := false
if d {
opts.UnsafeDisableDeepCopy = &definitelyTrue
} else {
opts.UnsafeDisableDeepCopy = &definitelyFalse
}
}

// UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
const UnsafeDisableDeepCopy = UnsafeDisableDeepCopyOption(true)

// Continue sets a continuation token to retrieve chunks of results when using limit.
// Continue does not implement DeleteAllOfOption interface because the server
// does not support setting it for deletecollection operations.
Expand Down

0 comments on commit d04e5a6

Please sign in to comment.