Skip to content

Commit b25daf9

Browse files
Add UnsafeDisableDeepCopy to GetOptions
1 parent 71f7db5 commit b25daf9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ type CacheReader struct {
5454
}
5555

5656
// Get checks the indexer for the object and writes a copy of it if found.
57-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object, _ ...client.GetOption) error {
57+
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
58+
getOpts := client.GetOptions{}
59+
getOpts.ApplyOptions(opts)
60+
5861
if c.scopeName == apimeta.RESTScopeNameRoot {
5962
key.Namespace = ""
6063
}
@@ -81,7 +84,7 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Ob
8184
return fmt.Errorf("cache contained %T, which is not an Object", obj)
8285
}
8386

84-
if c.disableDeepCopy {
87+
if c.disableDeepCopy || (getOpts.UnsafeDisableDeepCopy != nil && *getOpts.UnsafeDisableDeepCopy) {
8588
// skip deep copy which might be unsafe
8689
// you must DeepCopy any object before mutating it outside
8790
} else {
@@ -97,7 +100,7 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Ob
97100
return fmt.Errorf("cache had type %s, but %s was asked for", objVal.Type(), outVal.Type())
98101
}
99102
reflect.Indirect(outVal).Set(reflect.Indirect(objVal))
100-
if !c.disableDeepCopy {
103+
if !c.disableDeepCopy && (getOpts.UnsafeDisableDeepCopy == nil || !*getOpts.UnsafeDisableDeepCopy) {
101104
out.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
102105
}
103106

pkg/client/options.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ type GetOptions struct {
431431
// Raw represents raw GetOptions, as passed to the API server. Note
432432
// that these may not be respected by all implementations of interface.
433433
Raw *metav1.GetOptions
434+
435+
// UnsafeDisableDeepCopy indicates not to deep copy objects during get object.
436+
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
437+
// otherwise you will mutate the object in the cache.
438+
// +optional
439+
UnsafeDisableDeepCopy *bool
434440
}
435441

436442
var _ GetOption = &GetOptions{}
@@ -440,6 +446,9 @@ func (o *GetOptions) ApplyToGet(lo *GetOptions) {
440446
if o.Raw != nil {
441447
lo.Raw = o.Raw
442448
}
449+
if o.UnsafeDisableDeepCopy != nil {
450+
lo.UnsafeDisableDeepCopy = o.UnsafeDisableDeepCopy
451+
}
443452
}
444453

445454
// AsGetOptions returns these options as a flattened metav1.GetOptions.
@@ -692,6 +701,17 @@ func (l Limit) ApplyToList(opts *ListOptions) {
692701
// otherwise you will mutate the object in the cache.
693702
type UnsafeDisableDeepCopyOption bool
694703

704+
// ApplyToGet applies this configuration to the given an Get options.
705+
func (d UnsafeDisableDeepCopyOption) ApplyToGet(opts *GetOptions) {
706+
definitelyTrue := true
707+
definitelyFalse := false
708+
if d {
709+
opts.UnsafeDisableDeepCopy = &definitelyTrue
710+
} else {
711+
opts.UnsafeDisableDeepCopy = &definitelyFalse
712+
}
713+
}
714+
695715
// ApplyToList applies this configuration to the given an List options.
696716
func (d UnsafeDisableDeepCopyOption) ApplyToList(opts *ListOptions) {
697717
definitelyTrue := true

0 commit comments

Comments
 (0)