|
| 1 | +/* |
| 2 | + Copyright 2021 - 2022 Crunchy Data Solutions, Inc. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package runtime |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | +) |
| 23 | + |
| 24 | +// Types that implement single methods of the [client.Reader] interface. |
| 25 | +type ( |
| 26 | + // NOTE: The signature of [client.Client.Get] changes in [sigs.k8s.io/controller-runtime@v0.13.0]. |
| 27 | + // - https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.13.0 |
| 28 | + |
| 29 | + ClientGet func(context.Context, client.ObjectKey, client.Object) error |
| 30 | + ClientList func(context.Context, client.ObjectList, ...client.ListOption) error |
| 31 | +) |
| 32 | + |
| 33 | +// ClientReader implements [client.Reader] by composing assignable functions. |
| 34 | +type ClientReader struct { |
| 35 | + ClientGet |
| 36 | + ClientList |
| 37 | +} |
| 38 | + |
| 39 | +var _ client.Reader = ClientReader{} |
| 40 | + |
| 41 | +// Types that implement single methods of the [client.Writer] interface. |
| 42 | +type ( |
| 43 | + ClientCreate func(context.Context, client.Object, ...client.CreateOption) error |
| 44 | + ClientDelete func(context.Context, client.Object, ...client.DeleteOption) error |
| 45 | + ClientPatch func(context.Context, client.Object, client.Patch, ...client.PatchOption) error |
| 46 | + ClientDeleteAll func(context.Context, client.Object, ...client.DeleteAllOfOption) error |
| 47 | + ClientUpdate func(context.Context, client.Object, ...client.UpdateOption) error |
| 48 | +) |
| 49 | + |
| 50 | +// ClientWriter implements [client.Writer] by composing assignable functions. |
| 51 | +type ClientWriter struct { |
| 52 | + ClientCreate |
| 53 | + ClientDelete |
| 54 | + ClientDeleteAll |
| 55 | + ClientPatch |
| 56 | + ClientUpdate |
| 57 | +} |
| 58 | + |
| 59 | +var _ client.Writer = ClientWriter{} |
| 60 | + |
| 61 | +// NOTE: The following implementations can go away following https://go.dev/issue/47487. |
| 62 | +// The function types above would become single-method interfaces. |
| 63 | + |
| 64 | +func (fn ClientCreate) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { |
| 65 | + return fn(ctx, obj, opts...) |
| 66 | +} |
| 67 | + |
| 68 | +func (fn ClientDelete) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { |
| 69 | + return fn(ctx, obj, opts...) |
| 70 | +} |
| 71 | + |
| 72 | +func (fn ClientDeleteAll) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { |
| 73 | + return fn(ctx, obj, opts...) |
| 74 | +} |
| 75 | + |
| 76 | +func (fn ClientGet) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error { |
| 77 | + return fn(ctx, key, obj) |
| 78 | +} |
| 79 | + |
| 80 | +func (fn ClientList) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 81 | + return fn(ctx, list, opts...) |
| 82 | +} |
| 83 | + |
| 84 | +func (fn ClientPatch) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { |
| 85 | + return fn(ctx, obj, patch, opts...) |
| 86 | +} |
| 87 | + |
| 88 | +func (fn ClientUpdate) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { |
| 89 | + return fn(ctx, obj, opts...) |
| 90 | +} |
0 commit comments