-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
⚠️ Call MutateFn on CreateOrUpdate regardless of the operation #319
⚠️ Call MutateFn on CreateOrUpdate regardless of the operation #319
Conversation
588e4d1
to
1dfa4fe
Compare
I had it like that but @mengqiy commented in #313 that this is maybe not the thing we want to do... |
1dfa4fe
to
adbc7af
Compare
The behavior was to allways call on
|
Yes but doesn't the caller of |
I understand your point now. I think that can work but it's a breaking change and might deserve another PR. For now you can still get away without casting like this: controllerutil.CreateOrUpdate(context.TODO(), c, deploy, func(_ runtime.Object) error {
deploy.Spec.Replicas = 5
return nil
}) |
Ok so I'm not the only one seeing this 😀 anyways then I'm also fine with it |
I think the debate is if #313 has not been released yet, so we still have time to decide what we want @DirectXMan12 what was the initial intend of this method? I will let you make the final call since you are the reviewer of that PR. |
Probably I'm biased here doing the original implementation but my take on this is that is better to keep the things the way they are with Otherwise controller-runtime users will probably end up implementing something that's like server-side apply in The breaking change that I'd be inclined to do is to remove or make optional the Btw. we're relying to the original behavior on 3 operators already and the feedback on the original PR was that it enables good patterns in user's code. |
This is the original PR: #98. I think it's worth revisiting it first. |
I'm 👍 on changing I'm also 👍 on fixing this to always call |
(now is the time to make breaking changes, if we want to) |
P.S. We should add really obvious tests that make it clear that this is the desired behavior as well. |
I've removed the parameter in |
a360c32
to
ab1ec54
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nits inline, otherwise looks good
@@ -116,18 +116,26 @@ const ( // They should complete the sentence "Deployment default/foo has been .. | |||
|
|||
// CreateOrUpdate creates or updates the given object obj in the Kubernetes | |||
// cluster. The object's desired state should be reconciled with the existing | |||
// state using the passed in ReconcileFn. obj must be a struct pointer so that | |||
// state using the passed in MutateFn. obj is just a struct pointer so that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: docs are wrong now (nothing is passed to mutate function, the sentence about obj
needs to be updated too)
// It returns the executed operation and an error. | ||
func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) (OperationResult, error) { | ||
func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) (OperationResult, error) { // nolint: gocyclo | ||
key, err := client.ObjectKeyFromObject(obj) | ||
if err != nil { | ||
return OperationResultNone, err | ||
} | ||
|
||
if err := c.Get(ctx, key, obj); err != nil { | ||
if errors.IsNotFound(err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can improve nesting and readability here a bit by inverting this:
if errors.IsNotFound(err) { | |
if !errors.IsNotFound(err) { | |
return OperationResultNone, err | |
} | |
if err := f(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will probably fix the cyclo issue too.
ab1ec54
to
3683b64
Compare
3683b64
to
d98fd5b
Compare
Implemented the feedback and also addressed the gocyclo issue. @DirectXMan12 PTAL. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: calind, DirectXMan12 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
#313 breaks
CreateOrUpdate
by not calling theMutateFn
when new objects are created. This patch fixes that and updates the tests to better reflect the expected behavior.