Skip to content

Commit 0f7becb

Browse files
authored
Merge pull request kubernetes#130537 from saintube/scheduler-expose-nodeinfo-in-score
Expose NodeInfo to the ScorePlugin
2 parents 07e65da + afb4e96 commit 0f7becb

27 files changed

+138
-105
lines changed

pkg/scheduler/framework/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ type ScorePlugin interface {
609609
// Score is called on each filtered node. It must return success and an integer
610610
// indicating the rank of the node. All scoring plugins must return success or
611611
// the pod will be rejected.
612-
Score(ctx context.Context, state *CycleState, p *v1.Pod, nodeName string) (int64, *Status)
612+
Score(ctx context.Context, state *CycleState, p *v1.Pod, nodeInfo *NodeInfo) (int64, *Status)
613613

614614
// ScoreExtensions returns a ScoreExtensions interface if it implements one, or nil if does not.
615615
ScoreExtensions() ScoreExtensions

pkg/scheduler/framework/plugins/imagelocality/image_locality.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package imagelocality
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"strings"
2322

2423
v1 "k8s.io/api/core/v1"
@@ -51,12 +50,7 @@ func (pl *ImageLocality) Name() string {
5150
}
5251

5352
// Score invoked at the score extension point.
54-
func (pl *ImageLocality) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
55-
nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
56-
if err != nil {
57-
return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err))
58-
}
59-
53+
func (pl *ImageLocality) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) {
6054
nodeInfos, err := pl.handle.SnapshotSharedLister().NodeInfos().List()
6155
if err != nil {
6256
return 0, framework.AsStatus(err)

pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,14 @@ func TestImageLocalityPriority(t *testing.T) {
374374
var gotList framework.NodeScoreList
375375
for _, n := range test.nodes {
376376
nodeName := n.ObjectMeta.Name
377-
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName)
377+
// Currently, we use the snapshot instead of the tf.BuildNodeInfos to build the nodeInfo since some
378+
// fields like ImageStates is essential for the Score plugin but the latter does not construct that.
379+
// We should enhance the BuildNodeInfos to achieve feature parity with the core logic.
380+
nodeInfo, err := snapshot.NodeInfos().Get(nodeName)
381+
if err != nil {
382+
t.Errorf("failed to get node %q from snapshot: %v", nodeName, err)
383+
}
384+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo)
378385
if !status.IsSuccess() {
379386
t.Errorf("unexpected error: %v", status)
380387
}

pkg/scheduler/framework/plugins/interpodaffinity/scoring.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,7 @@ func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error)
236236
// The "score" returned in this function is the sum of weights got from cycleState which have its topologyKey matching with the node's labels.
237237
// it is normalized later.
238238
// Note: the returned "score" is positive for pod-affinity, and negative for pod-antiaffinity.
239-
func (pl *InterPodAffinity) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
240-
nodeInfo, err := pl.sharedLister.NodeInfos().Get(nodeName)
241-
if err != nil {
242-
return 0, framework.AsStatus(fmt.Errorf("failed to get node %q from Snapshot: %w", nodeName, err))
243-
}
239+
func (pl *InterPodAffinity) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) {
244240
node := nodeInfo.Node()
245241

246242
s, err := getPreScoreState(cycleState)

pkg/scheduler/framework/plugins/interpodaffinity/scoring_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ func TestPreferredAffinity(t *testing.T) {
786786
defer cancel()
787787
state := framework.NewCycleState()
788788
p := plugintesting.SetupPluginWithInformers(ctx, t, schedruntime.FactoryAdapter(feature.Features{}, New), &config.InterPodAffinityArgs{HardPodAffinityWeight: 1, IgnorePreferredTermsOfExistingPods: test.ignorePreferredTermsOfExistingPods}, cache.NewSnapshot(test.pods, test.nodes), namespaces)
789-
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes))
789+
nodeInfos := tf.BuildNodeInfos(test.nodes)
790+
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, nodeInfos)
790791

