Skip to content

[refactor] use shared functions to reduce redundancy in the estimator pkg#7296

Open
XiShanYongYe-Chang wants to merge 1 commit intokarmada-io:masterfrom
XiShanYongYe-Chang:refactor-estimator
Open

[refactor] use shared functions to reduce redundancy in the estimator pkg#7296
XiShanYongYe-Chang wants to merge 1 commit intokarmada-io:masterfrom
XiShanYongYe-Chang:refactor-estimator

Conversation

@XiShanYongYe-Chang
Copy link
Copy Markdown
Member

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

  1. Use shared functions to reduce redundancy in the estimator pkg
  2. Reduced unnecessary nodeAffinity parsing.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Copilot AI review requested due to automatic review settings March 17, 2026 12:04
@karmada-bot karmada-bot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Mar 17, 2026
@karmada-bot karmada-bot requested a review from Garrybest March 17, 2026 12:04
@karmada-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign garrybest for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details 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

@karmada-bot karmada-bot requested a review from jabellard March 17, 2026 12:04
@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 17, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the estimator package to enhance code reusability and efficiency. It introduces shared utility functions for node matching and resource calculation, consolidating logic that was previously duplicated. This change streamlines the codebase, making it easier to maintain and understand, and improves performance by reducing redundant computations, particularly in node affinity parsing.

Highlights

  • Code Refactoring: Centralized node matching logic into a shared MatchNode function, reducing redundancy across the estimator package.
  • Performance Optimization: Optimized node affinity parsing by pre-calculating RequiredNodeAffinity and Tolerations once per component, avoiding repeated parsing within loops.
Changelog
  • pkg/estimator/scheduling_simulator_components.go
    • Imported k8s.io/component-helpers/scheduling/corev1/nodeaffinity for node affinity handling.
    • Modified scheduleComponent to pre-calculate node affinity and tolerations and use the new MatchNode function.
    • Refactored the internal matchNode function into an exported MatchNode function, which now accepts pre-computed affinity and tolerations.
  • pkg/estimator/scheduling_simulator_components_test.go
    • Imported nodeutil for GetRequiredNodeAffinity.
    • Updated TestMatchNode to use the new MatchNode signature, passing pre-calculated affinity and tolerations.
  • pkg/estimator/server/framework/plugins/noderesource/noderesource.go
    • Introduced a disabledResult() helper function to encapsulate the logic for returning a disabled plugin result, removing redundant klog calls.
    • Updated Estimate to use the new estimator.MatchNode function for node matching.
    • Extracted the logic for calculating available node resources into a new getNodeAvailableResource function.
    • Modified nodeMaxAvailableReplica and getNodesAvailableResources to utilize the new getNodeAvailableResource function.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively refactors the estimator package to improve code reuse and performance. Key improvements include introducing a shared MatchNode function to avoid redundant node affinity parsing within loops, and extracting duplicated logic for handling disabled plugins and calculating available node resources into helper functions (disabledResult and getNodeAvailableResource). The changes are clear and align well with the goal of reducing redundancy. I have one suggestion to further reduce duplication by creating a helper for extracting affinity and tolerations.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the estimator package to reuse shared node-matching and node-available-resource helpers, aiming to reduce redundant logic and avoid repeated nodeAffinity parsing during scheduling simulation.

Changes:

  • Reused a shared MatchNode helper (now exported from pkg/estimator) across the scheduling simulator and the node resource estimator plugin.
  • Factored out node available-resource computation into getNodeAvailableResource and reused it for both replica and component-set estimation.
  • Consolidated the “plugin disabled” return path into a shared disabledResult() helper.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/estimator/server/framework/plugins/noderesource/noderesource.go Refactors node filtering and available-resource calculation; adds shared disabled handling.
pkg/estimator/scheduling_simulator_components.go Avoids per-node parsing by precomputing affinity/tolerations once per component; exports MatchNode.
pkg/estimator/scheduling_simulator_components_test.go Updates node-matching tests to call the new exported MatchNode and precompute inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 17, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 68.18182% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.03%. Comparing base (5d72d49) to head (b477b39).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
...ver/framework/plugins/noderesource/noderesource.go 60.00% 6 Missing ⚠️
pkg/estimator/scheduling_simulator_components.go 85.71% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7296      +/-   ##
==========================================
+ Coverage   42.02%   42.03%   +0.01%     
==========================================
  Files         874      874              
  Lines       53551    53552       +1     
==========================================
+ Hits        22503    22510       +7     
+ Misses      29356    29349       -7     
- Partials     1692     1693       +1     
Flag Coverage Δ
unittests 42.03% <68.18%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: changzhen <changzhen5@huawei.com>
@XiShanYongYe-Chang
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively refactors the estimator package to reduce code duplication and improve performance by avoiding redundant node affinity parsing. The introduction of shared functions like GetAffinityAndTolerations and MatchNode is a solid improvement. I have one suggestion to make the new getNodeAvailableResource function safer by removing a side effect, which will improve its robustness for future use.

@XiShanYongYe-Chang
Copy link
Copy Markdown
Member Author

/cc @zhzhuang-zju

@zhzhuang-zju
Copy link
Copy Markdown
Contributor

Thanks
/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants