Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Allow setting NewClientFunc w/o implementing an interface #1409

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 0 additions & 62 deletions pkg/cluster/client_builder.go

This file was deleted.

31 changes: 23 additions & 8 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ type Options struct {
// by the manager. If not set this will use the default new cache function.
NewCache cache.NewCacheFunc

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

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

writeObj, err := options.ClientBuilder.
WithUncached(options.ClientDisableCacheFor...).
Build(cache, config, clientOptions)
writeObj, err := options.NewClient(cache, config, clientOptions, options.ClientDisableCacheFor...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -219,9 +217,9 @@ func setOptionsDefaults(options Options) Options {
}
}

// Allow the client builder to be mocked
if options.ClientBuilder == nil {
options.ClientBuilder = NewClientBuilder()
// Allow users to define how to create a new client
if options.NewClient == nil {
options.NewClient = DefaultNewClient
}

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

return options
}

// NewClientFunc allows a user to define how to create a client
type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error)

// DefaultNewClient creates the default caching client
func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to consider making this options or something in the future -- this is getting a bit unwieldy

c, err := client.New(config, options)
if err != nil {
return nil, err
}

return client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: cache,
Client: c,
UncachedObjects: uncachedObjects,
})
}
21 changes: 7 additions & 14 deletions pkg/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cluster

import (
"context"
"errors"
"fmt"

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

type fakeClientBuilder struct {
err error
}

func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
return e
}

func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
return nil, e.err
}

var _ = Describe("cluster.Cluster", func() {
Describe("New", func() {
It("should return an error if there is no Config", func() {
Expand All @@ -68,7 +57,9 @@ var _ = Describe("cluster.Cluster", func() {

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

It("should create a client defined in by the new client function", func(done Done) {
c, err := New(cfg, func(o *Options) {
o.ClientBuilder = &fakeClientBuilder{}
o.NewClient = func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
return nil, nil
}
})
Expect(c).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
Expand Down
29 changes: 0 additions & 29 deletions pkg/manager/client_builder.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ type Options struct {
// by the manager. If not set this will use the default new cache function.
NewCache cache.NewCacheFunc

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

// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
// for the given objects.
Expand Down Expand Up @@ -290,7 +290,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
clusterOptions.SyncPeriod = options.SyncPeriod
clusterOptions.Namespace = options.Namespace
clusterOptions.NewCache = options.NewCache
clusterOptions.ClientBuilder = options.ClientBuilder
clusterOptions.NewClient = options.NewClient
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
clusterOptions.DryRunClient = options.DryRunClient
clusterOptions.EventBroadcaster = options.EventBroadcaster
Expand Down
20 changes: 6 additions & 14 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
)

type fakeClientBuilder struct {
err error
}

func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
return e
}

func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
return nil, e.err
}

var _ = Describe("manger.Manager", func() {
Describe("New", func() {
It("should return an error if there is no Config", func() {
Expand All @@ -88,7 +76,9 @@ var _ = Describe("manger.Manager", func() {

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

It("should create a client defined in by the new client function", func(done Done) {
m, err := New(cfg, Options{
ClientBuilder: &fakeClientBuilder{},
NewClient: func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
return nil, nil
},
})
Expect(m).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
Expand Down