Skip to content

Commit 611ea97

Browse files
committed
⚠️ Inteceptor: Unify Client and Subresource Client
At the moment, the inteceptor has two different constructors for a client interceptor and a subresource interceptor. This means that in order to intercept subresource requests, one has to set up a normal interceptor, set the `SubResource` func to return a subresource interceptor and then configure the desired behavior on the subresource interceptor. This is needlessly complex, unify the two and add the parameters for the subresource funcs into the top-level `Funcs` struct.
1 parent eb0ebc9 commit 611ea97

File tree

3 files changed

+98
-88
lines changed

3 files changed

+98
-88
lines changed

pkg/client/interceptor/intercept.go

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,29 @@ import (
1010
"sigs.k8s.io/controller-runtime/pkg/client"
1111
)
1212

13-
type (
14-
15-
// Funcs contains functions that are called instead of the underlying client's methods.
16-
Funcs struct {
17-
Get func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
18-
List func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
19-
Create func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
20-
Delete func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
21-
DeleteAllOf func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
22-
Update func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
23-
Patch func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
24-
Watch func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
25-
SubResource func(client client.WithWatch, subResource string) client.SubResourceClient
26-
}
27-
28-
// SubResourceFuncs is a set of functions that can be used to intercept calls to a SubResourceClient.
29-
SubResourceFuncs struct {
30-
Get func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
31-
Create func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
32-
Update func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error
33-
Patch func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
34-
}
35-
)
13+
// Funcs contains functions that are called instead of the underlying client's methods.
14+
type Funcs struct {
15+
Get func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
16+
List func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
17+
Create func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
18+
Delete func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
19+
DeleteAllOf func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
20+
Update func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
21+
Patch func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
22+
Watch func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
23+
SubResource func(client client.WithWatch, subResource string) client.SubResourceClient
24+
SubResourceGet func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
25+
SubResourceCreate func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
26+
SubResourceUpdate func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error
27+
SubResourcePatch func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
28+
}
3629

3730
// NewClient returns a new interceptor client that calls the functions in funcs instead of the underlying client's methods, if they are not nil.
3831
func NewClient(interceptedClient client.WithWatch, funcs Funcs) client.WithWatch {
39-
return interceptor{client: interceptedClient, funcs: funcs}
40-
}
41-
42-
// NewSubResourceClient returns a SubResourceClient that intercepts calls to the provided client with the provided functions.
43-
func NewSubResourceClient(interceptedClient client.SubResourceClient, funcs SubResourceFuncs) client.SubResourceClient {
44-
return subResourceInterceptor{client: interceptedClient, funcs: funcs}
32+
return interceptor{
33+
client: interceptedClient,
34+
funcs: funcs,
35+
}
4536
}
4637

