Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/apis/cluster/v1alpha1/cluster_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ func (c *Cluster) APIEnablement(gvk schema.GroupVersionKind) APIEnablementStatus

return APIUnknown
}

// IsClusterReady checks if the Cluster is ready.
func (c *Cluster) IsClusterReady() bool {
return meta.IsStatusConditionPresentAndEqual(c.Status.Conditions, ClusterConditionReady, metav1.ConditionTrue)
}
34 changes: 34 additions & 0 deletions pkg/scheduler/core/assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,40 @@ func Test_dynamicScale(t *testing.T) {
},
wantErr: false,
},
{
name: "replica 12 -> 24, dynamic weighted 10:10(unhealthy cluster):10",
candidates: []spreadconstraint.ClusterDetailInfo{
{Name: ClusterMember1, Cluster: helper.NewClusterWithResource(ClusterMember1, corev1.ResourceList{
corev1.ResourcePods: *resource.NewQuantity(10, resource.DecimalSI),
}, util.EmptyResource().ResourceList(), util.EmptyResource().ResourceList()), AllocatableReplicas: 10},
{Name: ClusterMember2, Cluster: helper.NewClusterWithUnhealthyStatus(ClusterMember2, corev1.ResourceList{
corev1.ResourcePods: *resource.NewQuantity(10, resource.DecimalSI),
}, util.EmptyResource().ResourceList(), util.EmptyResource().ResourceList()), AllocatableReplicas: 10},
{Name: ClusterMember3, Cluster: helper.NewClusterWithResource(ClusterMember3, corev1.ResourceList{
corev1.ResourcePods: *resource.NewQuantity(10, resource.DecimalSI),
}, util.EmptyResource().ResourceList(), util.EmptyResource().ResourceList()), AllocatableReplicas: 10},
},
object: &workv1alpha2.ResourceBindingSpec{
ReplicaRequirements: &workv1alpha2.ReplicaRequirements{
ResourceRequest: util.EmptyResource().ResourceList(),
},
Replicas: 24,
Clusters: []workv1alpha2.TargetCluster{
{Name: ClusterMember1, Replicas: 2},
{Name: ClusterMember2, Replicas: 4},
{Name: ClusterMember3, Replicas: 6},
},
Placement: &policyv1alpha1.Placement{
ReplicaScheduling: dynamicWeightStrategy,
},
},
want: []workv1alpha2.TargetCluster{
{Name: ClusterMember1, Replicas: 8},
{Name: ClusterMember2, Replicas: 4},
{Name: ClusterMember3, Replicas: 12},
},
wantErr: false,
},
{
name: "replica 12 -> 24, dynamic weighted 1:1:1",
candidates: []spreadconstraint.ClusterDetailInfo{
Expand Down
7 changes: 7 additions & 0 deletions pkg/scheduler/core/division_algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"sort"

"k8s.io/klog/v2"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/scheduler/core/spreadconstraint"
Expand Down Expand Up @@ -124,6 +126,11 @@ func dynamicScaleUp(state *assignState) ([]workv1alpha2.TargetCluster, error) {
state.buildAvailableClusters(func(clusters []spreadconstraint.ClusterDetailInfo, _ *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster {
clusterAvailableReplicas := make([]workv1alpha2.TargetCluster, len(clusters))
for i, cluster := range clusters {
// set allocatable replicas for unhealthy cluster to zero when scale up.
if !cluster.Cluster.IsClusterReady() {
klog.Warningf("Cluster %s is not ready, skip it when scale up.", cluster.Name)
cluster.AllocatableReplicas = 0
}
clusterAvailableReplicas[i] = workv1alpha2.TargetCluster{
Name: cluster.Name,
Replicas: cluster.AllocatableReplicas,
Comment on lines +132 to 136
Copy link

Copilot AI Oct 21, 2025

Choose a reason for hiding this comment

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

Modifying the cluster variable here only affects the loop-local copy and doesn't update the original clusters slice. This means cluster.AllocatableReplicas = 0 has no effect on the value used in line 133. You should modify clusters[i].AllocatableReplicas = 0 instead to ensure the change persists.

Suggested change
cluster.AllocatableReplicas = 0
}
clusterAvailableReplicas[i] = workv1alpha2.TargetCluster{
Name: cluster.Name,
Replicas: cluster.AllocatableReplicas,
clusters[i].AllocatableReplicas = 0
}
clusterAvailableReplicas[i] = workv1alpha2.TargetCluster{
Name: cluster.Name,
Replicas: clusters[i].AllocatableReplicas,

Copilot uses AI. Check for mistakes.
Expand Down
43 changes: 43 additions & 0 deletions test/helper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,17 @@ func MakeNodeWithTaints(node string, milliCPU, memory, pods, ephemeralStorage in
func NewCluster(name string) *clusterv1alpha1.Cluster {
return &clusterv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: name},
Status: clusterv1alpha1.ClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1alpha1.ClusterConditionReady,
Status: metav1.ConditionTrue,
Reason: "ClusterReady",
Message: "cluster is healthy and ready to accept workloads",
LastTransitionTime: metav1.Now(),
},
},
},
}
}

Expand All @@ -692,6 +703,38 @@ func NewClusterWithResource(name string, allocatable, allocating, allocated core
Allocating: allocating,
Allocated: allocated,
},
Conditions: []metav1.Condition{
{
Type: clusterv1alpha1.ClusterConditionReady,
Status: metav1.ConditionTrue,
Reason: "ClusterReady",
Message: "cluster is healthy and ready to accept workloads",
LastTransitionTime: metav1.Now(),
},
},
},
}
}

// NewClusterWithUnhealthyStatus will build a Cluster with resource.
func NewClusterWithUnhealthyStatus(name string, allocatable, allocating, allocated corev1.ResourceList) *clusterv1alpha1.Cluster {
return &clusterv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: name},
Status: clusterv1alpha1.ClusterStatus{
ResourceSummary: &clusterv1alpha1.ResourceSummary{
Allocatable: allocatable,
Allocating: allocating,
Allocated: allocated,
},
Conditions: []metav1.Condition{
{
Type: clusterv1alpha1.ClusterConditionReady,
Status: metav1.ConditionFalse,
Reason: "ClusterNotReachable",
Message: "cluster is not reachable",
LastTransitionTime: metav1.Now(),
},
},
},
}
}
Expand Down