@@ -118,15 +118,30 @@ type Options struct {
118
118
// for serving prometheus metrics
119
119
MetricsBindAddress string
120
120
121
+ // Functions to all for a user to customize the values that will be injected.
122
+
123
+ // NewCache is the function that will create the cache to be used
124
+ // by the manager. If not set this will use the default new cache function.
125
+ NewCache NewCacheFunc
126
+
127
+ // NewClient will create the client to be used by the manager.
128
+ // If not set this will create the default DelegatingClient that will
129
+ // use the cache for reads and the client for writes.
130
+ NewClient NewClientFunc
131
+
121
132
// Dependency injection for testing
122
- newCache func (config * rest.Config , opts cache.Options ) (cache.Cache , error )
123
- newClient func (config * rest.Config , options client.Options ) (client.Client , error )
124
133
newRecorderProvider func (config * rest.Config , scheme * runtime.Scheme , logger logr.Logger ) (recorder.Provider , error )
125
134
newResourceLock func (config * rest.Config , recorderProvider recorder.Provider , options leaderelection.Options ) (resourcelock.Interface , error )
126
135
newAdmissionDecoder func (scheme * runtime.Scheme ) (types.Decoder , error )
127
136
newMetricsListener func (addr string ) (net.Listener , error )
128
137
}
129
138
139
+ // NewCacheFunc allows a user to define how to create a cache
140
+ type NewCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error )
141
+
142
+ // NewClientFunc allows a user to define how to create a client
143
+ type NewClientFunc func (cache cache.Cache , config * rest.Config , options client.Options ) (client.Client , error )
144
+
130
145
// Runnable allows a component to be started.
131
146
type Runnable interface {
132
147
// Start starts running the component. The component will stop running when the channel is closed.
@@ -159,14 +174,13 @@ func New(config *rest.Config, options Options) (Manager, error) {
159
174
return nil , err
160
175
}
161
176
162
- // Create the Client for Write operations.
163
- writeObj , err := options .newClient (config , client .Options {Scheme : options .Scheme , Mapper : mapper })
177
+ // Create the cache for the cached read client and registering informers
178
+ cache , err := options .NewCache (config , cache .Options {Scheme : options .Scheme , Mapper : mapper , Resync : options . SyncPeriod , Namespace : options . Namespace })
164
179
if err != nil {
165
180
return nil , err
166
181
}
167
182
168
- // Create the cache for the cached read client and registering informers
169
- cache , err := options .newCache (config , cache.Options {Scheme : options .Scheme , Mapper : mapper , Resync : options .SyncPeriod , Namespace : options .Namespace })
183
+ writeObj , err := options .NewClient (cache , config , client.Options {Scheme : options .Scheme , Mapper : mapper })
170
184
if err != nil {
171
185
return nil , err
172
186
}
@@ -209,14 +223,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
209
223
errChan : make (chan error ),
210
224
cache : cache ,
211
225
fieldIndexes : cache ,
212
- client : client.DelegatingClient {
213
- Reader : & client.DelegatingReader {
214
- CacheReader : cache ,
215
- ClientReader : writeObj ,
216
- },
217
- Writer : writeObj ,
218
- StatusClient : writeObj ,
219
- },
226
+ client : writeObj ,
220
227
recorderProvider : recorderProvider ,
221
228
resourceLock : resourceLock ,
222
229
mapper : mapper ,
@@ -226,6 +233,24 @@ func New(config *rest.Config, options Options) (Manager, error) {
226
233
}, nil
227
234
}
228
235
236
+ // defaultNewClient creates the default caching client
237
+ func defaultNewClient (cache cache.Cache , config * rest.Config , options client.Options ) (client.Client , error ) {
238
+ // Create the Client for Write operations.
239
+ c , err := client .New (config , options )
240
+ if err != nil {
241
+ return nil , err
242
+ }
243
+
244
+ return & client.DelegatingClient {
245
+ Reader : & client.DelegatingReader {
246
+ CacheReader : cache ,
247
+ ClientReader : c ,
248
+ },
249
+ Writer : c ,
250
+ StatusClient : c ,
251
+ }, nil
252
+ }
253
+
229
254
// setOptionsDefaults set default values for Options fields
230
255
func setOptionsDefaults (options Options ) Options {
231
256
// Use the Kubernetes client-go scheme if none is specified
@@ -238,13 +263,13 @@ func setOptionsDefaults(options Options) Options {
238
263
}
239
264
240
265
// Allow newClient to be mocked
241
- if options .newClient == nil {
242
- options .newClient = client . New
266
+ if options .NewClient == nil {
267
+ options .NewClient = defaultNewClient
243
268
}
244
269
245
270
// Allow newCache to be mocked
246
- if options .newCache == nil {
247
- options .newCache = cache .New
271
+ if options .NewCache == nil {
272
+ options .NewCache = cache .New
248
273
}
249
274
250
275
// Allow newRecorderProvider to be mocked
0 commit comments