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

Shorten UpdateNodeLabelsAndRecordEvents function in nmc_reconciler.go #850

Conversation

TomerNewman
Copy link
Contributor

Summary

Today, the UpdateNodeLabelsAndRecordEvents function has around 100 lines of code. This commit aims to enhance its readability and maintainability by using helper functions, thereby reducing its total length. Additionally, adding unit tests that covers the new helper functions.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 27, 2024
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 27, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @TomerNewman. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 27, 2024
Copy link

netlify bot commented Jun 27, 2024

Deploy Preview for kubernetes-sigs-kmm ready!

Name Link
🔨 Latest commit fe5d5d8
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-kmm/deploys/6693e64509088d0008253968
😎 Deploy Preview https://deploy-preview-850--kubernetes-sigs-kmm.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch from 3e328b3 to f017dde Compare June 27, 2024 10:44
@TomerNewman
Copy link
Contributor Author

/assign @yevgeny-shnaidman

@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch from f017dde to 158c79a Compare June 27, 2024 11:51
@yevgeny-shnaidman
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 30, 2024
@yevgeny-shnaidman
Copy link
Contributor

It looks good to me , but i have another suggestion:
most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

@TomerNewman
Copy link
Contributor Author

It looks good to me , but i have another suggestion: most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

so it should be in a different package? maybe in internal->utils->kmmlabels.go?

@yevgeny-shnaidman
Copy link
Contributor

It looks good to me , but i have another suggestion: most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

so it should be in a different package? maybe in internal->utils->kmmlabels.go?

No, it can be in the same package, for example like PodManager interface

@TomerNewman
Copy link
Contributor Author

It looks good to me , but i have another suggestion: most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

so it should be in a different package? maybe in internal->utils->kmmlabels.go?

No, it can be in the same package, for example like PodManager interface

I'm on it, give me thumbs up to the comment to confirm and I'll push the changes

@ybettan
Copy link
Contributor

ybettan commented Jun 30, 2024

It looks good to me , but i have another suggestion:
most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

I think that the parts that add/remove labels from the nodes should be part of the new node interface as long as the label is passed as a parameter.

For other cases, I definitely think we should create another interface and I would also somehow take into consideration internal->utils->kmmlabels.go as it is serving a similar purpose.

@yevgeny-shnaidman
Copy link
Contributor

It looks good to me , but i have another suggestion:
most of the code of UpdateNodeLabelsAndRecordEvents function deals with preparing the labels for the node. What if we define another helper interface, that will handle that preparation? This way we can unit-test the UpdateNodeLabelsAndRecordEvents properly and in addition test the labels preparation thoroughly also, since it will be in a different interface. @ybettan , @TomerNewman , what do you think? Or is it an overkill?

I think that the parts that add/remove labels from the nodes should be part of the new node interface as long as the label is passed as a parameter.

For other cases, I definitely think we should create another interface and I would also somehow take into consideration internal->utils->kmmlabels.go as it is serving a similar purpose.

I thought about kmmlabels, but its main purpose is to define the labels, to to manipulate what and when should go to wihat node

@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch from 158c79a to 0436d6e Compare July 3, 2024 09:34
@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 3, 2024
@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch from 0436d6e to 86852c8 Compare July 3, 2024 10:12
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 3, 2024
@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch 2 times, most recently from 8601713 to 24f27dc Compare July 3, 2024 10:50
@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch 2 times, most recently from 680e260 to db98806 Compare July 11, 2024 12:22
@codecov-commenter
Copy link

codecov-commenter commented Jul 14, 2024

Codecov Report

Attention: Patch coverage is 97.46835% with 2 lines in your changes missing coverage. Please review.

Project coverage is 71.95%. Comparing base (fa23a9b) to head (fe5d5d8).
Report is 80 commits behind head on main.

Files Patch % Lines
internal/controllers/nmc_reconciler.go 96.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #850      +/-   ##
==========================================
- Coverage   79.09%   71.95%   -7.14%     
==========================================
  Files          51       66      +15     
  Lines        5109     4522     -587     
==========================================
- Hits         4041     3254     -787     
- Misses        882     1073     +191     
- Partials      186      195       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

internal/node/node.go Outdated Show resolved Hide resolved
internal/node/node_test.go Outdated Show resolved Hide resolved
internal/node/node_test.go Outdated Show resolved Hide resolved
internal/node/node_test.go Outdated Show resolved Hide resolved
internal/node/node_test.go Outdated Show resolved Hide resolved
@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch 2 times, most recently from b5eaa80 to 2ed533a Compare July 14, 2024 11:07
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 14, 2024
)

_, err := r.Reconcile(ctx, req)
_, err = r.Reconcile(ctx, req)
Expect(err).ToNot(BeNil())
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is for purpose learning only as it is not directly related to your PR.

This is not a great validation. The description of the spec is should complete all the reconcile functions and return combined error, therefore, the validation should be to (first return different error messages and then) validate that the combined error contain each one of the error messages of the individual errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ybettan so in that case, I can add validations that each expected error should be in err, to add it to the PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to add it to the PR so we can merge it faster. You can fix it in a future PR if you like. But to answer your question, the answer is YES.

One way to check it is like this example:

Expect(err.Error()).To(ContainSubstring("failed to get pull options for image"))

Today, the UpdateNodeLabelsAndRecordEvents function has around 100 lines of code. This commit aims to enhance its readability and maintainability by using helper functions, thereby reducing its total length.
Additionally, adding unit tests that covers the new helper functions.
@TomerNewman TomerNewman force-pushed the feature/shorten-UpdateNodeLabelsAndRecordEvents branch from 2ed533a to fe5d5d8 Compare July 14, 2024 14:52
@ybettan
Copy link
Contributor

ybettan commented Jul 16, 2024

/approve
/assign @yevgeny-shnaidman

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: TomerNewman, ybettan

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 Jul 16, 2024
@yevgeny-shnaidman
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 16, 2024
@k8s-ci-robot k8s-ci-robot merged commit 72e21e6 into kubernetes-sigs:main Jul 16, 2024
22 checks passed
@TomerNewman TomerNewman deleted the feature/shorten-UpdateNodeLabelsAndRecordEvents branch July 17, 2024 10:56
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants