Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResourceBinding scheduling suspension helper function #6019

Merged
Merged
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
10 changes: 10 additions & 0 deletions pkg/apis/work/v1alpha2/binding_types_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
57 changes: 57 additions & 0 deletions pkg/apis/work/v1alpha2/binding_types_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/scheduler/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/util/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
114 changes: 0 additions & 114 deletions pkg/util/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
})
}
}