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
20 changes: 13 additions & 7 deletions api/workloads/v1alpha1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ func (rbg *RoleBasedGroup) GetCommonAnnotationsFromRole(role *RoleSpec) map[stri
func (rbg *RoleBasedGroup) GetGroupSize() int {
ret := 0
for _, role := range rbg.Spec.Roles {
if role.Workload.String() == LeaderWorkerSetWorkloadType {
if role.LeaderWorkerSet == nil {
ret += 1 * int(*role.Replicas)
continue
if role.Replicas == nil {
continue
}

if role.Workload.String() == LeaderWorkerSetWorkloadType ||
(role.Workload.String() == InstanceSetWorkloadType && role.LeaderWorkerSet != nil) {
sizePerReplica := int32(1)
if role.LeaderWorkerSet != nil && role.LeaderWorkerSet.Size != nil && *role.LeaderWorkerSet.Size > 0 {
sizePerReplica = *role.LeaderWorkerSet.Size
}
ret += int(*role.LeaderWorkerSet.Size) * int(*role.Replicas)
} else {
ret += int(*role.Replicas)
ret += int(sizePerReplica * *role.Replicas)
continue
}

ret += int(*role.Replicas)
Comment on lines +34 to +44

Choose a reason for hiding this comment

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

medium

For improved readability and maintainability, consider refactoring this block. Extracting the complex condition into a boolean variable and using a standard if-else structure instead of continue can make the code's intent clearer and easier to follow.

		workloadType := role.Workload.String()
		useLwsSize := workloadType == LeaderWorkerSetWorkloadType ||
			(workloadType == InstanceSetWorkloadType && role.LeaderWorkerSet != nil)

		if useLwsSize {
			sizePerReplica := int32(1)
			if role.LeaderWorkerSet != nil && role.LeaderWorkerSet.Size != nil && *role.LeaderWorkerSet.Size > 0 {
				sizePerReplica = *role.LeaderWorkerSet.Size
			}
			ret += int(sizePerReplica * *role.Replicas)
		} else {
			ret += int(*role.Replicas)
		}

}
return ret
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/scheduler/podgroup_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ func TestPodGroupScheduler_Reconcile(t *testing.T) {
expectPG: true,
expectError: false,
},
{
name: "create kube pod group with instanceset lws role should use lws size",
client: fake.NewClientBuilder().WithScheme(scheme).Build(),
rbg: func() *workloadsv1alpha.RoleBasedGroup {
role := wrappers.BuildBasicRole("instance-lws-role").
WithWorkload(workloadsv1alpha.InstanceSetWorkloadType).
WithReplicas(2).Obj()
role.LeaderWorkerSet = &workloadsv1alpha.LeaderWorkerTemplate{
Size: ptr.To(int32(3)),
}

return wrappers.BuildBasicRoleBasedGroup(rbgName, rbgNamespace).
WithRoles([]workloadsv1alpha.RoleSpec{role}).
WithKubeGangScheduling(true).Obj()
}(),
apiReader: fake.NewClientBuilder().WithScheme(scheme).WithObjects(
&apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: KubePodGroupCrdName},
Status: apiextensionsv1.CustomResourceDefinitionStatus{
Conditions: []apiextensionsv1.CustomResourceDefinitionCondition{
{
Type: apiextensionsv1.Established,
Status: apiextensionsv1.ConditionTrue,
},
},
},
},
).Build(),
expectPG: true,
expectError: false,
},
{
name: "create pod group when volcano gang scheduling enabled and pod group not exists",
client: fake.NewClientBuilder().WithScheme(scheme).Build(),
Expand Down
Loading