Skip to content

Commit ded66c7

Browse files
committed
⚠️ Allow setting NewClientFunc w/o re-implementing ClientBuilder
When introducing the ClientBuilder we lost the ability to easily overwrite the NewClientFunc, this re-adds it.
1 parent e388e1e commit ded66c7

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

pkg/cluster/client_builder.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type ClientBuilder interface {
2929
// for this client. This function can be called multiple times, it should append to an internal slice.
3030
WithUncached(objs ...client.Object) ClientBuilder
3131

32+
// WithNewClientFunc can be used to override the default NewClientFunc.
33+
WithNewClientFunc(NewClientFunc) ClientBuilder
34+
3235
// Build returns a new client.
3336
Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error)
3437
}
@@ -39,16 +42,34 @@ func NewClientBuilder() ClientBuilder {
3942
}
4043

4144
type newClientBuilder struct {
42-
uncached []client.Object
45+
uncached []client.Object
46+
newClientFunc NewClientFunc
4347
}
4448

4549
func (n *newClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
4650
n.uncached = append(n.uncached, objs...)
4751
return n
4852
}
4953

54+
func (n *newClientBuilder) WithNewClientFunc(f NewClientFunc) ClientBuilder {
55+
n.newClientFunc = f
56+
return n
57+
}
58+
5059
func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
51-
// Create the Client for Write operations.
60+
if n.newClientFunc == nil {
61+
n.newClientFunc = DefaultNewClientFunc
62+
}
63+
64+
return n.newClientFunc(cache, config, options, n.uncached...)
65+
}
66+
67+
// NewClientFunc is the signature of the func that is called to construct a new client.
68+
type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error)
69+
70+
// DefaultNewClientFunc is the default NewClientFunc for the Builder. It's Reader interface
71+
// is cache-backed.
72+
func DefaultNewClientFunc(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
5273
c, err := client.New(config, options)
5374
if err != nil {
5475
return nil, err
@@ -57,6 +78,6 @@ func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options
5778
return client.NewDelegatingClient(client.NewDelegatingClientInput{
5879
CacheReader: cache,
5980
Client: c,
60-
UncachedObjects: n.uncached,
81+
UncachedObjects: uncachedObjects,
6182
})
6283
}

pkg/cluster/client_builder_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cluster
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
23+
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/client-go/rest"
25+
26+
"sigs.k8s.io/controller-runtime/pkg/cache"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
)
29+
30+
type recordingNewClientFunc struct {
31+
uncachedObjects []client.Object
32+
hasBeenCalled bool
33+
}
34+
35+
func (r *recordingNewClientFunc) Func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
36+
r.uncachedObjects = uncachedObjects
37+
r.hasBeenCalled = true
38+
return nil, nil
39+
}
40+
41+
var _ = Describe("cluster.ClientBuilder", func() {
42+
It("should set new client func", func() {
43+
recorder := &recordingNewClientFunc{}
44+
_, err := NewClientBuilder().WithNewClientFunc(recorder.Func).Build(nil, nil, client.Options{})
45+
Expect(err).NotTo(HaveOccurred())
46+
Expect(recorder.hasBeenCalled).To(BeTrue())
47+
})
48+
49+
It("should set uncached objects", func() {
50+
recorder := &recordingNewClientFunc{}
51+
_, err := NewClientBuilder().WithUncached(&corev1.Pod{}).WithNewClientFunc(recorder.Func).Build(nil, nil, client.Options{})
52+
Expect(err).NotTo(HaveOccurred())
53+
Expect(recorder.hasBeenCalled).To(BeTrue())
54+
Expect(recorder.uncachedObjects).To(Equal([]client.Object{&corev1.Pod{}}))
55+
})
56+
})

pkg/cluster/cluster_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
4343
return e
4444
}
4545

46+
func (e *fakeClientBuilder) WithNewClientFunc(f NewClientFunc) ClientBuilder {
47+
return e
48+
}
49+
4650
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
4751
return nil, e.err
4852
}

pkg/manager/manager_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
6363
return e
6464
}
6565

66+
func (e *fakeClientBuilder) WithNewClientFunc(f cluster.NewClientFunc) ClientBuilder {
67+
return e
68+
}
69+
6670
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
6771
return nil, e.err
6872
}

0 commit comments

Comments
 (0)