@@ -28,6 +28,8 @@ import (
2828 "strings"
2929 "sync"
3030
31+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
32+
3133 apierrors "k8s.io/apimachinery/pkg/api/errors"
3234 "k8s.io/apimachinery/pkg/api/meta"
3335 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -107,6 +109,7 @@ type ClientBuilder struct {
107109 initRuntimeObjects []runtime.Object
108110 withStatusSubresource []client.Object
109111 objectTracker testing.ObjectTracker
112+ interceptorFns * interceptor.Fns
110113
111114 // indexes maps each GroupVersionKind (GVK) to the indexes registered for that GVK.
112115 // The inner map maps from index name to IndexerFunc.
@@ -198,6 +201,12 @@ func (f *ClientBuilder) WithStatusSubresource(o ...client.Object) *ClientBuilder
198201 return f
199202}
200203
204+ // WithInterceptorFns configures the client methods to be intercepted using the provided interceptor.Fns.
205+ func (f * ClientBuilder ) WithInterceptorFns (interceptorFns interceptor.Fns ) * ClientBuilder {
206+ f .interceptorFns = & interceptorFns
207+ return f
208+ }
209+
201210// Build builds and returns a new fake client.
202211func (f * ClientBuilder ) Build () client.WithWatch {
203212 if f .scheme == nil {
@@ -240,13 +249,19 @@ func (f *ClientBuilder) Build() client.WithWatch {
240249 }
241250 }
242251
243- return & fakeClient {
252+ var result client. WithWatch = & fakeClient {
244253 tracker : tracker ,
245254 scheme : f .scheme ,
246255 restMapper : f .restMapper ,
247256 indexes : f .indexes ,
248257 withStatusSubresource : withStatusSubResource ,
249258 }
259+
260+ if f .interceptorFns != nil {
261+ result = interceptor .NewClient (result , * f .interceptorFns )
262+ }
263+
264+ return result
250265}
251266
252267const trackerAddResourceVersion = "999"
0 commit comments