Skip to content

Conversation

@LivingCcj
Copy link
Contributor

@LivingCcj LivingCcj commented Oct 27, 2025

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR defines how to identify the resourcebinding represents resource is a workload which has a pod template ;
the function will be called by both the karmada-scheduler and the karmada controller.

For example:#6156

When the replicas of single pod template workload is zero, and replicaSchedulingType of their resource bindings is not Duplicated, karmada-scehduler should ignore to reschedule these resource bindings.

Fix relevant code:

if rb.Spec.Replicas == 0 ||
rb.Spec.Placement.ReplicaSchedulingType() == policyv1alpha1.ReplicaSchedulingTypeDuplicated {
// Duplicated resources should always be scheduled. Note: non-workload is considered as duplicated
// even if scheduling type is divided.
klog.V(3).Infof("Start to schedule ResourceBinding(%s/%s) as scheduling type is duplicated", namespace, name)
err = s.scheduleResourceBinding(rb)
metrics.BindingSchedule(string(ReconcileSchedule), utilmetrics.DurationInSeconds(start), err)
return err
}

Copilot AI review requested due to automatic review settings October 27, 2025 11:47
@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Oct 27, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @LivingCcj, 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 introduces a refined mechanism for identifying workload-associated ResourceBindings within the system. By adding a dedicated helper function and integrating it into the scheduler's core logic, the system can now more accurately distinguish between different types of resources, leading to more intelligent and flexible replica scheduling and cluster assignment strategies. This change improves the overall resource management capabilities, particularly for distributed workloads.

Highlights

  • New Workload Identification Function: A new method, IsBindingWorkload(), has been added to the ResourceBindingSpec to programmatically determine if a given ResourceBinding represents a workload. This function checks for the presence of replicas, replica requirements, or components.
  • Enhanced Scheduler Logic: The scheduler's logic in AssignReplicas and doScheduleBinding/doScheduleClusterBinding has been updated to leverage the new IsBindingWorkload() function. This allows for more precise conditional handling of workload versus non-workload resources during scheduling and replica assignment.
  • Unit Test Coverage: Comprehensive unit tests have been added for the IsBindingWorkload() function to ensure its correctness and validate its behavior under different conditions, specifically checking cases with and without replicas.
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.

@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 27, 2025
Copy link
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 introduces a new IsBindingWorkload() method to improve workload identification for ResourceBindings. Instead of simply checking if replicas are zero, the new method provides a more comprehensive way to determine if a ResourceBinding is associated with a workload by also considering ReplicaRequirements and Components fields.

Key changes:

  • Added IsBindingWorkload() method to ResourceBindingSpec that checks replicas, replica requirements, and components
  • Replaced direct Replicas == 0 checks with calls to the new IsBindingWorkload() method in scheduler code
  • Updated replica assignment logic to use the new workload identification method

Reviewed Changes

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

File Description
pkg/apis/work/v1alpha2/binding_types_helper.go Adds the IsBindingWorkload() method to centralize workload identification logic
pkg/apis/work/v1alpha2/binding_types_helper_test.go Adds unit tests for the new IsBindingWorkload() method
pkg/scheduler/scheduler.go Replaces Replicas == 0 checks with IsBindingWorkload() calls in scheduling logic
pkg/scheduler/core/common.go Updates AssignReplicas() to use IsBindingWorkload() for conditional logic

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

Comment on lines 69 to 74
var targetClusters []workv1alpha2.TargetCluster
if !spec.IsBindingWorkload() {
for _, cluster := range clusters {
targetClusters = append(targetClusters, workv1alpha2.TargetCluster{Name: cluster.Cluster.Name})
}
}
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

When spec.IsBindingWorkload() returns true (indicating a workload), targetClusters remains nil instead of being initialized to an empty slice. This changes the return behavior from returning an initialized empty slice to returning nil, which could cause issues for callers expecting a non-nil slice. Initialize targetClusters to an empty slice using make([]workv1alpha2.TargetCluster, 0) or preserve the original pre-allocation logic for the workload case.

Copilot uses AI. Check for mistakes.
Copy link

@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 introduces a new helper function IsBindingWorkload to determine if a ResourceBinding is for a workload, and refactors the scheduler to use this function. The overall approach is sound. My review includes suggestions to improve code conciseness in IsBindingWorkload, enhance test coverage to ensure all conditions of the new function are tested, and a minor performance optimization in slice handling.

Comment on lines 451 to 464
{
name: "binding a workload",
spec: &ResourceBindingSpec{
Replicas: 1,
},
want: true,
},
{
name: "not binding a workload when replicas is 0",
spec: &ResourceBindingSpec{
Replicas: 0,
},
want: false,
},

Choose a reason for hiding this comment

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

high

The test cases for IsBindingWorkload are not comprehensive as they only cover the Replicas field. To ensure the function works correctly under all conditions, please add test cases for ReplicaRequirements and Components as well.