4738
type interceptor struct {
@@ -116,7 +107,10 @@ func (c interceptor) SubResource(subResource string) client.SubResourceClient {
116107
if c.funcs.SubResource != nil {
117108
return c.funcs.SubResource(c.client, subResource)
118109
}
119-
return c.client.SubResource(subResource)
110+
return subResourceInterceptor{
111+
client: c.client.SubResource(subResource),
112+
funcs: c.funcs,
113+
}
120114
}
121115

122116
func (c interceptor) Scheme() *runtime.Scheme {
@@ -136,35 +130,35 @@ func (c interceptor) Watch(ctx context.Context, obj client.ObjectList, opts ...c
136130

137131
type subResourceInterceptor struct {
138132
client client.SubResourceClient
139-
funcs SubResourceFuncs
133+
funcs Funcs
140134
}
141135

142136
var _ client.SubResourceClient = &subResourceInterceptor{}
143137

144138
func (s subResourceInterceptor) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
145-
if s.funcs.Get != nil {
146-
return s.funcs.Get(ctx, s.client, obj, subResource, opts...)
139+
if s.funcs.SubResourceGet != nil {
140+
return s.funcs.SubResourceGet(ctx, s.client, obj, subResource, opts...)
147141
}
148142
return s.client.Get(ctx, obj, subResource, opts...)
149143
}
150144

151145
func (s subResourceInterceptor) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
152-
if s.funcs.Create != nil {
153-
return s.funcs.Create(ctx, s.client, obj, subResource, opts...)
146+
if s.funcs.SubResourceCreate != nil {
147+
return s.funcs.SubResourceCreate(ctx, s.client, obj, subResource, opts...)
154148
}
155149
return s.client.Create(ctx, obj, subResource, opts...)
156150
}
157151

158152
func (s subResourceInterceptor) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
159-
if s.funcs.Update != nil {
160-
return s.funcs.Update(ctx, s.client, obj, opts...)
153+
if s.funcs.SubResourceUpdate != nil {
154+
return s.funcs.SubResourceUpdate(ctx, s.client, obj, opts...)
161155
}
162156
return s.client.Update(ctx, obj, opts...)
163157
}
164158

165159
func (s subResourceInterceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
166-
if s.funcs.Patch != nil {
167-
return s.funcs.Patch(ctx, s.client, obj, patch, opts...)
160+
if s.funcs.SubResourcePatch != nil {
161+
return s.funcs.SubResourcePatch(ctx, s.client, obj, patch, opts...)
168162
}
169163
return s.client.Patch(ctx, obj, patch, opts...)
170164
}

pkg/client/interceptor/intercept_test.go

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -254,98 +254,98 @@ var _ = Describe("NewClient", func() {
254254
})
255255

256256
var _ = Describe("NewSubResourceClient", func() {
257-
srClient := dummySubResourceClient{}
257+
c := dummyClient{}
258258
ctx := context.Background()
259259
It("should call the provided Get function", func() {
260260
var called bool
261-
client := NewSubResourceClient(srClient, SubResourceFuncs{
262-
Get: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
261+
c := NewClient(c, Funcs{
262+
SubResourceGet: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
263263
called = true
264264
return nil
265265
},
266266
})
267-
_ = client.Get(ctx, nil, nil)
267+
_ = c.SubResource("foo").Get(ctx, nil, nil)
268268
Expect(called).To(BeTrue())
269269
})
270270
It("should call the underlying client if the provided Get function is nil", func() {
271271
var called bool
272-
client1 := NewSubResourceClient(srClient, SubResourceFuncs{
273-
Get: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
272+
client1 := NewClient(c, Funcs{
273+
SubResourceGet: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
274274
called = true
275275
return nil
276276
},
277277
})
278-
client2 := NewSubResourceClient(client1, SubResourceFuncs{})
279-
_ = client2.Get(ctx, nil, nil)
278+
client2 := NewClient(client1, Funcs{})
279+
_ = client2.SubResource("foo").Get(ctx, nil, nil)
280280
Expect(called).To(BeTrue())
281281
})
282282
It("should call the provided Update function", func() {
283283
var called bool
284-
client := NewSubResourceClient(srClient, SubResourceFuncs{
285-
Update: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
284+
client := NewClient(c, Funcs{
285+
SubResourceUpdate: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
286286
called = true
287287
return nil
288288
},
289289
})
290-
_ = client.Update(ctx, nil, nil)
290+
_ = client.SubResource("foo").Update(ctx, nil, nil)
291291
Expect(called).To(BeTrue())
292292
})
293293
It("should call the underlying client if the provided Update function is nil", func() {
294294
var called bool
295-
client1 := NewSubResourceClient(srClient, SubResourceFuncs{
296-
Update: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
295+
client1 := NewClient(c, Funcs{
296+
SubResourceUpdate: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
297297
called = true
298298
return nil
299299
},
300300
})
301-
client2 := NewSubResourceClient(client1, SubResourceFuncs{})
302-
_ = client2.Update(ctx, nil, nil)
301+
client2 := NewClient(client1, Funcs{})
302+
_ = client2.SubResource("foo").Update(ctx, nil, nil)
303303
Expect(called).To(BeTrue())
304304
})
305305
It("should call the provided Patch function", func() {
306306
var called bool
307-
client := NewSubResourceClient(srClient, SubResourceFuncs{
308-
Patch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
307+
client := NewClient(c, Funcs{
308+
SubResourcePatch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
309309
called = true
310310
return nil
311311
},
312312
})
313-
_ = client.Patch(ctx, nil, nil)
313+
_ = client.SubResource("foo").Patch(ctx, nil, nil)
314314
Expect(called).To(BeTrue())
315315
})
316316
It("should call the underlying client if the provided Patch function is nil", func() {
317317
var called bool
318-
client1 := NewSubResourceClient(srClient, SubResourceFuncs{
319-
Patch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
318+
client1 := NewClient(c, Funcs{
319+
SubResourcePatch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
320320
called = true
321321
return nil
322322
},
323323
})
324-
client2 := NewSubResourceClient(client1, SubResourceFuncs{})
325-
_ = client2.Patch(ctx, nil, nil)
324+
client2 := NewClient(client1, Funcs{})
325+
_ = client2.SubResource("foo").Patch(ctx, nil, nil)
326326
Expect(called).To(BeTrue())
327327
})
328328
It("should call the provided Create function", func() {
329329
var called bool
330-
client := NewSubResourceClient(srClient, SubResourceFuncs{
331-
Create: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
330+
client := NewClient(c, Funcs{
331+
SubResourceCreate: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
332332
called = true
333333
return nil
334334
},
335335
})
336-
_ = client.Create(ctx, nil, nil)
336+
_ = client.SubResource("foo").Create(ctx, nil, nil)
337337
Expect(called).To(BeTrue())
338338
})
339339
It("should call the underlying client if the provided Create function is nil", func() {
340340
var called bool
341-
client1 := NewSubResourceClient(srClient, SubResourceFuncs{
342-
Create: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
341+
client1 := NewClient(c, Funcs{
342+
SubResourceCreate: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
343343
called = true
344344
return nil
345345
},
346346
})
347-
client2 := NewSubResourceClient(client1, SubResourceFuncs{})
348-
_ = client2.Create(ctx, nil, nil)
347+
client2 := NewClient(client1, Funcs{})
348+
_ = client2.SubResource("foo").Create(ctx, nil, nil)
349349
Expect(called).To(BeTrue())
350350
})
351351
})
@@ -409,23 +409,3 @@ func (d dummyClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
409409
func (d dummyClient) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
410410
return nil, nil
411411
}
412-
413-
type dummySubResourceClient struct{}
414-
415-
var _ client.SubResourceClient = &dummySubResourceClient{}
416-
417-
func (d dummySubResourceClient) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
418-
return nil
419-
}
420-
421-
func (d dummySubResourceClient) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
422-
return nil
423-
}
424-
425-
func (d dummySubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
426-
return nil
427-
}
428-
429-
func (d dummySubResourceClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
430-
return nil
431-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022 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 interceptor
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
25+
logf "sigs.k8s.io/controller-runtime/pkg/log"
26+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
27+
)
28+
29+
func TestInterceptor(t *testing.T) {
30+
RegisterFailHandler(Fail)
31+
RunSpecs(t, "Fake client Suite")
32+
}
33+
34+
var _ = BeforeSuite(func() {
35+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
36+
})

0 commit comments

Comments
 (0)