Skip to content

Commit a40a850

Browse files
committed
Scheduler should detect change in replicas for multiple component workloads
Signed-off-by: mszacillo <[email protected]>
1 parent 2dac564 commit a40a850

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

pkg/util/binding.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,41 @@ func IsBindingReplicasChanged(bindingSpec *workv1alpha2.ResourceBindingSpec, str
3838
if strategy == nil {
3939
return false
4040
}
41+
42+
bindingReplicas := GetTotalBindingReplicas(bindingSpec)
43+
4144
if strategy.ReplicaSchedulingType == policyv1alpha1.ReplicaSchedulingTypeDuplicated {
4245
for _, targetCluster := range bindingSpec.Clusters {
43-
if targetCluster.Replicas != bindingSpec.Replicas {
46+
if targetCluster.Replicas != bindingReplicas {
4447
return true
4548
}
4649
}
4750
return false
4851
}
4952
if strategy.ReplicaSchedulingType == policyv1alpha1.ReplicaSchedulingTypeDivided {
5053
replicasSum := GetSumOfReplicas(bindingSpec.Clusters)
51-
return replicasSum != bindingSpec.Replicas
54+
return replicasSum != bindingReplicas
5255
}
5356
return false
5457
}
5558

59+
// GetTotalBindingReplicas will get the total replicas for a given resourcebinding
60+
func GetTotalBindingReplicas(bindingSpec *workv1alpha2.ResourceBindingSpec) int32 {
61+
if len(bindingSpec.Components) > 0 {
62+
return GetSumOfReplicasForComponents(bindingSpec.Components)
63+
}
64+
return bindingSpec.Replicas
65+
}
66+
67+
// GetSumOfReplicasForComponents will get the sum of replicas for multi-component resources
68+
func GetSumOfReplicasForComponents(components []workv1alpha2.Component) int32 {
69+
replicasSum := int32(0)
70+
for _, component := range components {
71+
replicasSum += component.Replicas
72+
}
73+
return replicasSum
74+
}
75+
5676
// GetSumOfReplicas will get the sum of replicas in target clusters
5777
func GetSumOfReplicas(clusters []workv1alpha2.TargetCluster) int32 {
5878
replicasSum := int32(0)

pkg/util/binding_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,60 @@ func TestIsBindingReplicasChanged(t *testing.T) {
177177
strategy: &policyv1alpha1.ReplicaSchedulingStrategy{ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided},
178178
expected: true,
179179
},
180+
{
181+
name: "Duplicated strategy with components not changed",
182+
bindingSpec: &workv1alpha2.ResourceBindingSpec{
183+
Components: []workv1alpha2.Component{
184+
{
185+
Name: "jobmanager",
186+
Replicas: 2,
187+
},
188+
{
189+
Name: "taskmanager",
190+
Replicas: 3,
191+
},
192+
},
193+
Clusters: []workv1alpha2.TargetCluster{
194+
{
195+
Name: ClusterMember1,
196+
Replicas: 5,
197+
},
198+
{
199+
Name: ClusterMember2,
200+
Replicas: 5,
201+
},
202+
},
203+
},
204+
strategy: &policyv1alpha1.ReplicaSchedulingStrategy{ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDuplicated},
205+
expected: false,
206+
},
207+
{
208+
name: "Divided strategy with components changed",
209+
bindingSpec: &workv1alpha2.ResourceBindingSpec{
210+
Components: []workv1alpha2.Component{
211+
{
212+
Name: "jobmanager",
213+
Replicas: 2,
214+
},
215+
{
216+
Name: "taskmanager",
217+
Replicas: 3,
218+
},
219+
},
220+
Clusters: []workv1alpha2.TargetCluster{
221+
{
222+
Name: ClusterMember1,
223+
Replicas: 2,
224+
},
225+
{
226+
Name: ClusterMember2,
227+
Replicas: 2, // assigned sum = 4 != desired 5
228+
},
229+
},
230+
},
231+
strategy: &policyv1alpha1.ReplicaSchedulingStrategy{ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided},
232+
expected: true,
233+
},
180234
}
181235

182236
for _, tt := range tests {

0 commit comments

Comments
 (0)