{
			name: "binding a workload by replicas",
			spec: &ResourceBindingSpec{
				Replicas: 1,
			},
			want: true,
		},
		{
			name: "binding a workload by replica requirements",
			spec: &ResourceBindingSpec{
				ReplicaRequirements: &ReplicaRequirements{},
			},
			want: true,
		},
		{
			name: "binding a workload by components",
			spec: &ResourceBindingSpec{
				Components: []Component{{Name: "test"}},
			},
			want: true,
		},
		{
			name: "not a workload",
			spec: &ResourceBindingSpec{
				Replicas: 0,
			},
			want: false,
		},

Copy link
Member

Choose a reason for hiding this comment

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

  • binding represents a workload that has replicas
  • binding represents a workload that has replica requirements
  • binding represents a workload that has components

Comment on lines 219 to 222
if s.Replicas > 0 || s.ReplicaRequirements != nil || len(s.Components) > 0 {
return true
}
return false

Choose a reason for hiding this comment

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

medium

For better readability and conciseness, you can directly return the result of the boolean expression.

return s.Replicas > 0 || s.ReplicaRequirements != nil || len(s.Components) > 0

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member

Choose a reason for hiding this comment

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

I‘m ok with the current implementation, which I think is clear and easy to understand.

But I'm suggesting changing the method name, since Binding is a bit redundant. Like:

// IsWorkload returns true if the ResourceBinding represents a workload
// (e.g., Deployment, StatefulSet) that has replicas or replica requirements.
func (s *ResourceBindingSpec) IsWorkload() bool {
}

Comment on lines 70 to 74
if !spec.IsBindingWorkload() {
for _, cluster := range clusters {
targetClusters = append(targetClusters, workv1alpha2.TargetCluster{Name: cluster.Cluster.Name})
}
}

Choose a reason for hiding this comment

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

medium

Using append inside a loop can be inefficient due to potential reallocations if the slice grows. Since the number of clusters is known beforehand, it's more performant to pre-allocate the slice with make.

Suggested change
if !spec.IsBindingWorkload() {
for _, cluster := range clusters {
targetClusters = append(targetClusters, workv1alpha2.TargetCluster{Name: cluster.Cluster.Name})
}
}
if !spec.IsBindingWorkload() {
targetClusters = make([]workv1alpha2.TargetCluster, len(clusters))
for i, cluster := range clusters {
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Cluster.Name}
}
}

Copy link
Member

Choose a reason for hiding this comment

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

+1

@codecov-commenter
Copy link

codecov-commenter commented Oct 27, 2025

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

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.40%. Comparing base (a6a59d3) to head (45bda3a).

Files with missing lines Patch % Lines
pkg/scheduler/scheduler.go 0.00% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6881      +/-   ##
==========================================
+ Coverage   46.38%   46.40%   +0.02%     
==========================================
  Files         698      698              
  Lines       47763    47767       +4     
==========================================
+ Hits        22155    22167      +12     
+ Misses      23938    23932       -6     
+ Partials     1670     1668       -2     
Flag Coverage Δ
unittests 46.40% <66.66%> (+0.02%) ⬆️

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.

@LivingCcj
Copy link
Contributor Author

/retest

@karmada-bot
Copy link
Collaborator

@LivingCcj: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@karmada-bot
Copy link
Collaborator

@LivingCcj: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

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.

@zhzhuang-zju
Copy link
Contributor

/ok-to-test

@LivingCcj
Copy link
Contributor Author

/retest

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/assign

@RainbowMango RainbowMango added this to the v1.16 milestone Nov 3, 2025
Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

Generally looks good to me. But we might need to postpone this for a while, as the workloads do not currently support replica assignment yet. With this patch workload with multiple pod templates will be ignored during AssignReplicas, resulting in a return nil.

@RainbowMango
Copy link
Member

@LivingCcj This PR acutally fixes the #6156, right? Could you please update the PR description according to the PR template?

@karmada-bot karmada-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 12, 2025
@LivingCcj
Copy link
Contributor Author

@LivingCcj This PR acutally fixes the #6156, right? Could you please update the PR description according to the PR template?

Yes, this could fix the issue #6156, the PR describe and new commit has been pushed.

return err
}
if rb.Spec.Replicas == 0 ||
if !rb.Spec.IsSingleComponentsWorkload() ||
Copy link
Member

Choose a reason for hiding this comment

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

I think both single-component workload and multi-component workload should be skipped from rescheduling.

@karmada-bot
Copy link
Collaborator

[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 ask for approval from rainbowmango. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@karmada-bot karmada-bot added the do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. label Nov 14, 2025
@karmada-bot
Copy link
Collaborator

Adding label do-not-merge/contains-merge-commits because PR contains merge commits, which are not allowed in this repository.
Use git rebase to reapply your commits on top of the target branch. Detailed instructions for doing so can be found 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.

@karmada-bot karmada-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. kind/feature Categorizes issue or PR as related to a new feature. ok-to-test 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.

6 participants