diff --git a/pkg/scheduler/framework/interface.go b/pkg/scheduler/framework/interface.go index cf6f39c6748..10ee4203d7d 100644 --- a/pkg/scheduler/framework/interface.go +++ b/pkg/scheduler/framework/interface.go @@ -609,7 +609,7 @@ type ScorePlugin interface { // Score is called on each filtered node. It must return success and an integer // indicating the rank of the node. All scoring plugins must return success or // the pod will be rejected. - Score(ctx context.Context, state *CycleState, p *v1.Pod, nodeName string) (int64, *Status) + Score(ctx context.Context, state *CycleState, p *v1.Pod, nodeInfo *NodeInfo) (int64, *Status) // ScoreExtensions returns a ScoreExtensions interface if it implements one, or nil if does not. ScoreExtensions() ScoreExtensions diff --git a/pkg/scheduler/framework/plugins/imagelocality/image_locality.go b/pkg/scheduler/framework/plugins/imagelocality/image_locality.go index 3b0a7722960..694d457c754 100644 --- a/pkg/scheduler/framework/plugins/imagelocality/image_locality.go +++ b/pkg/scheduler/framework/plugins/imagelocality/image_locality.go @@ -18,7 +18,6 @@ package imagelocality import ( "context" - "fmt" "strings" v1 "k8s.io/api/core/v1" @@ -51,12 +50,7 @@ func (pl *ImageLocality) Name() string { } // Score invoked at the score extension point. -func (pl *ImageLocality) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } - +func (pl *ImageLocality) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { nodeInfos, err := pl.handle.SnapshotSharedLister().NodeInfos().List() if err != nil { return 0, framework.AsStatus(err) diff --git a/pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go b/pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go index 473c3779fa1..8813c20c8fe 100644 --- a/pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go +++ b/pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go @@ -374,7 +374,14 @@ func TestImageLocalityPriority(t *testing.T) { var gotList framework.NodeScoreList for _, n := range test.nodes { nodeName := n.ObjectMeta.Name - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName) + // Currently, we use the snapshot instead of the tf.BuildNodeInfos to build the nodeInfo since some + // fields like ImageStates is essential for the Score plugin but the latter does not construct that. + // We should enhance the BuildNodeInfos to achieve feature parity with the core logic. + nodeInfo, err := snapshot.NodeInfos().Get(nodeName) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", nodeName, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go b/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go index c3490821519..126f54a472b 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go @@ -236,11 +236,7 @@ func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) // The "score" returned in this function is the sum of weights got from cycleState which have its topologyKey matching with the node's labels. // it is normalized later. // Note: the returned "score" is positive for pod-affinity, and negative for pod-antiaffinity. -func (pl *InterPodAffinity) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := pl.sharedLister.NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("failed to get node %q from Snapshot: %w", nodeName, err)) - } +func (pl *InterPodAffinity) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { node := nodeInfo.Node() s, err := getPreScoreState(cycleState) diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/scoring_test.go b/pkg/scheduler/framework/plugins/interpodaffinity/scoring_test.go index 3e6f4d23634..f5c510b6ea7 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/scoring_test.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/scoring_test.go @@ -786,7 +786,8 @@ func TestPreferredAffinity(t *testing.T) { defer cancel() state := framework.NewCycleState() p := plugintesting.SetupPluginWithInformers(ctx, t, schedruntime.FactoryAdapter(feature.Features{}, New), &config.InterPodAffinityArgs{HardPodAffinityWeight: 1, IgnorePreferredTermsOfExistingPods: test.ignorePreferredTermsOfExistingPods}, cache.NewSnapshot(test.pods, test.nodes), namespaces) - status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes)) + nodeInfos := tf.BuildNodeInfos(test.nodes) + status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, nodeInfos) if !status.IsSuccess() { if status.Code() != test.wantStatus.Code() { @@ -800,9 +801,9 @@ func TestPreferredAffinity(t *testing.T) { } var gotList framework.NodeScoreList - for _, n := range test.nodes { - nodeName := n.ObjectMeta.Name - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName) + for _, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error from Score: %v", status) } @@ -954,7 +955,8 @@ func TestPreferredAffinityWithHardPodAffinitySymmetricWeight(t *testing.T) { defer cancel() state := framework.NewCycleState() p := plugintesting.SetupPluginWithInformers(ctx, t, schedruntime.FactoryAdapter(feature.Features{}, New), &config.InterPodAffinityArgs{HardPodAffinityWeight: test.hardPodAffinityWeight}, cache.NewSnapshot(test.pods, test.nodes), namespaces) - status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes)) + nodeInfos := tf.BuildNodeInfos(test.nodes) + status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, nodeInfos) if !test.wantStatus.Equal(status) { t.Errorf("InterPodAffinity#PreScore() returned unexpected status.Code got: %v, want: %v", status.Code(), test.wantStatus.Code()) } @@ -963,9 +965,13 @@ func TestPreferredAffinityWithHardPodAffinitySymmetricWeight(t *testing.T) { } var gotList framework.NodeScoreList - for _, n := range test.nodes { - nodeName := n.ObjectMeta.Name - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName) + for _, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + nodeInfo, err := p.(*InterPodAffinity).sharedLister.NodeInfos().Get(nodeName) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", nodeName, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } diff --git a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go index b123d9e2356..f63efb61616 100644 --- a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go +++ b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go @@ -256,12 +256,7 @@ func (pl *NodeAffinity) PreScore(ctx context.Context, cycleState *framework.Cycl // Score returns the sum of the weights of the terms that match the Node. // Terms came from the Pod .spec.affinity.nodeAffinity and from the plugin's // default affinity. -func (pl *NodeAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } - +func (pl *NodeAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { node := nodeInfo.Node() var count int64 diff --git a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go index 39a0ece58ac..1c486381503 100644 --- a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go +++ b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go @@ -1212,9 +1212,10 @@ func TestNodeAffinityPriority(t *testing.T) { } } var gotList framework.NodeScoreList - for _, n := range test.nodes { - nodeName := n.ObjectMeta.Name - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName) + nodeInfos := tf.BuildNodeInfos(test.nodes) + for _, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } diff --git a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go index a0634848400..9162758da48 100644 --- a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go +++ b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go @@ -89,12 +89,7 @@ func (ba *BalancedAllocation) Name() string { } // Score invoked at the score extension point. -func (ba *BalancedAllocation) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := ba.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } - +func (ba *BalancedAllocation) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { s, err := getBalancedAllocationPreScoreState(state) if err != nil { s = &balancedAllocationPreScoreState{podRequests: ba.calculatePodResourceRequestList(pod, ba.resources)} diff --git a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation_test.go b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation_test.go index b89f1d98f1a..cc7060fc365 100644 --- a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation_test.go @@ -399,7 +399,11 @@ func TestNodeResourcesBalancedAllocation(t *testing.T) { t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status) } } - hostResult, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, test.nodes[i].Name) + nodeInfo, err := snapshot.Get(test.nodes[i].Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", test.nodes[i].Name, err) + } + hostResult, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score is expected to return success, but didn't. Got status: %v", status) } diff --git a/pkg/scheduler/framework/plugins/noderesources/fit.go b/pkg/scheduler/framework/plugins/noderesources/fit.go index 4bf7943e4a7..ef6c2244c6b 100644 --- a/pkg/scheduler/framework/plugins/noderesources/fit.go +++ b/pkg/scheduler/framework/plugins/noderesources/fit.go @@ -579,12 +579,7 @@ func fitsRequest(podRequest *preFilterState, nodeInfo *framework.NodeInfo, ignor } // Score invoked at the Score extension point. -func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := f.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } - +func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { s, err := getPreScoreState(state) if err != nil { s = &preScoreState{ diff --git a/pkg/scheduler/framework/plugins/noderesources/fit_test.go b/pkg/scheduler/framework/plugins/noderesources/fit_test.go index b0d56a63f85..84727423ca4 100644 --- a/pkg/scheduler/framework/plugins/noderesources/fit_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/fit_test.go @@ -1106,7 +1106,11 @@ func TestFitScore(t *testing.T) { t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status) } } - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score is expected to return success, but didn't. Got status: %v", status) } @@ -1221,12 +1225,16 @@ func BenchmarkTestFitScore(b *testing.B) { var nodeResourcesFunc = runtime.FactoryAdapter(plfeature.Features{}, NewFit) pl := plugintesting.SetupPlugin(ctx, b, nodeResourcesFunc, &test.nodeResourcesFitArgs, cache.NewSnapshot(existingPods, nodes)) p := pl.(*Fit) + nodeInfo, err := p.handle.SnapshotSharedLister().NodeInfos().Get(nodes[0].Name) + if err != nil { + b.Errorf("failed to get node %q from snapshot: %v", nodes[0].Name, err) + } b.ResetTimer() requestedPod := st.MakePod().Req(map[v1.ResourceName]string{"cpu": "1000", "memory": "2000"}).Obj() for i := 0; i < b.N; i++ { - _, status := p.Score(ctx, state, requestedPod, nodes[0].Name) + _, status := p.Score(ctx, state, requestedPod, nodeInfo) if !status.IsSuccess() { b.Errorf("unexpected status: %v", status) } diff --git a/pkg/scheduler/framework/plugins/noderesources/least_allocated_test.go b/pkg/scheduler/framework/plugins/noderesources/least_allocated_test.go index 540a8ad1df0..004bcce2e58 100644 --- a/pkg/scheduler/framework/plugins/noderesources/least_allocated_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/least_allocated_test.go @@ -419,7 +419,11 @@ func TestLeastAllocatedScoringStrategy(t *testing.T) { var gotScores framework.NodeScoreList for _, n := range test.nodes { - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, nodeInfo) if status.Code() != test.wantStatusCode { t.Errorf("unexpected status code, want: %v, got: %v", test.wantStatusCode, status.Code()) } diff --git a/pkg/scheduler/framework/plugins/noderesources/most_allocated_test.go b/pkg/scheduler/framework/plugins/noderesources/most_allocated_test.go index 8d1f134d6e1..39b8232a319 100644 --- a/pkg/scheduler/framework/plugins/noderesources/most_allocated_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/most_allocated_test.go @@ -375,7 +375,11 @@ func TestMostAllocatedScoringStrategy(t *testing.T) { var gotScores framework.NodeScoreList for _, n := range test.nodes { - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, nodeInfo) if status.Code() != test.wantStatusCode { t.Errorf("unexpected status code, want: %v, got: %v", test.wantStatusCode, status.Code()) } diff --git a/pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio_test.go b/pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio_test.go index 15fa3eb60ae..b168811db1f 100644 --- a/pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio_test.go @@ -136,7 +136,11 @@ func TestRequestedToCapacityRatioScoringStrategy(t *testing.T) { if !status.IsSuccess() { t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status) } - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.requestedPod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score is expected to return success, but didn't. Got status: %v", status) } @@ -333,7 +337,11 @@ func TestResourceBinPackingSingleExtended(t *testing.T) { if !status.IsSuccess() { t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status) } - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score is expected to return success, but didn't. Got status: %v", status) } @@ -562,7 +570,11 @@ func TestResourceBinPackingMultipleExtended(t *testing.T) { var gotScores framework.NodeScoreList for _, n := range test.nodes { - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, n.Name) + nodeInfo, err := snapshot.Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score is expected to return success, but didn't. Got status: %v", status) } diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go index d18dd3b6a9b..ed8a9542f44 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go @@ -192,12 +192,7 @@ func (pl *PodTopologySpread) PreScore( // Score invoked at the Score extension point. // The "score" returned in this function is the matching number of pods on the `nodeName`, // it is normalized later. -func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := pl.sharedLister.NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } - +func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { node := nodeInfo.Node() s, err := getPreScoreState(cycleState) if err != nil { diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go b/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go index 3752e2269ea..f783c77ad2e 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go @@ -1396,7 +1396,11 @@ func TestPodTopologySpreadScore(t *testing.T) { var gotList framework.NodeScoreList for _, n := range tt.nodes { nodeName := n.Name - score, status := p.Score(ctx, state, tt.pod, nodeName) + nodeInfo, err := p.sharedLister.NodeInfos().Get(n.Name) + if err != nil { + t.Errorf("failed to get node %q from snapshot: %v", n.Name, err) + } + score, status := p.Score(ctx, state, tt.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } @@ -1463,13 +1467,21 @@ func BenchmarkTestPodTopologySpreadScore(b *testing.B) { if !status.IsSuccess() { b.Fatalf("unexpected error: %v", status) } + nodeInfos := make([]*framework.NodeInfo, len(filteredNodes)) + for i, n := range filteredNodes { + nodeInfo, err := p.sharedLister.NodeInfos().Get(n.Name) + if err != nil { + b.Fatalf("failed to get node %q from snapshot: %v", n.Name, err) + } + nodeInfos[i] = nodeInfo + } b.ResetTimer() for i := 0; i < b.N; i++ { var gotList framework.NodeScoreList - for _, n := range filteredNodes { - nodeName := n.Name - score, status := p.Score(ctx, state, tt.pod, nodeName) + for _, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + score, status := p.Score(ctx, state, tt.pod, nodeInfo) if !status.IsSuccess() { b.Fatalf("unexpected error: %v", status) } diff --git a/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go index af9cb74d8c8..3eb8f5e1a2f 100644 --- a/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go +++ b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go @@ -180,11 +180,7 @@ func countIntolerableTaintsPreferNoSchedule(taints []v1.Taint, tolerations []v1. } // Score invoked at the Score extension point. -func (pl *TaintToleration) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { - nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) - if err != nil { - return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err)) - } +func (pl *TaintToleration) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { node := nodeInfo.Node() s, err := getPreScoreState(state) diff --git a/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration_test.go b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration_test.go index 73b240906c2..654f17fe572 100644 --- a/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration_test.go +++ b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration_test.go @@ -243,14 +243,15 @@ func TestTaintTolerationScore(t *testing.T) { if err != nil { t.Fatalf("creating plugin: %v", err) } - status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes)) + nodeInfos := tf.BuildNodeInfos(test.nodes) + status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, nodeInfos) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } var gotList framework.NodeScoreList - for _, n := range test.nodes { - nodeName := n.ObjectMeta.Name - score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeName) + for _, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("unexpected error: %v", status) } diff --git a/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go b/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go index bdf096c226f..6738384d5fd 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go +++ b/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go @@ -458,7 +458,7 @@ func (pl *VolumeBinding) PreScore(ctx context.Context, cs *framework.CycleState, } // Score invoked at the score extension point. -func (pl *VolumeBinding) Score(ctx context.Context, cs *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *VolumeBinding) Score(ctx context.Context, cs *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { if pl.scorer == nil { return 0, nil } @@ -466,6 +466,7 @@ func (pl *VolumeBinding) Score(ctx context.Context, cs *framework.CycleState, po if err != nil { return 0, framework.AsStatus(err) } + nodeName := nodeInfo.Node().Name podVolumes, ok := state.podVolumesByNode[nodeName] if !ok { return 0, nil diff --git a/pkg/scheduler/framework/plugins/volumebinding/volume_binding_test.go b/pkg/scheduler/framework/plugins/volumebinding/volume_binding_test.go index c7f0cb8ae81..00b2b1a4b8f 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/volume_binding_test.go +++ b/pkg/scheduler/framework/plugins/volumebinding/volume_binding_test.go @@ -773,12 +773,7 @@ func TestVolumeBinding(t *testing.T) { t.Log("Verify") p := pl.(*VolumeBinding) - nodeInfos := make([]*framework.NodeInfo, 0) - for _, node := range item.nodes { - nodeInfo := framework.NewNodeInfo() - nodeInfo.SetNode(node) - nodeInfos = append(nodeInfos, nodeInfo) - } + nodeInfos := tf.BuildNodeInfos(item.nodes) state := framework.NewCycleState() t.Logf("Verify: call PreFilter and check status") @@ -818,7 +813,7 @@ func TestVolumeBinding(t *testing.T) { } t.Logf("Verify: call PreScore and check status") - gotPreScoreStatus := p.PreScore(ctx, state, item.pod, tf.BuildNodeInfos(item.nodes)) + gotPreScoreStatus := p.PreScore(ctx, state, item.pod, nodeInfos) if diff := cmp.Diff(item.wantPreScoreStatus, gotPreScoreStatus); diff != "" { t.Errorf("state got after prescore does not match (-want,+got):\n%s", diff) } @@ -827,13 +822,14 @@ func TestVolumeBinding(t *testing.T) { } t.Logf("Verify: Score") - for i, node := range item.nodes { - score, status := p.Score(ctx, state, item.pod, node.Name) + for i, nodeInfo := range nodeInfos { + nodeName := nodeInfo.Node().Name + score, status := p.Score(ctx, state, item.pod, nodeInfo) if !status.IsSuccess() { t.Errorf("Score expects success status, got: %v", status) } if score != item.wantScores[i] { - t.Errorf("Score expects score %d for node %q, got: %d", item.wantScores[i], node.Name, score) + t.Errorf("Score expects score %d for node %q, got: %d", item.wantScores[i], nodeName, score) } } }) diff --git a/pkg/scheduler/framework/runtime/framework.go b/pkg/scheduler/framework/runtime/framework.go index 0fe2a02577d..dbdc6d0986e 100644 --- a/pkg/scheduler/framework/runtime/framework.go +++ b/pkg/scheduler/framework/runtime/framework.go @@ -1137,7 +1137,8 @@ func (f *frameworkImpl) RunScorePlugins(ctx context.Context, state *framework.Cy } // Run Score method for each node in parallel. f.Parallelizer().Until(ctx, len(nodes), func(index int) { - nodeName := nodes[index].Node().Name + nodeInfo := nodes[index] + nodeName := nodeInfo.Node().Name logger := logger if verboseLogs { logger = klog.LoggerWithValues(logger, "node", klog.ObjectRef{Name: nodeName}) @@ -1148,7 +1149,7 @@ func (f *frameworkImpl) RunScorePlugins(ctx context.Context, state *framework.Cy logger := klog.LoggerWithName(logger, pl.Name()) ctx = klog.NewContext(ctx, logger) } - s, status := f.runScorePlugin(ctx, pl, state, pod, nodeName) + s, status := f.runScorePlugin(ctx, pl, state, pod, nodeInfo) if !status.IsSuccess() { err := fmt.Errorf("plugin %q failed with: %w", pl.Name(), status.AsError()) errCh.SendErrorWithCancel(err, cancel) @@ -1217,12 +1218,12 @@ func (f *frameworkImpl) RunScorePlugins(ctx context.Context, state *framework.Cy return allNodePluginScores, nil } -func (f *frameworkImpl) runScorePlugin(ctx context.Context, pl framework.ScorePlugin, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { +func (f *frameworkImpl) runScorePlugin(ctx context.Context, pl framework.ScorePlugin, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { if !state.ShouldRecordPluginMetrics() { - return pl.Score(ctx, state, pod, nodeName) + return pl.Score(ctx, state, pod, nodeInfo) } startTime := time.Now() - s, status := pl.Score(ctx, state, pod, nodeName) + s, status := pl.Score(ctx, state, pod, nodeInfo) f.metricsRecorder.ObservePluginDurationAsync(metrics.Score, pl.Name(), status.Code().String(), metrics.SinceInSeconds(startTime)) return s, status } diff --git a/pkg/scheduler/framework/runtime/framework_test.go b/pkg/scheduler/framework/runtime/framework_test.go index 07a0310b8ee..aee8d55960b 100644 --- a/pkg/scheduler/framework/runtime/framework_test.go +++ b/pkg/scheduler/framework/runtime/framework_test.go @@ -132,7 +132,7 @@ func (pl *TestScoreWithNormalizePlugin) NormalizeScore(ctx context.Context, stat return injectNormalizeRes(pl.inj, scores) } -func (pl *TestScoreWithNormalizePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *TestScoreWithNormalizePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { return setScoreRes(pl.inj) } @@ -154,7 +154,7 @@ func (pl *TestScorePlugin) PreScore(ctx context.Context, state *framework.CycleS return framework.NewStatus(framework.Code(pl.inj.PreScoreStatus), injectReason) } -func (pl *TestScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *TestScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { return setScoreRes(pl.inj) } @@ -194,7 +194,7 @@ func (pl *TestPlugin) Less(*framework.QueuedPodInfo, *framework.QueuedPodInfo) b return false } -func (pl *TestPlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *TestPlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { return 0, framework.NewStatus(framework.Code(pl.inj.ScoreStatus), injectReason) } diff --git a/pkg/scheduler/framework/runtime/instrumented_plugins.go b/pkg/scheduler/framework/runtime/instrumented_plugins.go index f285ad00bb4..04e9004c8c6 100644 --- a/pkg/scheduler/framework/runtime/instrumented_plugins.go +++ b/pkg/scheduler/framework/runtime/instrumented_plugins.go @@ -77,7 +77,7 @@ type instrumentedScorePlugin struct { var _ framework.ScorePlugin = &instrumentedScorePlugin{} -func (p *instrumentedScorePlugin) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) { +func (p *instrumentedScorePlugin) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { p.metric.Inc() - return p.ScorePlugin.Score(ctx, state, pod, nodeName) + return p.ScorePlugin.Score(ctx, state, pod, nodeInfo) } diff --git a/pkg/scheduler/schedule_one_test.go b/pkg/scheduler/schedule_one_test.go index 5493319244c..8ed4d7bf098 100644 --- a/pkg/scheduler/schedule_one_test.go +++ b/pkg/scheduler/schedule_one_test.go @@ -174,7 +174,7 @@ func (pl *falseMapPlugin) Name() string { return "FalseMap" } -func (pl *falseMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) (int64, *framework.Status) { +func (pl *falseMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ *framework.NodeInfo) (int64, *framework.Status) { return 0, framework.AsStatus(errPrioritize) } @@ -194,7 +194,8 @@ func (pl *numericMapPlugin) Name() string { return "NumericMap" } -func (pl *numericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *numericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { + nodeName := nodeInfo.Node().Name score, err := strconv.Atoi(nodeName) if err != nil { return 0, framework.NewStatus(framework.Error, fmt.Sprintf("Error converting nodename to int: %+v", nodeName)) @@ -217,7 +218,8 @@ func (pl *reverseNumericMapPlugin) Name() string { return "ReverseNumericMap" } -func (pl *reverseNumericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *reverseNumericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { + nodeName := nodeInfo.Node().Name score, err := strconv.Atoi(nodeName) if err != nil { return 0, framework.NewStatus(framework.Error, fmt.Sprintf("Error converting nodename to int: %+v", nodeName)) @@ -258,7 +260,7 @@ func (pl *trueMapPlugin) Name() string { return "TrueMap" } -func (pl *trueMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) (int64, *framework.Status) { +func (pl *trueMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ *framework.NodeInfo) (int64, *framework.Status) { return 1, nil } @@ -372,7 +374,7 @@ func (t *TestPlugin) Name() string { return t.name } -func (t *TestPlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (t *TestPlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { return 1, nil } @@ -3001,8 +3003,12 @@ func TestZeroRequest(t *testing.T) { if err != nil { t.Fatalf("error filtering nodes: %+v", err) } - fwk.RunPreScorePlugins(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes)) - list, err := prioritizeNodes(ctx, nil, fwk, state, test.pod, tf.BuildNodeInfos(test.nodes)) + nodeInfos, err := snapshot.NodeInfos().List() + if err != nil { + t.Fatalf("failed to list node from snapshot: %v", err) + } + fwk.RunPreScorePlugins(ctx, state, test.pod, nodeInfos) + list, err := prioritizeNodes(ctx, nil, fwk, state, test.pod, nodeInfos) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -3399,7 +3405,11 @@ func Test_prioritizeNodes(t *testing.T) { for ii := range test.extenders { extenders = append(extenders, &test.extenders[ii]) } - nodesscores, err := prioritizeNodes(ctx, extenders, fwk, state, test.pod, tf.BuildNodeInfos(test.nodes)) + nodeInfos, err := snapshot.NodeInfos().List() + if err != nil { + t.Fatalf("failed to list node from snapshot: %v", err) + } + nodesscores, err := prioritizeNodes(ctx, extenders, fwk, state, test.pod, nodeInfos) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/pkg/scheduler/testing/framework/fake_extender.go b/pkg/scheduler/testing/framework/fake_extender.go index 4073bd9c98c..9cb095e27d3 100644 --- a/pkg/scheduler/testing/framework/fake_extender.go +++ b/pkg/scheduler/testing/framework/fake_extender.go @@ -124,9 +124,9 @@ func (pl *node2PrioritizerPlugin) Name() string { } // Score return score 100 if the given nodeName is "node2"; otherwise return score 10. -func (pl *node2PrioritizerPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *node2PrioritizerPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { score := 10 - if nodeName == "node2" { + if nodeInfo.Node().Name == "node2" { score = 100 } return int64(score), nil diff --git a/pkg/scheduler/testing/framework/fake_plugins.go b/pkg/scheduler/testing/framework/fake_plugins.go index ccff22bd5ae..c6c26b2f704 100644 --- a/pkg/scheduler/testing/framework/fake_plugins.go +++ b/pkg/scheduler/testing/framework/fake_plugins.go @@ -258,7 +258,7 @@ func (pl *FakePreScoreAndScorePlugin) Name() string { return pl.name } -func (pl *FakePreScoreAndScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (pl *FakePreScoreAndScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { return pl.score, pl.scoreStatus } diff --git a/test/integration/scheduler/plugins/plugins_test.go b/test/integration/scheduler/plugins/plugins_test.go index 0520ba5df51..d359b513d40 100644 --- a/test/integration/scheduler/plugins/plugins_test.go +++ b/test/integration/scheduler/plugins/plugins_test.go @@ -355,7 +355,7 @@ func (sp *ScorePlugin) Name() string { } // Score returns the score of scheduling a pod on a specific node. -func (sp *ScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (sp *ScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { sp.mutex.Lock() defer sp.mutex.Unlock() @@ -367,7 +367,7 @@ func (sp *ScorePlugin) Score(ctx context.Context, state *framework.CycleState, p score := int64(1) if sp.numScoreCalled == 1 { // The first node is scored the highest, the rest is scored lower. - sp.highScoreNode = nodeName + sp.highScoreNode = nodeInfo.Node().Name score = framework.MaxNodeScore } return score, nil @@ -383,7 +383,7 @@ func (sp *ScoreWithNormalizePlugin) Name() string { } // Score returns the score of scheduling a pod on a specific node. -func (sp *ScoreWithNormalizePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { +func (sp *ScoreWithNormalizePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeInfo *framework.NodeInfo) (int64, *framework.Status) { sp.mutex.Lock() defer sp.mutex.Unlock()