github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/controller-runtime/client/fake_ssa.go (about) 1 package client 2 3 import ( 4 "context" 5 6 "k8s.io/apimachinery/pkg/runtime" 7 "k8s.io/apimachinery/pkg/types" 8 k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client" 9 fakecontrollerclient "sigs.k8s.io/controller-runtime/pkg/client/fake" 10 ) 11 12 // FakeApplier provides a wrapper around the fake k8s controller client to convert the unsupported apply-type patches to merge patches. 13 func NewFakeApplier(scheme *runtime.Scheme, owner string, objs ...runtime.Object) *ServerSideApplier { 14 return &ServerSideApplier{ 15 client: &fakeApplier{fakecontrollerclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()}, 16 Scheme: scheme, 17 Owner: k8scontrollerclient.FieldOwner(owner), 18 } 19 } 20 21 type fakeApplier struct { 22 k8scontrollerclient.Client 23 } 24 25 func (c *fakeApplier) Patch(ctx context.Context, obj k8scontrollerclient.Object, patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.PatchOption) error { 26 patch, opts = convertApplyToMergePatch(patch, opts...) 27 return c.Client.Patch(ctx, obj, patch, opts...) 28 } 29 30 func (c *fakeApplier) Status() k8scontrollerclient.StatusWriter { 31 return fakeStatusWriter{c.Client.Status()} 32 } 33 34 type fakeStatusWriter struct { 35 k8scontrollerclient.StatusWriter 36 } 37 38 func (c fakeStatusWriter) Patch(ctx context.Context, obj k8scontrollerclient.Object, patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.SubResourcePatchOption) error { 39 return c.StatusWriter.Patch(ctx, obj, patch, opts...) 40 } 41 42 func convertApplyToMergePatch(patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.PatchOption) (k8scontrollerclient.Patch, []k8scontrollerclient.PatchOption) { 43 // Apply patch type is not supported on the fake controller 44 if patch.Type() == types.ApplyPatchType { 45 patch = k8scontrollerclient.Merge 46 patchOptions := make([]k8scontrollerclient.PatchOption, 0, len(opts)) 47 for _, opt := range opts { 48 if opt == k8scontrollerclient.ForceOwnership { 49 continue 50 } 51 patchOptions = append(patchOptions, opt) 52 } 53 opts = patchOptions 54 } 55 return patch, opts 56 }