Skip to content

Commit a1e2ea2

Browse files
authored
Merge pull request #1409 from alvaroaleman/allow-new-client
⚠️ Allow setting NewClientFunc w/o implementing an interface
2 parents 4a8536e + aa1bfee commit a1e2ea2

File tree

6 files changed

+39
-130
lines changed

6 files changed

+39
-130
lines changed

pkg/cluster/client_builder.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/cluster/cluster.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ type Options struct {
109109
// by the manager. If not set this will use the default new cache function.
110110
NewCache cache.NewCacheFunc
111111

112-
// ClientBuilder is the builder that creates the client to be used by the manager.
112+
// NewClient is the func that creates the client to be used by the manager.
113113
// If not set this will create the default DelegatingClient that will
114114
// use the cache for reads and the client for writes.
115-
ClientBuilder ClientBuilder
115+
NewClient NewClientFunc
116116

117117
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
118118
// for the given objects.
@@ -174,9 +174,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
174174
return nil, err
175175
}
176176

177-
writeObj, err := options.ClientBuilder.
178-
WithUncached(options.ClientDisableCacheFor...).
179-
Build(cache, config, clientOptions)
177+
writeObj, err := options.NewClient(cache, config, clientOptions, options.ClientDisableCacheFor...)
180178
if err != nil {
181179
return nil, err
182180
}
@@ -219,9 +217,9 @@ func setOptionsDefaults(options Options) Options {
219217
}
220218
}
221219

222-
// Allow the client builder to be mocked
223-
if options.ClientBuilder == nil {
224-
options.ClientBuilder = NewClientBuilder()
220+
// Allow users to define how to create a new client
221+
if options.NewClient == nil {
222+
options.NewClient = DefaultNewClient
225223
}
226224

227225
// Allow newCache to be mocked
@@ -253,3 +251,20 @@ func setOptionsDefaults(options Options) Options {
253251

254252
return options
255253
}
254+
255+
// NewClientFunc allows a user to define how to create a client
256+
type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error)
257+
258+
// DefaultNewClient creates the default caching client
259+
func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
260+
c, err := client.New(config, options)
261+
if err != nil {
262+
return nil, err
263+
}
264+
265+
return client.NewDelegatingClient(client.NewDelegatingClientInput{
266+
CacheReader: cache,
267+
Client: c,
268+
UncachedObjects: uncachedObjects,
269+
})
270+
}

pkg/cluster/cluster_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cluster
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223

2324
"github.com/go-logr/logr"
@@ -35,18 +36,6 @@ import (
3536
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3637
)
3738

38-
type fakeClientBuilder struct {
39-
err error
40-
}
41-
42-
func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
43-
return e
44-
}
45-
46-
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
47-
return nil, e.err
48-
}
49-
5039
var _ = Describe("cluster.Cluster", func() {
5140
Describe("New", func() {
5241
It("should return an error if there is no Config", func() {
@@ -68,7 +57,9 @@ var _ = Describe("cluster.Cluster", func() {
6857

6958
It("should return an error it can't create a client.Client", func(done Done) {
7059
c, err := New(cfg, func(o *Options) {
71-
o.ClientBuilder = &fakeClientBuilder{err: fmt.Errorf("expected error")}
60+
o.NewClient = func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
61+
return nil, errors.New("expected error")
62+
}
7263
})
7364
Expect(c).To(BeNil())
7465
Expect(err).To(HaveOccurred())
@@ -92,7 +83,9 @@ var _ = Describe("cluster.Cluster", func() {
9283

9384
It("should create a client defined in by the new client function", func(done Done) {
9485
c, err := New(cfg, func(o *Options) {
95-
o.ClientBuilder = &fakeClientBuilder{}
86+
o.NewClient = func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
87+
return nil, nil
88+
}
9689
})
9790
Expect(c).ToNot(BeNil())
9891
Expect(err).ToNot(HaveOccurred())

pkg/manager/client_builder.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

pkg/manager/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ type Options struct {
226226
// by the manager. If not set this will use the default new cache function.
227227
NewCache cache.NewCacheFunc
228228

229-
// ClientBuilder is the builder that creates the client to be used by the manager.
229+
// NewClient is the func that creates the client to be used by the manager.
230230
// If not set this will create the default DelegatingClient that will
231231
// use the cache for reads and the client for writes.
232-
ClientBuilder ClientBuilder
232+
NewClient cluster.NewClientFunc
233233

234234
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
235235
// for the given objects.
@@ -309,7 +309,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
309309
clusterOptions.SyncPeriod = options.SyncPeriod
310310
clusterOptions.Namespace = options.Namespace
311311
clusterOptions.NewCache = options.NewCache
312-
clusterOptions.ClientBuilder = options.ClientBuilder
312+
clusterOptions.NewClient = options.NewClient
313313
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
314314
clusterOptions.DryRunClient = options.DryRunClient
315315
clusterOptions.EventBroadcaster = options.EventBroadcaster

pkg/manager/manager_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,6 @@ import (
5555
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
5656
)
5757

58-
type fakeClientBuilder struct {
59-
err error
60-
}
61-
62-
func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
63-
return e
64-
}
65-
66-
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
67-
return nil, e.err
68-
}
69-
7058
var _ = Describe("manger.Manager", func() {
7159
Describe("New", func() {
7260
It("should return an error if there is no Config", func() {
@@ -88,7 +76,9 @@ var _ = Describe("manger.Manager", func() {
8876

8977
It("should return an error it can't create a client.Client", func(done Done) {
9078
m, err := New(cfg, Options{
91-
ClientBuilder: &fakeClientBuilder{err: fmt.Errorf("expected error")},
79+
NewClient: func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
80+
return nil, errors.New("expected error")
81+
},
9282
})
9383
Expect(m).To(BeNil())
9484
Expect(err).To(HaveOccurred())
@@ -112,7 +102,9 @@ var _ = Describe("manger.Manager", func() {
112102

113103
It("should create a client defined in by the new client function", func(done Done) {
114104
m, err := New(cfg, Options{
115-
ClientBuilder: &fakeClientBuilder{},
105+
NewClient: func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
106+
return nil, nil
107+
},
116108
})
117109
Expect(m).ToNot(BeNil())
118110
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)