Skip to content

Commit 4c0ea9d

Browse files
authored
Merge pull request kubernetes-sigs#227 from shawn-hurley/issue/226
✨ Allow user to define how create managed objects
2 parents 1f7c0c6 + 8fba660 commit 4c0ea9d

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

pkg/manager/manager.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,30 @@ type Options struct {
118118
// for serving prometheus metrics
119119
MetricsBindAddress string
120120

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+
121132
// 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)
124133
newRecorderProvider func(config *rest.Config, scheme *runtime.Scheme, logger logr.Logger) (recorder.Provider, error)
125134
newResourceLock func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error)
126135
newAdmissionDecoder func(scheme *runtime.Scheme) (types.Decoder, error)
127136
newMetricsListener func(addr string) (net.Listener, error)
128137
}
129138

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+
130145
// Runnable allows a component to be started.
131146
type Runnable interface {
132147
// 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) {
159174
return nil, err
160175
}
161176

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})
164179
if err != nil {
165180
return nil, err
166181
}
167182

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})
170184
if err != nil {
171185
return nil, err
172186
}
@@ -209,14 +223,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
209223
errChan: make(chan error),
210224
cache: cache,
211225
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,
220227
recorderProvider: recorderProvider,
221228
resourceLock: resourceLock,
222229
mapper: mapper,
@@ -226,6 +233,24 @@ func New(config *rest.Config, options Options) (Manager, error) {
226233
}, nil
227234
}
228235

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+
229254
// setOptionsDefaults set default values for Options fields
230255
func setOptionsDefaults(options Options) Options {
231256
// Use the Kubernetes client-go scheme if none is specified
@@ -238,13 +263,13 @@ func setOptionsDefaults(options Options) Options {
238263
}
239264

240265
// Allow newClient to be mocked
241-
if options.newClient == nil {
242-
options.newClient = client.New
266+
if options.NewClient == nil {
267+
options.NewClient = defaultNewClient
243268
}
244269

245270
// 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
248273
}
249274

250275
// Allow newRecorderProvider to be mocked

pkg/manager/manager_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var _ = Describe("manger.Manager", func() {
7272

7373
It("should return an error it can't create a client.Client", func(done Done) {
7474
m, err := New(cfg, Options{
75-
newClient: func(config *rest.Config, options client.Options) (client.Client, error) {
75+
NewClient: func(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
7676
return nil, fmt.Errorf("expected error")
7777
},
7878
})
@@ -85,7 +85,7 @@ var _ = Describe("manger.Manager", func() {
8585

8686
It("should return an error it can't create a cache.Cache", func(done Done) {
8787
m, err := New(cfg, Options{
88-
newCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
88+
NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
8989
return nil, fmt.Errorf("expected error")
9090
},
9191
})
@@ -95,6 +95,20 @@ var _ = Describe("manger.Manager", func() {
9595

9696
close(done)
9797
})
98+
99+
It("should create a client defined in by the new client function", func(done Done) {
100+
m, err := New(cfg, Options{
101+
NewClient: func(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
102+
return nil, nil
103+
},
104+
})
105+
Expect(m).ToNot(BeNil())
106+
Expect(err).ToNot(HaveOccurred())
107+
Expect(m.GetClient()).To(BeNil())
108+
109+
close(done)
110+
})
111+
98112
It("should return an error it can't create a recorder.Provider", func(done Done) {
99113
m, err := New(cfg, Options{
100114
newRecorderProvider: func(config *rest.Config, scheme *runtime.Scheme, logger logr.Logger) (recorder.Provider, error) {

0 commit comments

Comments
 (0)