Skip to content

Commit 773dcf0

Browse files
authored
Merge pull request #7394 from vie-serendipity/fix/event
fix: record schedule result correctly in the event
2 parents e918fb1 + da13584 commit 773dcf0

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

pkg/scheduler/scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func (s *Scheduler) scheduleResourceBindingWithClusterAffinities(rb *workv1alpha
660660
patchErr := s.patchScheduleResultForResourceBinding(rb, string(placementBytes), scheduleResult.SuggestedClusters)
661661
patchStatusErr := patchBindingStatusWithAffinityName(s.KarmadaClient, rb, updatedStatus.SchedulerObservedAffinityName)
662662
scheduleErr := utilerrors.NewAggregate([]error{patchErr, patchStatusErr})
663-
s.recordScheduleResultEventForResourceBinding(rb, nil, scheduleErr)
663+
s.recordScheduleResultEventForResourceBinding(rb, scheduleResult.SuggestedClusters, scheduleErr)
664664
return scheduleErr
665665
}
666666

@@ -803,7 +803,7 @@ func (s *Scheduler) scheduleClusterResourceBindingWithClusterAffinities(crb *wor
803803
patchErr := s.patchScheduleResultForClusterResourceBinding(crb, string(placementBytes), scheduleResult.SuggestedClusters)
804804
patchStatusErr := patchClusterBindingStatusWithAffinityName(s.KarmadaClient, crb, updatedStatus.SchedulerObservedAffinityName)
805805
scheduleErr := utilerrors.NewAggregate([]error{patchErr, patchStatusErr})
806-
s.recordScheduleResultEventForClusterResourceBinding(crb, nil, scheduleErr)
806+
s.recordScheduleResultEventForClusterResourceBinding(crb, scheduleResult.SuggestedClusters, scheduleErr)
807807
return scheduleErr
808808
}
809809

pkg/scheduler/scheduler_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"reflect"
24-
"strings"
2524
"testing"
2625
"time"
2726

@@ -569,7 +568,7 @@ func TestScheduleResourceBindingWithClusterAffinities(t *testing.T) {
569568
`{"metadata":{"annotations":{"policy.karmada.io/applied-placement":"{\"clusterAffinities\":[{\"affinityName\":\"affinity1\",\"clusterNames\":[\"cluster1\"]},{\"affinityName\":\"affinity2\",\"clusterNames\":[\"cluster2\"]}]}"}},"spec":{"clusters":[{"name":"cluster1","replicas":1}]}}`,
570569
`{"status":{"schedulerObservingAffinityName":"affinity1"}}`,
571570
},
572-
expectedEvent: "Normal ScheduleBindingSucceed Binding has been scheduled successfully.",
571+
expectedEvent: fmt.Sprintf("Normal ScheduleBindingSucceed %s Result: {cluster1:1}", successfulSchedulingMessage),
573572
},
574573
{
575574
name: "successful scheduling with second affinity",
@@ -688,11 +687,7 @@ func TestScheduleResourceBindingWithClusterAffinities(t *testing.T) {
688687
// Check if an event was recorded
689688
select {
690689
case event := <-fakeRecorder.Events:
691-
if strings.Contains(event, "ScheduleBindingFailed") {
692-
assert.Contains(t, event, tt.expectedEvent, "Event does not match expected")
693-
} else {
694-
assert.Contains(t, event, "ScheduleBindingSucceed", "Expected ScheduleBindingSucceed event")
695-
}
690+
assert.Contains(t, event, tt.expectedEvent, "Event does not match expected")
696691
default:
697692
t.Errorf("Expected an event to be recorded")
698693
}
@@ -900,6 +895,7 @@ func TestScheduleClusterResourceBindingWithClusterAffinities(t *testing.T) {
900895
scheduleResults []core.ScheduleResult
901896
scheduleErrors []error
902897
expectError bool
898+
expectedEvent string
903899
}{
904900
{
905901
name: "successful scheduling with first affinity",
@@ -935,6 +931,7 @@ func TestScheduleClusterResourceBindingWithClusterAffinities(t *testing.T) {
935931
},
936932
scheduleErrors: []error{nil},
937933
expectError: false,
934+
expectedEvent: fmt.Sprintf("Normal ScheduleBindingSucceed %s Result {cluster1:1}", successfulSchedulingMessage),
938935
},
939936
{
940937
name: "successful scheduling with second affinity",
@@ -971,6 +968,7 @@ func TestScheduleClusterResourceBindingWithClusterAffinities(t *testing.T) {
971968
},
972969
scheduleErrors: []error{errors.New("first affinity failed"), nil},
973970
expectError: false,
971+
expectedEvent: "Warning ScheduleBindingFailed failed to schedule ClusterResourceBinding(test-cluster-binding-2) with clusterAffiliates index(0): first affinity failed",
974972
},
975973
{
976974
name: "all affinities fail",
@@ -1000,6 +998,7 @@ func TestScheduleClusterResourceBindingWithClusterAffinities(t *testing.T) {
1000998
scheduleResults: []core.ScheduleResult{{}, {}},
1001999
scheduleErrors: []error{errors.New("first affinity failed"), errors.New("second affinity failed")},
10021000
expectError: true,
1001+
expectedEvent: "Warning ScheduleBindingFailed failed to schedule ClusterResourceBinding(test-cluster-binding-fail) with clusterAffiliates index(0): first affinity failed",
10031002
},
10041003
}
10051004

@@ -1027,6 +1026,14 @@ func TestScheduleClusterResourceBindingWithClusterAffinities(t *testing.T) {
10271026
if (err != nil) != tt.expectError {
10281027
t.Errorf("scheduleClusterResourceBindingWithClusterAffinities() error = %v, expectError %v", err, tt.expectError)
10291028
}
1029+
1030+
// Check if an event was recorded
1031+
select {
1032+
case event := <-fakeRecorder.Events:
1033+
assert.Contains(t, event, tt.expectedEvent, "Event does not match expected")
1034+
default:
1035+
t.Errorf("Expected an event to be recorded")
1036+
}
10301037
})
10311038
}
10321039
}

0 commit comments

Comments
 (0)