From 2f180f26cd7cf5210626776690de3443648c3686 Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 6 Jan 2025 17:24:31 +0800 Subject: [PATCH] Add ResourceBinding scheduling suspension helper functions to simplify code Signed-off-by: RainbowMango --- .../work/v1alpha2/binding_types_helper.go | 10 ++ .../v1alpha2/binding_types_helper_test.go | 57 +++++++++ pkg/scheduler/event_handler.go | 4 +- pkg/util/binding.go | 16 --- pkg/util/binding_test.go | 114 ------------------ 5 files changed, 69 insertions(+), 132 deletions(-) diff --git a/pkg/apis/work/v1alpha2/binding_types_helper.go b/pkg/apis/work/v1alpha2/binding_types_helper.go index cd0c3cc19..e89723c20 100644 --- a/pkg/apis/work/v1alpha2/binding_types_helper.go +++ b/pkg/apis/work/v1alpha2/binding_types_helper.go @@ -194,3 +194,13 @@ func (s *ResourceBindingSpec) GracefulEvictCluster(name string, options *TaskOpt } s.GracefulEvictionTasks = append(s.GracefulEvictionTasks, evictionTask) } + +// SchedulingSuspended tells if the scheduling of ResourceBinding or +// ClusterResourceBinding is suspended. +func (s *ResourceBindingSpec) SchedulingSuspended() bool { + if s == nil || s.Suspension == nil || s.Suspension.Scheduling == nil { + return false + } + + return *s.Suspension.Scheduling +} diff --git a/pkg/apis/work/v1alpha2/binding_types_helper_test.go b/pkg/apis/work/v1alpha2/binding_types_helper_test.go index 7db5aecda..e088fb95c 100644 --- a/pkg/apis/work/v1alpha2/binding_types_helper_test.go +++ b/pkg/apis/work/v1alpha2/binding_types_helper_test.go @@ -384,3 +384,60 @@ func TestResourceBindingSpec_ClusterInGracefulEvictionTasks(t *testing.T) { }) } } + +func TestResourceBindingSpec_SchedulingSuspended(t *testing.T) { + tests := []struct { + name string + rbSpec *ResourceBindingSpec + Suspended bool + }{ + { + name: "nil ResourceBindingSpec results in not suspended", + rbSpec: nil, + Suspended: false, + }, + { + name: "nil Suspension results in not suspended", + rbSpec: &ResourceBindingSpec{ + Suspension: nil, + }, + Suspended: false, + }, + { + name: "nil Scheduling results in not suspended", + rbSpec: &ResourceBindingSpec{ + Suspension: &Suspension{ + Scheduling: nil, + }, + }, + Suspended: false, + }, + { + name: "false Scheduling results in not suspended", + rbSpec: &ResourceBindingSpec{ + Suspension: &Suspension{ + Scheduling: ptr.To(false), + }, + }, + Suspended: false, + }, + { + name: "true Scheduling results in suspended", + rbSpec: &ResourceBindingSpec{ + Suspension: &Suspension{ + Scheduling: ptr.To(true), + }, + }, + Suspended: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + suspended := tc.rbSpec.SchedulingSuspended() + if suspended != tc.Suspended { + t.Fatalf("SchedulingSuspended(): expected: %t, but got: %t", tc.Suspended, suspended) + } + }) + } +} diff --git a/pkg/scheduler/event_handler.go b/pkg/scheduler/event_handler.go index a37af1f02..b136aabd6 100644 --- a/pkg/scheduler/event_handler.go +++ b/pkg/scheduler/event_handler.go @@ -101,14 +101,14 @@ func (s *Scheduler) resourceBindingEventFilter(obj interface{}) bool { if !schedulerNameFilter(s.schedulerName, t.Spec.SchedulerName) { return false } - if util.IsBindingSuspendScheduling(t) { + if t.Spec.SchedulingSuspended() { return false } case *workv1alpha2.ClusterResourceBinding: if !schedulerNameFilter(s.schedulerName, t.Spec.SchedulerName) { return false } - if util.IsClusterBindingSuspendScheduling(t) { + if t.Spec.SchedulingSuspended() { return false } } diff --git a/pkg/util/binding.go b/pkg/util/binding.go index ba4631ab6..6811984f0 100644 --- a/pkg/util/binding.go +++ b/pkg/util/binding.go @@ -110,19 +110,3 @@ func RescheduleRequired(rescheduleTriggeredAt, lastScheduledTime *metav1.Time) b } return rescheduleTriggeredAt.After(lastScheduledTime.Time) } - -// IsBindingSuspendScheduling tells whether resource binding is scheduling suspended. -func IsBindingSuspendScheduling(rb *workv1alpha2.ResourceBinding) bool { - if rb == nil || rb.Spec.Suspension == nil || rb.Spec.Suspension.Scheduling == nil { - return false - } - return *rb.Spec.Suspension.Scheduling -} - -// IsClusterBindingSuspendScheduling tells whether cluster resource binding is scheduling suspended. -func IsClusterBindingSuspendScheduling(crb *workv1alpha2.ClusterResourceBinding) bool { - if crb == nil || crb.Spec.Suspension == nil || crb.Spec.Suspension.Scheduling == nil { - return false - } - return *crb.Spec.Suspension.Scheduling -} diff --git a/pkg/util/binding_test.go b/pkg/util/binding_test.go index 48ac8790d..79355978b 100644 --- a/pkg/util/binding_test.go +++ b/pkg/util/binding_test.go @@ -21,10 +21,8 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/utils/ptr" policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" @@ -421,115 +419,3 @@ func TestRescheduleRequired(t *testing.T) { }) } } - -func TestIsBindingSuspendScheduling(t *testing.T) { - tests := []struct { - name string - rb *workv1alpha2.ResourceBinding - expected bool - }{ - { - name: "rb is nil", - rb: nil, - expected: false, - }, - { - name: "rb.Spec.Suspension is nil", - rb: &workv1alpha2.ResourceBinding{}, - expected: false, - }, - { - name: "rb.Spec.Suspension.Scheduling is nil", - rb: &workv1alpha2.ResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{}, - }, - }, - expected: false, - }, - { - name: "rb.Spec.Suspension.Scheduling is false", - rb: &workv1alpha2.ResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{ - Scheduling: ptr.To(false), - }, - }, - }, - expected: false, - }, - { - name: "rb.Spec.Suspension.Scheduling is true", - rb: &workv1alpha2.ResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{ - Scheduling: ptr.To(true), - }, - }, - }, - expected: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.expected, IsBindingSuspendScheduling(tt.rb)) - }) - } -} - -func TestIsClusterBindingSuspendScheduling(t *testing.T) { - tests := []struct { - name string - crb *workv1alpha2.ClusterResourceBinding - expected bool - }{ - { - name: "crb is nil", - crb: nil, - expected: false, - }, - { - name: "crb.Spec.Suspension is nil", - crb: &workv1alpha2.ClusterResourceBinding{}, - expected: false, - }, - { - name: "crb.Spec.Suspension.Scheduling is nil", - crb: &workv1alpha2.ClusterResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{}, - }, - }, - expected: false, - }, - { - name: "crb.Spec.Suspension.Scheduling is false", - crb: &workv1alpha2.ClusterResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{ - Scheduling: ptr.To(false), - }, - }, - }, - expected: false, - }, - { - name: "crb.Spec.Suspension.Scheduling is true", - crb: &workv1alpha2.ClusterResourceBinding{ - Spec: workv1alpha2.ResourceBindingSpec{ - Suspension: &workv1alpha2.Suspension{ - Scheduling: ptr.To(true), - }, - }, - }, - expected: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.expected, IsClusterBindingSuspendScheduling(tt.crb)) - }) - } -}