|
| 1 | +package interceptor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/api/meta" |
| 7 | + "k8s.io/apimachinery/pkg/runtime" |
| 8 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 9 | + "k8s.io/apimachinery/pkg/watch" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | +) |
| 12 | + |
| 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 | +) |
| 36 | + |
| 37 | +// NewClient returns a new interceptor client that calls the functions in funcs instead of the underlying client's methods, if they are not nil. |
| 38 | +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} |
| 45 | +} |
| 46 | + |
| 47 | +type interceptor struct { |
| 48 | + client client.WithWatch |
| 49 | + funcs Funcs |
| 50 | +} |
| 51 | + |
| 52 | +var _ client.WithWatch = &interceptor{} |
| 53 | + |
| 54 | +func (c interceptor) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) { |
| 55 | + return c.client.GroupVersionKindFor(obj) |
| 56 | +} |
| 57 | + |
| 58 | +func (c interceptor) IsObjectNamespaced(obj runtime.Object) (bool, error) { |
| 59 | + return c.client.IsObjectNamespaced(obj) |
| 60 | +} |
| 61 | + |
| 62 | +func (c interceptor) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 63 | + if c.funcs.Get != nil { |
| 64 | + return c.funcs.Get(ctx, c.client, key, obj, opts...) |
| 65 | + } |
| 66 | + return c.client.Get(ctx, key, obj, opts...) |
| 67 | +} |
| 68 | + |
| 69 | +func (c interceptor) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 70 | + if c.funcs.List != nil { |
| 71 | + return c.funcs.List(ctx, c.client, list, opts...) |
| 72 | + } |
| 73 | + return c.client.List(ctx, list, opts...) |
| 74 | +} |
| 75 | + |
| 76 | +func (c interceptor) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { |
| 77 | + if c.funcs.Create != nil { |
| 78 | + return c.funcs.Create(ctx, c.client, obj, opts...) |
| 79 | + } |
| 80 | + return c.client.Create(ctx, obj, opts...) |
| 81 | +} |
| 82 | + |
| 83 | +func (c interceptor) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { |
| 84 | + if c.funcs.Delete != nil { |
| 85 | + return c.funcs.Delete(ctx, c.client, obj, opts...) |
| 86 | + } |
| 87 | + return c.client.Delete(ctx, obj, opts...) |
| 88 | +} |
| 89 | + |
| 90 | +func (c interceptor) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { |
| 91 | + if c.funcs.Update != nil { |
| 92 | + return c.funcs.Update(ctx, c.client, obj, opts...) |
| 93 | + } |
| 94 | + return c.client.Update(ctx, obj, opts...) |
| 95 | +} |
| 96 | + |
| 97 | +func (c interceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { |
| 98 | + if c.funcs.Patch != nil { |
| 99 | + return c.funcs.Patch(ctx, c.client, obj, patch, opts...) |
| 100 | + } |
| 101 | + return c.client.Patch(ctx, obj, patch, opts...) |
| 102 | +} |
| 103 | + |
| 104 | +func (c interceptor) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { |
| 105 | + if c.funcs.DeleteAllOf != nil { |
| 106 | + return c.funcs.DeleteAllOf(ctx, c.client, obj, opts...) |
| 107 | + } |
| 108 | + return c.client.DeleteAllOf(ctx, obj, opts...) |
| 109 | +} |
| 110 | + |
| 111 | +func (c interceptor) Status() client.SubResourceWriter { |
| 112 | + return c.SubResource("status") |
| 113 | +} |
| 114 | + |
| 115 | +func (c interceptor) SubResource(subResource string) client.SubResourceClient { |
| 116 | + if c.funcs.SubResource != nil { |
| 117 | + return c.funcs.SubResource(c.client, subResource) |
| 118 | + } |
| 119 | + return c.client.SubResource(subResource) |
| 120 | +} |
| 121 | + |
| 122 | +func (c interceptor) Scheme() *runtime.Scheme { |
| 123 | + return c.client.Scheme() |
| 124 | +} |
| 125 | + |
| 126 | +func (c interceptor) RESTMapper() meta.RESTMapper { |
| 127 | + return c.client.RESTMapper() |
| 128 | +} |
| 129 | + |
| 130 | +func (c interceptor) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) { |
| 131 | + if c.funcs.Watch != nil { |
| 132 | + return c.funcs.Watch(ctx, c.client, obj, opts...) |
| 133 | + } |
| 134 | + return c.client.Watch(ctx, obj, opts...) |
| 135 | +} |
| 136 | + |
| 137 | +type subResourceInterceptor struct { |
| 138 | + client client.SubResourceClient |
| 139 | + funcs SubResourceFuncs |
| 140 | +} |
| 141 | + |
| 142 | +var _ client.SubResourceClient = &subResourceInterceptor{} |
| 143 | + |
| 144 | +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...) |
| 147 | + } |
| 148 | + return s.client.Get(ctx, obj, subResource, opts...) |
| 149 | +} |
| 150 | + |
| 151 | +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...) |
| 154 | + } |
| 155 | + return s.client.Create(ctx, obj, subResource, opts...) |
| 156 | +} |
| 157 | + |
| 158 | +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...) |
| 161 | + } |
| 162 | + return s.client.Update(ctx, obj, opts...) |
| 163 | +} |
| 164 | + |
| 165 | +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...) |
| 168 | + } |
| 169 | + return s.client.Patch(ctx, obj, patch, opts...) |
| 170 | +} |
0 commit comments