Skip to content

Commit 579d226

Browse files
committed
add IsWorkload() for ResourceBindingSpec
Signed-off-by: LivingCcj <[email protected]>
1 parent 8dbca68 commit 579d226

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

pkg/apis/work/v1alpha2/binding_types_helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,12 @@ func (s *ResourceBindingSpec) SchedulePriorityValue() int32 {
213213
}
214214
return s.SchedulePriority.Priority
215215
}
216+
217+
// IsWorkload return true if the ResourceBinding represents workload which has replicas or replica requirements (e.g., Deployment, StatefulSet)
218+
// or multi-component workloads (e.g., FlinkDeployment), false otherwise.
219+
func (s *ResourceBindingSpec) IsWorkload() bool {
220+
if s.Replicas > 0 || s.ReplicaRequirements != nil || len(s.Components) >= 1 {
221+
return true
222+
}
223+
return false
224+
}

pkg/apis/work/v1alpha2/binding_types_helper_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/api/resource"
2325
"k8s.io/utils/ptr"
2426

2527
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
@@ -441,3 +443,84 @@ func TestResourceBindingSpec_SchedulingSuspended(t *testing.T) {
441443
})
442444
}
443445
}
446+
447+
func TestResourceBindingSpec_IsWorkload(t *testing.T) {
448+
tests := []struct {
449+
name string
450+
spec *ResourceBindingSpec
451+
want bool
452+
}{
453+
{
454+
name: "binding an object with replicas",
455+
spec: &ResourceBindingSpec{
456+
Replicas: 1,
457+
},
458+
want: true,
459+
},
460+
{
461+
name: "binding an object when replicas is 0",
462+
spec: &ResourceBindingSpec{
463+
Replicas: 0,
464+
},
465+
want: false,
466+
},
467+
{
468+
name: "binding an object with ReplicaRequirement",
469+
spec: &ResourceBindingSpec{
470+
ReplicaRequirements: &ReplicaRequirements{
471+
ResourceRequest: corev1.ResourceList{
472+
corev1.ResourceCPU: resource.MustParse("100m"),
473+
corev1.ResourceMemory: resource.MustParse("200Mi"),
474+
},
475+
},
476+
},
477+
want: true,
478+
},
479+
{
480+
name: "binding an object with no ReplicaRequirement",
481+
spec: &ResourceBindingSpec{
482+
ReplicaRequirements: nil,
483+
},
484+
want: false,
485+
},
486+
{
487+
name: "binding an object with single component",
488+
spec: &ResourceBindingSpec{
489+
Components: []Component{
490+
{
491+
Name: "comp1",
492+
Replicas: 1,
493+
ReplicaRequirements: nil,
494+
},
495+
},
496+
},
497+
want: true,
498+
},
499+
{
500+
name: "binding an object with multiple components",
501+
spec: &ResourceBindingSpec{
502+
Components: []Component{
503+
{
504+
Name: "comp1",
505+
Replicas: 1,
506+
ReplicaRequirements: nil,
507+
},
508+
{
509+
Name: "comp2",
510+
Replicas: 1,
511+
ReplicaRequirements: nil,
512+
},
513+
},
514+
},
515+
want: true,
516+
},
517+
}
518+
for _, tt := range tests {
519+
t.Run(tt.name, func(t *testing.T) {
520+
ret := tt.spec.IsWorkload()
521+
if ret != tt.want {
522+
t.Fatalf("want %v, got %v", tt.want, ret)
523+
}
524+
})
525+
}
526+
}

pkg/scheduler/scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ func (s *Scheduler) doScheduleBinding(namespace, name string) (err error) {
420420
metrics.BindingSchedule(string(ReconcileSchedule), utilmetrics.DurationInSeconds(start), err)
421421
return err
422422
}
423-
if rb.Spec.Replicas == 0 ||
423+
if !rb.Spec.IsWorkload() ||
424424
rb.Spec.Placement.ReplicaSchedulingType() == policyv1alpha1.ReplicaSchedulingTypeDuplicated {
425425
// Duplicated resources should always be scheduled. Note: non-workload is considered as duplicated
426426
// even if scheduling type is divided.
@@ -496,7 +496,7 @@ func (s *Scheduler) doScheduleClusterBinding(name string) (err error) {
496496
metrics.BindingSchedule(string(ReconcileSchedule), utilmetrics.DurationInSeconds(start), err)
497497
return err
498498
}
499-
if crb.Spec.Replicas == 0 ||
499+
if !crb.Spec.IsWorkload() ||
500500
crb.Spec.Placement.ReplicaSchedulingType() == policyv1alpha1.ReplicaSchedulingTypeDuplicated {
501501
// Duplicated resources should always be scheduled. Note: non-workload is considered as duplicated
502502
// even if scheduling type is divided.

0 commit comments

Comments
 (0)