@@ -113,6 +113,12 @@ type Options struct {
113
113
// [1] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Selector
114
114
// [2] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Set
115
115
SelectorsByObject SelectorsByObject
116
+
117
+ // UnsafeDisableDeepCopyByObject indicates not to deep copy objects during get or
118
+ // list objects per GVK at the specified object.
119
+ // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
120
+ // otherwise you will mutate the object in the cache.
121
+ UnsafeDisableDeepCopyByObject DisableDeepCopyByObject
116
122
}
117
123
118
124
var defaultResyncTime = 10 * time .Hour
@@ -127,7 +133,11 @@ func New(config *rest.Config, opts Options) (Cache, error) {
127
133
if err != nil {
128
134
return nil , err
129
135
}
130
- im := internal .NewInformersMap (config , opts .Scheme , opts .Mapper , * opts .Resync , opts .Namespace , selectorsByGVK )
136
+ disableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (opts .UnsafeDisableDeepCopyByObject , opts .Scheme )
137
+ if err != nil {
138
+ return nil , err
139
+ }
140
+ im := internal .NewInformersMap (config , opts .Scheme , opts .Mapper , * opts .Resync , opts .Namespace , selectorsByGVK , disableDeepCopyByGVK )
131
141
return & informerCache {InformersMap : im }, nil
132
142
}
133
143
@@ -136,6 +146,8 @@ func New(config *rest.Config, opts Options) (Cache, error) {
136
146
// SelectorsByObject
137
147
// WARNING: if SelectorsByObject is specified. filtered out resources are not
138
148
// returned.
149
+ // WARNING: if UnsafeDisableDeepCopy is enabled, you must DeepCopy any object
150
+ // returned from cache get/list before mutating it.
139
151
func BuilderWithOptions (options Options ) NewCacheFunc {
140
152
return func (config * rest.Config , opts Options ) (Cache , error ) {
141
153
if opts .Scheme == nil {
@@ -151,6 +163,7 @@ func BuilderWithOptions(options Options) NewCacheFunc {
151
163
opts .Namespace = options .Namespace
152
164
}
153
165
opts .SelectorsByObject = options .SelectorsByObject
166
+ opts .UnsafeDisableDeepCopyByObject = options .UnsafeDisableDeepCopyByObject
154
167
return New (config , opts )
155
168
}
156
169
}
@@ -189,3 +202,30 @@ func convertToSelectorsByGVK(selectorsByObject SelectorsByObject, scheme *runtim
189
202
}
190
203
return selectorsByGVK , nil
191
204
}
205
+
206
+ // DisableDeepCopyByObject associate a client.Object's GVK to disable DeepCopy during get or list from cache.
207
+ type DisableDeepCopyByObject map [client.Object ]bool
208
+
209
+ var _ client.Object = & ObjectAll {}
210
+
211
+ // ObjectAll is the argument to represent all objects' types.
212
+ type ObjectAll struct {
213
+ client.Object
214
+ }
215
+
216
+ func convertToDisableDeepCopyByGVK (disableDeepCopyByObject DisableDeepCopyByObject , scheme * runtime.Scheme ) (internal.DisableDeepCopyByGVK , error ) {
217
+ disableDeepCopyByGVK := internal.DisableDeepCopyByGVK {}
218
+ for obj , disable := range disableDeepCopyByObject {
219
+ switch obj .(type ) {
220
+ case ObjectAll , * ObjectAll :
221
+ disableDeepCopyByGVK [internal .GroupVersionKindAll ] = disable
222
+ default :
223
+ gvk , err := apiutil .GVKForObject (obj , scheme )
224
+ if err != nil {
225
+ return nil , err
226
+ }
227
+ disableDeepCopyByGVK [gvk ] = disable
228
+ }
229
+ }
230
+ return disableDeepCopyByGVK , nil
231
+ }
0 commit comments