Add ResourceBinding scheduling suspension helper functions to simplify code
Signed-off-by: RainbowMango <qdurenhongcai@gmail.com>
This commit is contained in:
parent
0d938d0270
commit
2f180f26cd
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue