Skip to content

Commit 5221233

Browse files
committed
Move interceptor to new package
1 parent d74b869 commit 5221233

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

pkg/client/fake/intercept.go pkg/client/interceptor/intercept.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fake
1+
package interceptor
22

33
import (
44
"context"
@@ -10,16 +10,15 @@ import (
1010
"sigs.k8s.io/controller-runtime/pkg/client"
1111
)
1212

13-
// Interceptor is a client that intercepts calls to the underlying client and
14-
// calls the provided functions instead. If the function is nil, the call is
15-
// forwarded to the underlying client.
16-
func Interceptor(client client.WithWatch, fns InterceptorFns) client.WithWatch {
13+
// NewClient returns a new interceptor client that calls the functions in fns
14+
// instead of the underlying client's methods, if they are not nil.
15+
func NewClient(client client.WithWatch, fns Fns) client.WithWatch {
1716
return interceptor{client: client, fns: fns}
1817
}
1918

20-
// InterceptorFns contains functions that are called instead of the underlying
19+
// Fns contains functions that are called instead of the underlying
2120
// client's methods.
22-
type InterceptorFns struct {
21+
type Fns struct {
2322
Get func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
2423
List func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
2524
Create func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
@@ -33,7 +32,7 @@ type InterceptorFns struct {
3332

3433
type interceptor struct {
3534
client client.WithWatch
36-
fns InterceptorFns
35+
fns Fns
3736
}
3837

3938
var _ client.WithWatch = &interceptor{}
@@ -137,9 +136,9 @@ type subResourceInterceptor struct {
137136

138137
var _ client.SubResourceClient = &subResourceInterceptor{}
139138

140-
// SubResourceInterceptor returns a SubResourceClient that intercepts calls to
139+
// NewSubResourceClient returns a SubResourceClient that intercepts calls to
141140
// the provided client with the provided functions.
142-
func SubResourceInterceptor(client client.SubResourceClient, fns SubResourceInterceptorFns) client.SubResourceClient {
141+
func NewSubResourceClient(client client.SubResourceClient, fns SubResourceInterceptorFns) client.SubResourceClient {
143142
return subResourceInterceptor{client: client, fns: fns}
144143
}
145144

pkg/client/fake/intercept_test.go pkg/client/interceptor/intercept_test.go

+48-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package fake
1+
package interceptor
22

33
import (
44
"context"
5+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
56

67
. "github.com/onsi/ginkgo/v2"
78
. "github.com/onsi/gomega"
@@ -10,12 +11,12 @@ import (
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112
)
1213

13-
var _ = Describe("Interceptor", func() {
14-
wrappedClient := NewClientBuilder().Build()
14+
var _ = Describe("NewClient", func() {
15+
wrappedClient := fake.NewClientBuilder().Build()
1516
ctx := context.Background()
1617
It("should call the provided Get function", func() {
1718
var called bool
18-
client := Interceptor(wrappedClient, InterceptorFns{
19+
client := NewClient(wrappedClient, Fns{
1920
Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
2021
called = true
2122
return nil
@@ -26,19 +27,19 @@ var _ = Describe("Interceptor", func() {
2627
})
2728
It("should call the underlying client if the provided Get function is nil", func() {
2829
var called bool
29-
client1 := Interceptor(wrappedClient, InterceptorFns{
30+
client1 := NewClient(wrappedClient, Fns{
3031
Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
3132
called = true
3233
return nil
3334
},
3435
})
35-
client2 := Interceptor(client1, InterceptorFns{})
36+
client2 := NewClient(client1, Fns{})
3637
_ = client2.Get(ctx, types.NamespacedName{}, nil)
3738
Expect(called).To(BeTrue())
3839
})
3940
It("should call the provided List function", func() {
4041
var called bool
41-
client := Interceptor(wrappedClient, InterceptorFns{
42+
client := NewClient(wrappedClient, Fns{
4243
List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
4344
called = true
4445
return nil
@@ -49,19 +50,19 @@ var _ = Describe("Interceptor", func() {
4950
})
5051
It("should call the underlying client if the provided List function is nil", func() {
5152
var called bool
52-
client1 := Interceptor(wrappedClient, InterceptorFns{
53+
client1 := NewClient(wrappedClient, Fns{
5354
List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
5455
called = true
5556
return nil
5657
},
5758
})
58-
client2 := Interceptor(client1, InterceptorFns{})
59+
client2 := NewClient(client1, Fns{})
5960
_ = client2.List(ctx, nil)
6061
Expect(called).To(BeTrue())
6162
})
6263
It("should call the provided Create function", func() {
6364
var called bool
64-
client := Interceptor(wrappedClient, InterceptorFns{
65+
client := NewClient(wrappedClient, Fns{
6566
Create: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
6667
called = true
6768
return nil
@@ -72,19 +73,19 @@ var _ = Describe("Interceptor", func() {
7273
})
7374
It("should call the underlying client if the provided Create function is nil", func() {
7475
var called bool
75-
client1 := Interceptor(wrappedClient, InterceptorFns{
76+
client1 := NewClient(wrappedClient, Fns{
7677
Create: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
7778
called = true
7879
return nil
7980
},
8081
})
81-
client2 := Interceptor(client1, InterceptorFns{})
82+
client2 := NewClient(client1, Fns{})
8283
_ = client2.Create(ctx, nil)
8384
Expect(called).To(BeTrue())
8485
})
8586
It("should call the provided Delete function", func() {
8687
var called bool
87-
client := Interceptor(wrappedClient, InterceptorFns{
88+
client := NewClient(wrappedClient, Fns{
8889
Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
8990
called = true
9091
return nil
@@ -95,19 +96,19 @@ var _ = Describe("Interceptor", func() {
9596
})
9697
It("should call the underlying client if the provided Delete function is nil", func() {
9798
var called bool
98-
client1 := Interceptor(wrappedClient, InterceptorFns{
99+
client1 := NewClient(wrappedClient, Fns{
99100
Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
100101
called = true
101102
return nil
102103
},
103104
})
104-
client2 := Interceptor(client1, InterceptorFns{})
105+
client2 := NewClient(client1, Fns{})
105106
_ = client2.Delete(ctx, nil)
106107
Expect(called).To(BeTrue())
107108
})
108109
It("should call the provided DeleteAllOf function", func() {
109110
var called bool
110-
client := Interceptor(wrappedClient, InterceptorFns{
111+
client := NewClient(wrappedClient, Fns{
111112
DeleteAllOf: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error {
112113
called = true
113114
return nil
@@ -118,19 +119,19 @@ var _ = Describe("Interceptor", func() {
118119
})
119120
It("should call the underlying client if the provided DeleteAllOf function is nil", func() {
120121
var called bool
121-
client1 := Interceptor(wrappedClient, InterceptorFns{
122+
client1 := NewClient(wrappedClient, Fns{
122123
DeleteAllOf: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error {
123124
called = true
124125
return nil
125126
},
126127
})
127-
client2 := Interceptor(client1, InterceptorFns{})
128+
client2 := NewClient(client1, Fns{})
128129
_ = client2.DeleteAllOf(ctx, nil)
129130
Expect(called).To(BeTrue())
130131
})
131132
It("should call the provided Update function", func() {
132133
var called bool
133-
client := Interceptor(wrappedClient, InterceptorFns{
134+
client := NewClient(wrappedClient, Fns{
134135
Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
135136
called = true
136137
return nil
@@ -141,19 +142,19 @@ var _ = Describe("Interceptor", func() {
141142
})
142143
It("should call the underlying client if the provided Update function is nil", func() {
143144
var called bool
144-
client1 := Interceptor(wrappedClient, InterceptorFns{
145+
client1 := NewClient(wrappedClient, Fns{
145146
Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
146147
called = true
147148
return nil
148149
},
149150
})
150-
client2 := Interceptor(client1, InterceptorFns{})
151+
client2 := NewClient(client1, Fns{})
151152
_ = client2.Update(ctx, nil)
152153
Expect(called).To(BeTrue())
153154
})
154155
It("should call the provided Patch function", func() {
155156
var called bool
156-
client := Interceptor(wrappedClient, InterceptorFns{
157+
client := NewClient(wrappedClient, Fns{
157158
Patch: func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
158159
called = true
159160
return nil
@@ -164,19 +165,19 @@ var _ = Describe("Interceptor", func() {
164165
})
165166
It("should call the underlying client if the provided Patch function is nil", func() {
166167
var called bool
167-
client1 := Interceptor(wrappedClient, InterceptorFns{
168+
client1 := NewClient(wrappedClient, Fns{
168169
Patch: func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
169170
called = true
170171
return nil
171172
},
172173
})
173-
client2 := Interceptor(client1, InterceptorFns{})
174+
client2 := NewClient(client1, Fns{})
174175
_ = client2.Patch(ctx, nil, nil)
175176
Expect(called).To(BeTrue())
176177
})
177178
It("should call the provided Watch function", func() {
178179
var called bool
179-
client := Interceptor(wrappedClient, InterceptorFns{
180+
client := NewClient(wrappedClient, Fns{
180181
Watch: func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
181182
called = true
182183
return nil, nil
@@ -187,19 +188,19 @@ var _ = Describe("Interceptor", func() {
187188
})
188189
It("should call the underlying client if the provided Watch function is nil", func() {
189190
var called bool
190-
client1 := Interceptor(wrappedClient, InterceptorFns{
191+
client1 := NewClient(wrappedClient, Fns{
191192
Watch: func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
192193
called = true
193194
return nil, nil
194195
},
195196
})
196-
client2 := Interceptor(client1, InterceptorFns{})
197+
client2 := NewClient(client1, Fns{})
197198
_, _ = client2.Watch(ctx, nil)
198199
Expect(called).To(BeTrue())
199200
})
200201
It("should call the provided SubResource function", func() {
201202
var called bool
202-
client := Interceptor(wrappedClient, InterceptorFns{
203+
client := NewClient(wrappedClient, Fns{
203204
SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
204205
called = true
205206
return nil
@@ -210,19 +211,19 @@ var _ = Describe("Interceptor", func() {
210211
})
211212
It("should call the underlying client if the provided SubResource function is nil", func() {
212213
var called bool
213-
client1 := Interceptor(wrappedClient, InterceptorFns{
214+
client1 := NewClient(wrappedClient, Fns{
214215
SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
215216
called = true
216217
return nil
217218
},
218219
})
219-
client2 := Interceptor(client1, InterceptorFns{})
220+
client2 := NewClient(client1, Fns{})
220221
_ = client2.SubResource("")
221222
Expect(called).To(BeTrue())
222223
})
223224
It("should call the provided SubResource function with 'status' when calling Status()", func() {
224225
var called bool
225-
client := Interceptor(wrappedClient, InterceptorFns{
226+
client := NewClient(wrappedClient, Fns{
226227
SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
227228
if subResource == "status" {
228229
called = true
@@ -235,27 +236,27 @@ var _ = Describe("Interceptor", func() {
235236
})
236237
It("should call the underlying client if the provided SubResource function is nil when calling Status", func() {
237238
var called bool
238-
client1 := Interceptor(wrappedClient, InterceptorFns{
239+
client1 := NewClient(wrappedClient, Fns{
239240
SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
240241
if subResource == "status" {
241242
called = true
242243
}
243244
return nil
244245
},
245246
})
246-
client2 := Interceptor(client1, InterceptorFns{})
247+
client2 := NewClient(client1, Fns{})
247248
_ = client2.Status()
248249
Expect(called).To(BeTrue())
249250
})
250251
})
251252

252-
var _ = Describe("SubResourceInterceptor", func() {
253-
wrappedClient := NewClientBuilder().Build()
253+
var _ = Describe("NewSubResourceClient", func() {
254+
wrappedClient := fake.NewClientBuilder().Build()
254255
srClient := wrappedClient.SubResource("test")
255256
ctx := context.Background()
256257
It("should call the provided Get function", func() {
257258
var called bool
258-
client := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
259+
client := NewSubResourceClient(srClient, SubResourceInterceptorFns{
259260
Get: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
260261
called = true
261262
return nil
@@ -266,19 +267,19 @@ var _ = Describe("SubResourceInterceptor", func() {
266267
})
267268
It("should call the underlying client if the provided Get function is nil", func() {
268269
var called bool
269-
client1 := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
270+
client1 := NewSubResourceClient(srClient, SubResourceInterceptorFns{
270271
Get: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
271272
called = true
272273
return nil
273274
},
274275
})
275-
client2 := SubResourceInterceptor(client1, SubResourceInterceptorFns{})
276+
client2 := NewSubResourceClient(client1, SubResourceInterceptorFns{})
276277
_ = client2.Get(ctx, nil, nil)
277278
Expect(called).To(BeTrue())
278279
})
279280
It("should call the provided Update function", func() {
280281
var called bool
281-
client := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
282+
client := NewSubResourceClient(srClient, SubResourceInterceptorFns{
282283
Update: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
283284
called = true
284285
return nil
@@ -289,19 +290,19 @@ var _ = Describe("SubResourceInterceptor", func() {
289290
})
290291
It("should call the underlying client if the provided Update function is nil", func() {
291292
var called bool
292-
client1 := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
293+
client1 := NewSubResourceClient(srClient, SubResourceInterceptorFns{
293294
Update: func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error {
294295
called = true
295296
return nil
296297
},
297298
})
298-
client2 := SubResourceInterceptor(client1, SubResourceInterceptorFns{})
299+
client2 := NewSubResourceClient(client1, SubResourceInterceptorFns{})
299300
_ = client2.Update(ctx, nil, nil)
300301
Expect(called).To(BeTrue())
301302
})
302303
It("should call the provided Patch function", func() {
303304
var called bool
304-
client := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
305+
client := NewSubResourceClient(srClient, SubResourceInterceptorFns{
305306
Patch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
306307
called = true
307308
return nil
@@ -312,19 +313,19 @@ var _ = Describe("SubResourceInterceptor", func() {
312313
})
313314
It("should call the underlying client if the provided Patch function is nil", func() {
314315
var called bool
315-
client1 := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
316+
client1 := NewSubResourceClient(srClient, SubResourceInterceptorFns{
316317
Patch: func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
317318
called = true
318319
return nil
319320
},
320321
})
321-
client2 := SubResourceInterceptor(client1, SubResourceInterceptorFns{})
322+
client2 := NewSubResourceClient(client1, SubResourceInterceptorFns{})
322323
_ = client2.Patch(ctx, nil, nil)
323324
Expect(called).To(BeTrue())
324325
})
325326
It("should call the provided Create function", func() {
326327
var called bool
327-
client := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
328+
client := NewSubResourceClient(srClient, SubResourceInterceptorFns{
328329
Create: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
329330
called = true
330331
return nil
@@ -335,13 +336,13 @@ var _ = Describe("SubResourceInterceptor", func() {
335336
})
336337
It("should call the underlying client if the provided Create function is nil", func() {
337338
var called bool
338-
client1 := SubResourceInterceptor(srClient, SubResourceInterceptorFns{
339+
client1 := NewSubResourceClient(srClient, SubResourceInterceptorFns{
339340
Create: func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
340341
called = true
341342
return nil
342343
},
343344
})
344-
client2 := SubResourceInterceptor(client1, SubResourceInterceptorFns{})
345+
client2 := NewSubResourceClient(client1, SubResourceInterceptorFns{})
345346
_ = client2.Create(ctx, nil, nil)
346347
Expect(called).To(BeTrue())
347348
})

0 commit comments

Comments
 (0)