-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🌱 Reuse hasMatchingLabels
in MachineSet controller
#4652
Conversation
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.
One quick question, otherwise seems reasonable to me
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.
some minor nits/suggestions.
controllers/machineset_controller.go
Outdated
log.Error(err, "Failed to list machine sets") | ||
return nil | ||
if err := r.Client.List(ctx, msList, client.InNamespace(m.Namespace)); err != nil { | ||
return nil, fmt.Errorf("failed to list MachineSets: %w", 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.
nit: Maybe also re-introduce the logger here.
return nil, fmt.Errorf("failed to list MachineSets: %w", err) | |
log.Error(err, "Failed to list machine sets") | |
return nil, errors.Wrapf(err, "failed to list MachineSets") |
The second one is more of a general thing, in most of the places in the codebase we use Wrapf
instead of Errorf
. Maybe we should use a single practice across (with the emphasis on maybe)
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.
errors.Wrapf
is around probably only because there was no native support for it before golang 1.13. Unless there's a reason to not I suggest we rely on the standard errors library package and follow fmt.Errorf %w", err)
pattern for any upcoming changes rather than perpetuate the old pattern.
https://blog.golang.org/go1.13-errors#TOC_3.3.
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.
I agree on the fmt.Errorf
, but I like the suggestion to keep the logging.
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.
This function is now returning an appropriate concatenated error string. It's a consumer of the function choice to handle the error, which in this case is already logging, so if we log here it'd be duplicated https://github.com/kubernetes-sigs/cluster-api/pull/4652/files#diff-2678c5ef8cd150e20f9ea965dcaaea54e17e2c8010eb37447b5745760aca890fR538-R540
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.
We usually use the pkg/errors package instead of fmt
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.
@vincepri Is there a reason to keep perpetuating that pattern and an external dependency rather than using the standard core package? I assume we are using pkg/errors only because before Golang 1.13 there was no builtin way to wrap errors? If that's the case I'd be in favour of using the standard library for upcoming changes fmt.Errorf %w", err)
.
No need to bikeshed on this though, I'm good either way though I would like to get context and understand the motivation.
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.
Updated anyways so we can move along.
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.
pkg/errors is in general easier to use, and it's used pretty much everywhere within k8s https://grep.app/search?q=pkg/errors&filter[repo][0]=kubernetes/kubernetes — In general though, if it's not broken why switch?
c1ebebe
to
55c20f3
Compare
55c20f3
to
0624a57
Compare
/test pull-cluster-api-test-main |
1 similar comment
/test pull-cluster-api-test-main |
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.
/lgtm
controllers/machineset_controller.go
Outdated
@@ -519,6 +519,7 @@ func (r *MachineSetReconciler) waitForMachineDeletion(ctx context.Context, machi | |||
// MachineToMachineSets is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation | |||
// for MachineSets that might adopt an orphaned Machine. | |||
func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Request { | |||
log := ctrl.LoggerFrom(context.TODO(), "object", client.ObjectKeyFromObject(o)) |
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.
FYI, this will never log unless the global logger is set
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.
Added a comment
controllers/machineset_controller.go
Outdated
@@ -534,7 +535,11 @@ func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Requ | |||
} | |||
} | |||
|
|||
mss := r.getMachineSetsForMachine(context.TODO(), m) | |||
mss, err := r.getMachineSetsForMachine(context.TODO(), m) |
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 we define this context only once at the top of the function?
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.
Done
/retitle 🌱 Reuse |
hasMatchingLabels
in MachineSet controller
This let the machineSet reconciler reuse the logic from hasMatchingLabels. It also fix getMachineSetsForMachine to return error appropriately to let the caller ignore them or not.
0624a57
to
9ad4ea8
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.
/approve
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: vincepri 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 |
What this PR does / why we need it:
This let the machineSet reconciler reuse the logic from hasMatchingLabels.
It also fix getMachineSetsForMachine to return error appropriately to let the caller ignore it or not.
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #