Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱Add a Client wrapper that sets the field owner #1474

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/fieldowner/field_owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fieldowner
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better in pkg/client, ppl might not find it here


import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"
)

// Wrap wraps a Client with one that sets the field owner.
func Wrap(upstream client.Client, owner string) client.Client {
return &clientWithOwner{Client: upstream, owner: client.FieldOwner(owner)}
}

type clientWithOwner struct {
client.Client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to embedd the Client, because should we extend the Client in the future with a method that needs wrapping, the clientWithOwner won't do that but the code will still compile.

owner client.FieldOwner
}

func (c *clientWithOwner) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return c.Client.Create(ctx, obj, append(opts, c.owner)...)
}

func (c *clientWithOwner) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return c.Client.Update(ctx, obj, append(opts, c.owner)...)
}

func (c *clientWithOwner) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete doesn't need it? I intuitively would have expected Delete to make you owner of metadata.DeletionTimestamp (but haven't actually checked)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, me too, but the FieldOwner type does not coerce to a DeleteOption.

I wonder if @DirectXMan12 who seems to have authored that in #435 has a view?

return c.Client.Patch(ctx, obj, patch, append(opts, c.owner)...)
}

func (c *clientWithOwner) Status() client.StatusWriter {
return &statusWriterWithOwner{StatusWriter: c.Client.Status(), owner: c.owner}
}

type statusWriterWithOwner struct {
client.StatusWriter
owner client.FieldOwner
}

func (s *statusWriterWithOwner) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return s.StatusWriter.Update(ctx, obj, append(opts, s.owner)...)
}

func (s *statusWriterWithOwner) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return s.StatusWriter.Patch(ctx, obj, patch, append(opts, s.owner)...)
}