791792
if !status.IsSuccess() {
792793
if status.Code() != test.wantStatus.Code() {
@@ -800,9 +801,9 @@ func TestPreferredAffinity(t *testing.T) {
800801
}
801802

802803
var gotList framework.NodeScoreList
803-
for _, n := range test.nodes {
804-
nodeName := n.ObjectMeta.Name
805-
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName)
804+
for _, nodeInfo := range nodeInfos {
805+
nodeName := nodeInfo.Node().Name
806+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo)
806807
if !status.IsSuccess() {
807808
t.Errorf("unexpected error from Score: %v", status)
808809
}
@@ -954,7 +955,8 @@ func TestPreferredAffinityWithHardPodAffinitySymmetricWeight(t *testing.T) {
954955
defer cancel()
955956
state := framework.NewCycleState()
956957
p := plugintesting.SetupPluginWithInformers(ctx, t, schedruntime.FactoryAdapter(feature.Features{}, New), &config.InterPodAffinityArgs{HardPodAffinityWeight: test.hardPodAffinityWeight}, cache.NewSnapshot(test.pods, test.nodes), namespaces)
957-
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes))
958+
nodeInfos := tf.BuildNodeInfos(test.nodes)
959+
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, nodeInfos)
958960
if !test.wantStatus.Equal(status) {
959961
t.Errorf("InterPodAffinity#PreScore() returned unexpected status.Code got: %v, want: %v", status.Code(), test.wantStatus.Code())
960962
}
@@ -963,9 +965,13 @@ func TestPreferredAffinityWithHardPodAffinitySymmetricWeight(t *testing.T) {
963965
}
964966

965967
var gotList framework.NodeScoreList
966-
for _, n := range test.nodes {
967-
nodeName := n.ObjectMeta.Name
968-
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName)
968+
for _, nodeInfo := range nodeInfos {
969+
nodeName := nodeInfo.Node().Name
970+
nodeInfo, err := p.(*InterPodAffinity).sharedLister.NodeInfos().Get(nodeName)
971+
if err != nil {
972+
t.Errorf("failed to get node %q from snapshot: %v", nodeName, err)
973+
}
974+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo)
969975
if !status.IsSuccess() {
970976
t.Errorf("unexpected error: %v", status)
971977
}

pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,7 @@ func (pl *NodeAffinity) PreScore(ctx context.Context, cycleState *framework.Cycl
256256
// Score returns the sum of the weights of the terms that match the Node.
257257
// Terms came from the Pod .spec.affinity.nodeAffinity and from the plugin's
258258
// default affinity.
259-
func (pl *NodeAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
260-
nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
261-
if err != nil {
262-
return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err))
263-
}
264-
259+
func (pl *NodeAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) {
265260
node := nodeInfo.Node()
266261

267262
var count int64

pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,10 @@ func TestNodeAffinityPriority(t *testing.T) {
12121212
}
12131213
}
12141214
var gotList framework.NodeScoreList
1215-
for _, n := range test.nodes {
1216-
nodeName := n.ObjectMeta.Name
1217-
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName)
1215+
nodeInfos := tf.BuildNodeInfos(test.nodes)
1216+
for _, nodeInfo := range nodeInfos {
1217+
nodeName := nodeInfo.Node().Name
1218+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo)
12181219
if !status.IsSuccess() {
12191220
t.Errorf("unexpected error: %v", status)
12201221
}

pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ func (ba *BalancedAllocation) Name() string {
8989
}
9090

9191
// Score invoked at the score extension point.
92-
func (ba *BalancedAllocation) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
93-
nodeInfo, err := ba.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
94-
if err != nil {
95-
return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err))
96-
}
97-
92+
func (ba *BalancedAllocation) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) {
9893
s, err := getBalancedAllocationPreScoreState(state)
9994
if err != nil {
10095
s = &balancedAllocationPreScoreState{podRequests: ba.calculatePodResourceRequestList(pod, ba.resources)}

pkg/scheduler/framework/plugins/noderesources/balanced_allocation_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,11 @@ func TestNodeResourcesBalancedAllocation(t *testing.T) {
399399
t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status)
400400
}
401401
}
402-
hostResult, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, test.nodes[i].Name)
402+
nodeInfo, err := snapshot.Get(test.nodes[i].Name)
403+
if err != nil {
404+
t.Errorf("failed to get node %q from snapshot: %v", test.nodes[i].Name, err)
405+
}
406+
hostResult, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo)
403407
if !status.IsSuccess() {
404408
t.Errorf("Score is expected to return success, but didn't. Got status: %v", status)
405409
}

pkg/scheduler/framework/plugins/noderesources/fit.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,7 @@ func fitsRequest(podRequest *preFilterState, nodeInfo *framework.NodeInfo, ignor
579579
}
580580

581581
// Score invoked at the Score extension point.
582-
func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
583-
nodeInfo, err := f.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
584-
if err != nil {
585-
return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err))
586-
}
587-
582+
func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) {
588583
s, err := getPreScoreState(state)
589584
if err != nil {
590585
s = &preScoreState{

0 commit comments

Comments
 (0)