Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ludydoo committed Apr 25, 2023
1 parent e358be3 commit f264518
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 129 deletions.
14 changes: 7 additions & 7 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type ClientBuilder struct {
initRuntimeObjects []runtime.Object
withStatusSubresource []client.Object
objectTracker testing.ObjectTracker
interceptorFns *interceptor.Fns
interceptorFuncs *interceptor.Funcs

// indexes maps each GroupVersionKind (GVK) to the indexes registered for that GVK.
// The inner map maps from index name to IndexerFunc.
Expand Down Expand Up @@ -195,15 +195,15 @@ func (f *ClientBuilder) WithIndex(obj runtime.Object, field string, extractValue
}

// WithStatusSubresource configures the passed object with a status subresource, which means
// calls to Update and Patch will not alters its status.
// calls to Update and Patch will not alter its status.
func (f *ClientBuilder) WithStatusSubresource(o ...client.Object) *ClientBuilder {
f.withStatusSubresource = append(f.withStatusSubresource, o...)
return f
}

// WithInterceptorFns configures the client methods to be intercepted using the provided interceptor.Fns.
func (f *ClientBuilder) WithInterceptorFns(interceptorFns interceptor.Fns) *ClientBuilder {
f.interceptorFns = &interceptorFns
// WithInterceptorFuncs configures the client methods to be intercepted using the provided interceptor.Funcs.
func (f *ClientBuilder) WithInterceptorFuncs(interceptorFuncs interceptor.Funcs) *ClientBuilder {
f.interceptorFuncs = &interceptorFuncs
return f
}

Expand Down Expand Up @@ -257,8 +257,8 @@ func (f *ClientBuilder) Build() client.WithWatch {
withStatusSubresource: withStatusSubResource,
}

if f.interceptorFns != nil {
result = interceptor.NewClient(result, *f.interceptorFns)
if f.interceptorFuncs != nil {
result = interceptor.NewClient(result, *f.interceptorFuncs)
}

return result
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,9 +1452,9 @@ var _ = Describe("Fake client builder", func() {
}).To(Panic())
})

It("should wrap the fake client with an interceptor when WithInterceptorFns is called", func() {
It("should wrap the fake client with an interceptor when WithInterceptorFuncs is called", func() {
var called bool
cli := NewClientBuilder().WithInterceptorFns(interceptor.Fns{
cli := NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{
Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
called = true
return nil
Expand Down
129 changes: 51 additions & 78 deletions pkg/client/interceptor/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,42 @@ import (
)

type (
// GetFn is a function that can be used to intercept GET calls.
GetFn func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
// ListFn is a function that can be used to intercept LIST calls.
ListFn func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
// CreateFn is a function that can be used to intercept CREATE calls.
CreateFn func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
// DeleteFn is a function that can be used to intercept DELETE calls.
DeleteFn func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
// DeleteAllOfFn is a function that can be used to intercept Collection DELETE calls.
DeleteAllOfFn func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
// UpdateFn is a function that can be used to intercept UPDATE calls.
UpdateFn func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
// PatchFn is a function that can be used to intercept PATCH calls.
PatchFn func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
// WatchFn is a function that can be used to intercept WATCH calls.
WatchFn func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
// SubResourceFn is a function that can be used to intercept SubResource calls.
SubResourceFn func(client client.WithWatch, subResource string) client.SubResourceClient

// SubResourceGetFn is a function that can be used to intercept SubResource GET calls.
SubResourceGetFn func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
// SubResourceCreateFn is a function that can be used to intercept SubResource CREATE calls.
SubResourceCreateFn func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
// SubResourceUpdateFn is a function that can be used to intercept SubResource UPDATE calls.
SubResourceUpdateFn func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error
// SubResourcePatchFn is a function that can be used to intercept SubResource PATCH calls.
SubResourcePatchFn func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error

// Fns contains functions that are called instead of the underlying client's methods.
Fns struct {
Get GetFn
List ListFn
Create CreateFn
Delete DeleteFn
DeleteAllOf DeleteAllOfFn
Update UpdateFn
Patch PatchFn
Watch WatchFn
SubResource SubResourceFn

// Funcs contains functions that are called instead of the underlying client's methods.
Funcs struct {
Get func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
List func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
Create func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
Delete func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
DeleteAllOf func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
Update func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
Patch func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
Watch func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
SubResource func(client client.WithWatch, subResource string) client.SubResourceClient
}

// SubResourceFns is a set of functions that can be used to intercept calls to a SubResourceClient.
SubResourceFns struct {
Get SubResourceGetFn
Create SubResourceCreateFn
Update SubResourceUpdateFn
Patch SubResourcePatchFn
// SubResourceFuncs is a set of functions that can be used to intercept calls to a SubResourceClient.
SubResourceFuncs struct {
Get func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
Create func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
Update func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error
Patch func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
}
)

// NewClient returns a new interceptor client that calls the functions in fns instead of the underlying client's methods, if they are not nil.
func NewClient(interceptedClient client.WithWatch, fns Fns) client.WithWatch {
return interceptor{client: interceptedClient, fns: fns}
// NewClient returns a new interceptor client that calls the functions in funcs instead of the underlying client's methods, if they are not nil.
func NewClient(interceptedClient client.WithWatch, funcs Funcs) client.WithWatch {
return interceptor{client: interceptedClient, funcs: funcs}
}

// NewSubResourceClient returns a SubResourceClient that intercepts calls to the provided client with the provided functions.
func NewSubResourceClient(interceptedClient client.SubResourceClient, fns SubResourceFns) client.SubResourceClient {
return subResourceInterceptor{client: interceptedClient, fns: fns}
func NewSubResourceClient(interceptedClient client.SubResourceClient, funcs SubResourceFuncs) client.SubResourceClient {
return subResourceInterceptor{client: interceptedClient, funcs: funcs}
}

type interceptor struct {
client client.WithWatch
fns Fns
funcs Funcs
}

var _ client.WithWatch = &interceptor{}
Expand All @@ -87,50 +60,50 @@ func (c interceptor) IsObjectNamespaced(obj runtime.Object) (bool, error) {
}

func (c interceptor) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if c.fns.Get != nil {
return c.fns.Get(ctx, c.client, key, obj, opts...)
if c.funcs.Get != nil {
return c.funcs.Get(ctx, c.client, key, obj, opts...)
}
return c.client.Get(ctx, key, obj, opts...)
}

func (c interceptor) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if c.fns.List != nil {
return c.fns.List(ctx, c.client, list, opts...)
if c.funcs.List != nil {
return c.funcs.List(ctx, c.client, list, opts...)
}
return c.client.List(ctx, list, opts...)
}

func (c interceptor) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
if c.fns.Create != nil {
return c.fns.Create(ctx, c.client, obj, opts...)
if c.funcs.Create != nil {
return c.funcs.Create(ctx, c.client, obj, opts...)
}
return c.client.Create(ctx, obj, opts...)
}

func (c interceptor) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
if c.fns.Delete != nil {
return c.fns.Delete(ctx, c.client, obj, opts...)
if c.funcs.Delete != nil {
return c.funcs.Delete(ctx, c.client, obj, opts...)
}
return c.client.Delete(ctx, obj, opts...)
}

func (c interceptor) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
if c.fns.Update != nil {
return c.fns.Update(ctx, c.client, obj, opts...)
if c.funcs.Update != nil {
return c.funcs.Update(ctx, c.client, obj, opts...)
}
return c.client.Update(ctx, obj, opts...)
}

func (c interceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
if c.fns.Patch != nil {
return c.fns.Patch(ctx, c.client, obj, patch, opts...)
if c.funcs.Patch != nil {
return c.funcs.Patch(ctx, c.client, obj, patch, opts...)
}
return c.client.Patch(ctx, obj, patch, opts...)
}

func (c interceptor) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
if c.fns.DeleteAllOf != nil {
return c.fns.DeleteAllOf(ctx, c.client, obj, opts...)
if c.funcs.DeleteAllOf != nil {
return c.funcs.DeleteAllOf(ctx, c.client, obj, opts...)
}
return c.client.DeleteAllOf(ctx, obj, opts...)
}
Expand All @@ -140,8 +113,8 @@ func (c interceptor) Status() client.SubResourceWriter {
}

func (c interceptor) SubResource(subResource string) client.SubResourceClient {
if c.fns.SubResource != nil {
return c.fns.SubResource(c.client, subResource)
if c.funcs.SubResource != nil {
return c.funcs.SubResource(c.client, subResource)
}
return c.client.SubResource(subResource)
}
Expand All @@ -155,43 +128,43 @@ func (c interceptor) RESTMapper() meta.RESTMapper {
}

func (c interceptor) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
if c.fns.Watch != nil {
return c.fns.Watch(ctx, c.client, obj, opts...)
if c.funcs.Watch != nil {
return c.funcs.Watch(ctx, c.client, obj, opts...)
}
return c.client.Watch(ctx, obj, opts...)
}

type subResourceInterceptor struct {
client client.SubResourceClient
fns SubResourceFns
funcs SubResourceFuncs
}

var _ client.SubResourceClient = &subResourceInterceptor{}

func (s subResourceInterceptor) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
if s.fns.Get != nil {
return s.fns.Get(ctx, s.client, obj, subResource, opts...)
if s.funcs.Get != nil {
return s.funcs.Get(ctx, s.client, obj, subResource, opts...)
}
return s.client.Get(ctx, obj, subResource, opts...)
}

func (s subResourceInterceptor) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
if s.fns.Create != nil {
return s.fns.Create(ctx, s.client, obj, subResource, opts...)
if s.funcs.Create != nil {
return s.funcs.Create(ctx, s.client, obj, subResource, opts...)
}
return s.client.Create(ctx, obj, subResource, opts...)
}

func (s subResourceInterceptor) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
if s.fns.Update != nil {
return s.fns.Update(ctx, s.client, obj, opts...)
if s.funcs.Update != nil {
return s.funcs.Update(ctx, s.client, obj, opts...)
}
return s.client.Update(ctx, obj, opts...)
}

func (s subResourceInterceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
if s.fns.Patch != nil {
return s.fns.Patch(ctx, s.client, obj, patch, opts...)
if s.funcs.Patch != nil {
return s.funcs.Patch(ctx, s.client, obj, patch, opts...)
}
return s.client.Patch(ctx, obj, patch, opts...)
}
Loading

0 comments on commit f264518

Please sign in to comment.