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

🌱 Reuse hasMatchingLabels in MachineSet controller #4652

Merged
merged 1 commit into from
Jun 9, 2021

Conversation

enxebre
Copy link
Member

@enxebre enxebre commented May 24, 2021

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 #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 24, 2021
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 24, 2021
@enxebre enxebre changed the title Reuse hasMatchingLabels 🌱 Reuse hasMatchingLabels May 24, 2021
Copy link
Contributor

@JoelSpeed JoelSpeed left a 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

controllers/machineset_controller.go Outdated Show resolved Hide resolved
controllers/machineset_controller.go Outdated Show resolved Hide resolved
controllers/machineset_controller.go Outdated Show resolved Hide resolved
controllers/machineset_controller.go Outdated Show resolved Hide resolved
controllers/machineset_controller_test.go Show resolved Hide resolved
controllers/machineset_controller.go Outdated Show resolved Hide resolved
controllers/machineset_controller.go Outdated Show resolved Hide resolved
Copy link
Contributor

@srm09 srm09 left a 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.

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)
Copy link
Contributor

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.

Suggested change
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)

Copy link
Member Author

@enxebre enxebre May 31, 2021

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.

Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Member

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

Copy link
Member Author

@enxebre enxebre Jun 8, 2021

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.

Copy link
Member Author

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.

Copy link
Member

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?

controllers/machineset_controller.go Outdated Show resolved Hide resolved
@enxebre enxebre force-pushed the hasMatchingLabels branch 2 times, most recently from c1ebebe to 55c20f3 Compare May 31, 2021 09:26
@enxebre enxebre force-pushed the hasMatchingLabels branch from 55c20f3 to 0624a57 Compare June 8, 2021 08:22
@enxebre
Copy link
Member Author

enxebre commented Jun 8, 2021

/test pull-cluster-api-test-main

1 similar comment
@enxebre
Copy link
Member Author

enxebre commented Jun 8, 2021

/test pull-cluster-api-test-main

Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 9, 2021
@@ -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))
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment

@@ -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)
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@vincepri
Copy link
Member

vincepri commented Jun 9, 2021

/retitle 🌱 Reuse hasMatchingLabels in MachineSet controller

@k8s-ci-robot k8s-ci-robot changed the title 🌱 Reuse hasMatchingLabels 🌱 Reuse hasMatchingLabels in MachineSet controller Jun 9, 2021
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.
@enxebre enxebre force-pushed the hasMatchingLabels branch from 0624a57 to 9ad4ea8 Compare June 9, 2021 15:53
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 9, 2021
Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 9, 2021
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 9, 2021
@k8s-ci-robot k8s-ci-robot merged commit 45a0abb into kubernetes-sigs:master Jun 9, 2021
@k8s-ci-robot k8s-ci-robot added this to the v0.4 milestone Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants