sigs.k8s.io/controller-runtime@v0.18.2/pkg/client/fieldowner.go (about) 1 /* 2 Copyright 2024 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package client 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/api/meta" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 ) 26 27 // WithFieldOwner wraps a Client and adds the fieldOwner as the field 28 // manager to all write requests from this client. If additional [FieldOwner] 29 // options are specified on methods of this client, the value specified here 30 // will be overridden. 31 func WithFieldOwner(c Client, fieldOwner string) Client { 32 return &clientWithFieldManager{ 33 owner: fieldOwner, 34 c: c, 35 Reader: c, 36 } 37 } 38 39 type clientWithFieldManager struct { 40 owner string 41 c Client 42 Reader 43 } 44 45 func (f *clientWithFieldManager) Create(ctx context.Context, obj Object, opts ...CreateOption) error { 46 return f.c.Create(ctx, obj, append([]CreateOption{FieldOwner(f.owner)}, opts...)...) 47 } 48 49 func (f *clientWithFieldManager) Update(ctx context.Context, obj Object, opts ...UpdateOption) error { 50 return f.c.Update(ctx, obj, append([]UpdateOption{FieldOwner(f.owner)}, opts...)...) 51 } 52 53 func (f *clientWithFieldManager) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error { 54 return f.c.Patch(ctx, obj, patch, append([]PatchOption{FieldOwner(f.owner)}, opts...)...) 55 } 56 57 func (f *clientWithFieldManager) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error { 58 return f.c.Delete(ctx, obj, opts...) 59 } 60 61 func (f *clientWithFieldManager) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error { 62 return f.c.DeleteAllOf(ctx, obj, opts...) 63 } 64 65 func (f *clientWithFieldManager) Scheme() *runtime.Scheme { return f.c.Scheme() } 66 func (f *clientWithFieldManager) RESTMapper() meta.RESTMapper { return f.c.RESTMapper() } 67 func (f *clientWithFieldManager) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) { 68 return f.c.GroupVersionKindFor(obj) 69 } 70 func (f *clientWithFieldManager) IsObjectNamespaced(obj runtime.Object) (bool, error) { 71 return f.c.IsObjectNamespaced(obj) 72 } 73 74 func (f *clientWithFieldManager) Status() StatusWriter { 75 return &subresourceClientWithFieldOwner{ 76 owner: f.owner, 77 subresourceWriter: f.c.Status(), 78 } 79 } 80 81 func (f *clientWithFieldManager) SubResource(subresource string) SubResourceClient { 82 c := f.c.SubResource(subresource) 83 return &subresourceClientWithFieldOwner{ 84 owner: f.owner, 85 subresourceWriter: c, 86 SubResourceReader: c, 87 } 88 } 89 90 type subresourceClientWithFieldOwner struct { 91 owner string 92 subresourceWriter SubResourceWriter 93 SubResourceReader 94 } 95 96 func (f *subresourceClientWithFieldOwner) Create(ctx context.Context, obj Object, subresource Object, opts ...SubResourceCreateOption) error { 97 return f.subresourceWriter.Create(ctx, obj, subresource, append([]SubResourceCreateOption{FieldOwner(f.owner)}, opts...)...) 98 } 99 100 func (f *subresourceClientWithFieldOwner) Update(ctx context.Context, obj Object, opts ...SubResourceUpdateOption) error { 101 return f.subresourceWriter.Update(ctx, obj, append([]SubResourceUpdateOption{FieldOwner(f.owner)}, opts...)...) 102 } 103 104 func (f *subresourceClientWithFieldOwner) Patch(ctx context.Context, obj Object, patch Patch, opts ...SubResourcePatchOption) error { 105 return f.subresourceWriter.Patch(ctx, obj, patch, append([]SubResourcePatchOption{FieldOwner(f.owner)}, opts...)...) 106 }