Skip to content

Commit aa1bfee

Browse files
committed
⚠️ Allow setting NewClientFunc w/o implementing an interface
When we introduced the ClientBuilder we lost the ability to easily overwrite the NewClient func, as interfaces can not be implemented inline. This change reverts us back to using a NewClientFunc but with a slightly changed signature so objects that should not be cached can still be configured. In order to not provide two ways to do the same thing, the ClientBuilder is removed.
1 parent e388e1e commit aa1bfee

File tree

6 files changed

+39
-130
lines changed

6 files changed

+39
-130
lines changed

pkg/cluster/client_builder.go

-62
This file was deleted.

pkg/cluster/cluster.go

+23-8
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

+7-14
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

-29
This file was deleted.

pkg/manager/manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ type Options struct {
207207
// by the manager. If not set this will use the default new cache function.
208208
NewCache cache.NewCacheFunc
209209

210-
// ClientBuilder is the builder that creates the client to be used by the manager.
210+
// NewClient is the func that creates the client to be used by the manager.
211211
// If not set this will create the default DelegatingClient that will
212212
// use the cache for reads and the client for writes.
213-
ClientBuilder ClientBuilder
213+
NewClient cluster.NewClientFunc
214214

215215
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
216216
// for the given objects.
@@ -290,7 +290,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
290290
clusterOptions.SyncPeriod = options.SyncPeriod
291291
clusterOptions.Namespace = options.Namespace
292292
clusterOptions.NewCache = options.NewCache
293-
clusterOptions.ClientBuilder = options.ClientBuilder
293+
clusterOptions.NewClient = options.NewClient
294294
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
295295
clusterOptions.DryRunClient = options.DryRunClient
296296
clusterOptions.EventBroadcaster = options.EventBroadcaster

pkg/manager/manager_test.go

+6-14
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)