Skip to content

Commit 175ee0a

Browse files
authored
Merge pull request kubernetes#86542 from zouyee/nodeinfocheck
introduce checker for the result of nodeInfo.Node()
2 parents 1ca0f89 + bf8a8a6 commit 175ee0a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

pkg/scheduler/framework/plugins/nodelabel/node_label.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func (pl *NodeLabel) Name() string {
101101
// and it may be desirable to avoid scheduling new pods on this node.
102102
func (pl *NodeLabel) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
103103
node := nodeInfo.Node()
104+
if node == nil {
105+
return framework.NewStatus(framework.Error, "node not found")
106+
}
104107
nodeLabels := labels.Set(node.Labels)
105108
check := func(labels []string, presence bool) bool {
106109
for _, label := range labels {
@@ -121,8 +124,8 @@ func (pl *NodeLabel) Filter(ctx context.Context, _ *framework.CycleState, pod *v
121124
// Score invoked at the score extension point.
122125
func (pl *NodeLabel) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
123126
nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
124-
if err != nil {
125-
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
127+
if err != nil || nodeInfo.Node() == nil {
128+
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v, node is nil: %v", nodeName, err, nodeInfo.Node() == nil))
126129
}
127130

128131
node := nodeInfo.Node()

pkg/scheduler/framework/plugins/nodelabel/node_label_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,33 @@ func TestNodeLabelScore(t *testing.T) {
244244
})
245245
}
246246
}
247+
248+
func TestNodeLabelFilterWithoutNode(t *testing.T) {
249+
var pod *v1.Pod
250+
t.Run("node does not exist", func(t *testing.T) {
251+
nodeInfo := schedulernodeinfo.NewNodeInfo()
252+
p, err := New(nil, nil)
253+
if err != nil {
254+
t.Fatalf("Failed to create plugin: %v", err)
255+
}
256+
status := p.(framework.FilterPlugin).Filter(context.TODO(), nil, pod, nodeInfo)
257+
if status.Code() != framework.Error {
258+
t.Errorf("Status mismatch. got: %v, want: %v", status.Code(), framework.Error)
259+
}
260+
})
261+
}
262+
263+
func TestNodeLabelScoreWithoutNode(t *testing.T) {
264+
t.Run("node does not exist", func(t *testing.T) {
265+
fh, _ := framework.NewFramework(nil, nil, nil, framework.WithSnapshotSharedLister(nodeinfosnapshot.NewEmptySnapshot()))
266+
p, err := New(nil, fh)
267+
if err != nil {
268+
t.Fatalf("Failed to create plugin: %+v", err)
269+
}
270+
_, status := p.(framework.ScorePlugin).Score(context.Background(), nil, nil, "")
271+
if status.Code() != framework.Error {
272+
t.Errorf("Status mismatch. got: %v, want: %v", status.Code(), framework.Error)
273+
}
274+
})
275+
276+
}

0 commit comments

Comments
 (0)