Created
April 11, 2024 06:00
-
-
Save ahmetb/34bd3f750c0d2cbcdeabd0f53c3dea32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ctrlutil | |
import ( | |
"context" | |
"k8s.io/apimachinery/pkg/api/meta" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/apimachinery/pkg/runtime/schema" | |
"sigs.k8s.io/controller-runtime/pkg/client" | |
) | |
// ClientWithFieldManager wraps a client.Client and adds the specified | |
// fieldManager to outgoing create/update/patch requests. | |
func ClientWithFieldManager(c client.Client, fieldManager string) client.Client { | |
return &fieldManagerClient{ | |
fieldManager: fieldManager, | |
c: c, | |
Reader: c, | |
} | |
} | |
type fieldManagerClient struct { | |
fieldManager string | |
c client.Client | |
client.Reader | |
} | |
func (f *fieldManagerClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { | |
return f.c.Create(ctx, obj, append([]client.CreateOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} | |
func (f *fieldManagerClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { | |
return f.c.Update(ctx, obj, append([]client.UpdateOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} | |
func (f *fieldManagerClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { | |
return f.c.Patch(ctx, obj, patch, append([]client.PatchOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} | |
func (f *fieldManagerClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { | |
return f.c.Delete(ctx, obj, opts...) // untouched | |
} | |
func (f *fieldManagerClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { | |
return f.c.DeleteAllOf(ctx, obj, opts...) // untouched | |
} | |
func (f *fieldManagerClient) Scheme() *runtime.Scheme { return f.c.Scheme() } | |
func (f *fieldManagerClient) RESTMapper() meta.RESTMapper { return f.c.RESTMapper() } | |
func (f *fieldManagerClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) { | |
return f.c.GroupVersionKindFor(obj) | |
} | |
func (f *fieldManagerClient) IsObjectNamespaced(obj runtime.Object) (bool, error) { | |
return f.c.IsObjectNamespaced(obj) | |
} | |
func (f *fieldManagerClient) Status() client.StatusWriter { | |
return &fieldManagerSubresourceClient{ | |
fieldManager: f.fieldManager, | |
subresourceWriter: f.c.Status(), | |
} | |
} | |
func (f *fieldManagerClient) SubResource(subresource string) client.SubResourceClient { | |
c := f.c.SubResource(subresource) | |
return &fieldManagerSubresourceClient{ | |
fieldManager: f.fieldManager, | |
subresourceWriter: c, | |
SubResourceReader: c, | |
} | |
} | |
type fieldManagerSubresourceClient struct { | |
fieldManager string | |
subresourceWriter client.SubResourceWriter | |
client.SubResourceReader | |
} | |
func (f *fieldManagerSubresourceClient) Create(ctx context.Context, obj client.Object, subresource client.Object, opts ...client.SubResourceCreateOption) error { | |
return f.subresourceWriter.Create(ctx, obj, subresource, append([]client.SubResourceCreateOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} | |
func (f *fieldManagerSubresourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error { | |
return f.subresourceWriter.Update(ctx, obj, append([]client.SubResourceUpdateOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} | |
func (f *fieldManagerSubresourceClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error { | |
return f.subresourceWriter.Patch(ctx, obj, patch, append([]client.SubResourcePatchOption{client.FieldOwner(f.fieldManager)}, opts...)...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment