Skip to content

Commit fe27862

Browse files
committed
Added methods to intercept calls to a fake k8s client
1 parent 3b0376c commit fe27862

File tree

2 files changed

+514
-0
lines changed

2 files changed

+514
-0
lines changed

pkg/client/fake/intercept.go

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package fake
2+
3+
import (
4+
"context"
5+
"k8s.io/apimachinery/pkg/api/meta"
6+
"k8s.io/apimachinery/pkg/runtime"
7+
"k8s.io/apimachinery/pkg/runtime/schema"
8+
"k8s.io/apimachinery/pkg/watch"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
// Interceptor is a client that intercepts calls to the underlying client and
13+
// calls the provided functions instead. If the function is nil, the call is
14+
// forwarded to the underlying client.
15+
func Interceptor(client client.WithWatch, fns InterceptorFns) client.WithWatch {
16+
return interceptor{client: client, fns: fns}
17+
}
18+
19+
// InterceptorFns contains functions that are called instead of the underlying
20+
// client's methods.
21+
type InterceptorFns struct {
22+
Get func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
23+
List func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
24+
Create func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
25+
Delete func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
26+
DeleteAllOf func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
27+
Update func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
28+
Patch func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
29+
Watch func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
30+
SubResource func(client client.WithWatch, subResource string) client.SubResourceClient
31+
Status func(client client.WithWatch) client.StatusWriter
32+
}
33+
34+
type interceptor struct {
35+
client client.WithWatch
36+
fns InterceptorFns
37+
}
38+
39+
var _ client.WithWatch = &interceptor{}
40+
41+
func (c interceptor) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
42+
return c.client.GroupVersionKindFor(obj)
43+
}
44+
45+
func (c interceptor) IsObjectNamespaced(obj runtime.Object) (bool, error) {
46+
return c.client.IsObjectNamespaced(obj)
47+
}
48+
49+
func (c interceptor) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
50+
if c.fns.Get != nil {
51+
return c.fns.Get(ctx, c.client, key, obj, opts...)
52+
}
53+
return c.client.Get(ctx, key, obj, opts...)
54+
}
55+
56+
func (c interceptor) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
57+
if c.fns.List != nil {
58+
return c.fns.List(ctx, c.client, list, opts...)
59+
}
60+
return c.client.List(ctx, list, opts...)
61+
}
62+
63+
func (c interceptor) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
64+
if c.fns.Create != nil {
65+
return c.fns.Create(ctx, c.client, obj, opts...)
66+
}
67+
return c.client.Create(ctx, obj, opts...)
68+
}
69+
70+
func (c interceptor) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
71+
if c.fns.Delete != nil {
72+
return c.fns.Delete(ctx, c.client, obj, opts...)
73+
}
74+
return c.client.Delete(ctx, obj, opts...)
75+
}
76+
77+
func (c interceptor) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
78+
if c.fns.Update != nil {
79+
return c.fns.Update(ctx, c.client, obj, opts...)
80+
}
81+
return c.client.Update(ctx, obj, opts...)
82+
}
83+
84+
func (c interceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
85+
if c.fns.Patch != nil {
86+
return c.fns.Patch(ctx, c.client, obj, patch, opts...)
87+
}
88+
return c.client.Patch(ctx, obj, patch, opts...)
89+
}
90+
91+
func (c interceptor) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
92+
if c.fns.DeleteAllOf != nil {
93+
return c.fns.DeleteAllOf(ctx, c.client, obj, opts...)
94+
}
95+
return c.client.DeleteAllOf(ctx, obj, opts...)
96+
}
97+
98+
func (c interceptor) Status() client.SubResourceWriter {
99+
if c.fns.Status != nil {
100+
return c.fns.Status(c.client)
101+
}
102+
return c.client.Status()
103+
}
104+
105+
func (c interceptor) SubResource(subResource string) client.SubResourceClient {
106+
if c.fns.SubResource != nil {
107+
return c.fns.SubResource(c.client, subResource)
108+
}
109+
return c.client.SubResource(subResource)
110+
}
111+
112+
func (c interceptor) Scheme() *runtime.Scheme {
113+
return c.client.Scheme()
114+
}
115+
116+
func (c interceptor) RESTMapper() meta.RESTMapper {
117+
return c.client.RESTMapper()
118+
}
119+
120+
func (c interceptor) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
121+
if c.fns.Watch != nil {
122+
return c.fns.Watch(ctx, c.client, obj, opts...)
123+
}
124+
return c.client.Watch(ctx, obj, opts...)
125+
}
126+
127+
type SubResourceInterceptorFns struct {
128+
Get func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
129+
Create func(ctx context.Context, client client.SubResourceClient, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
130+
Update func(ctx context.Context, client client.SubResourceClient, obj client.Object, opts ...client.SubResourceUpdateOption) error
131+
Patch func(ctx context.Context, client client.SubResourceClient, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
132+
}
133+
134+
type subResourceInterceptor struct {
135+
client client.SubResourceClient
136+
fns SubResourceInterceptorFns
137+
}
138+
139+
var _ client.SubResourceClient = &subResourceInterceptor{}
140+
141+
func SubResourceInterceptor(client client.SubResourceClient, fns SubResourceInterceptorFns) client.SubResourceClient {
142+
return subResourceInterceptor{client: client, fns: fns}
143+
}
144+
145+
func (s subResourceInterceptor) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
146+
if s.fns.Get != nil {
147+
return s.fns.Get(ctx, s.client, obj, subResource, opts...)
148+
}
149+
return s.client.Get(ctx, obj, subResource, opts...)
150+
}
151+
152+
func (s subResourceInterceptor) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
153+
if s.fns.Create != nil {
154+
return s.fns.Create(ctx, s.client, obj, subResource, opts...)
155+
}
156+
return s.client.Create(ctx, obj, subResource, opts...)
157+
}
158+
159+
func (s subResourceInterceptor) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
160+
if s.fns.Update != nil {
161+
return s.fns.Update(ctx, s.client, obj, opts...)
162+
}
163+
return s.client.Update(ctx, obj, opts...)
164+
}
165+
166+
func (s subResourceInterceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
167+
if s.fns.Patch != nil {
168+
return s.fns.Patch(ctx, s.client, obj, patch, opts...)
169+
}
170+
return s.client.Patch(ctx, obj, patch, opts...)
171+
}

0 commit comments

Comments
 (0)