Skip to content

Commit dfe9e41

Browse files
Keep track of remaining pods when a node is deleted.
The apiserver is expected to send pod deletion events that might arrive at a different time. However, sometimes a node could be recreated without its pods being deleted. Partial revert of kubernetes#86964 Signed-off-by: Aldo Culquicondor <[email protected]> Change-Id: I51f683e5f05689b711c81ebff34e7118b5337571
1 parent 16d7ecf commit dfe9e41

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

pkg/scheduler/framework/v1alpha1/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ func (n *NodeInfo) SetNode(node *v1.Node) error {
625625
return nil
626626
}
627627

628+
// RemoveNode removes the node object, leaving all other tracking information.
629+
func (n *NodeInfo) RemoveNode() {
630+
n.node = nil
631+
n.Generation = nextGeneration()
632+
}
633+
628634
// FilterOutPods receives a list of pods and filters out those whose node names
629635
// are equal to the node of this NodeInfo, but are not found in the pods of this NodeInfo.
630636
//

pkg/scheduler/internal/cache/cache.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,6 @@ func (cache *schedulerCache) addPod(pod *v1.Pod) {
420420

421421
// Assumes that lock is already acquired.
422422
func (cache *schedulerCache) updatePod(oldPod, newPod *v1.Pod) error {
423-
if _, ok := cache.nodes[newPod.Spec.NodeName]; !ok {
424-
// The node might have been deleted already.
425-
// This is not a problem in the case where a pod update arrives before the
426-
// node creation, because we will always have a create pod event before
427-
// that, which will create the placeholder node item.
428-
return nil
429-
}
430423
if err := cache.removePod(oldPod); err != nil {
431424
return err
432425
}
@@ -435,18 +428,23 @@ func (cache *schedulerCache) updatePod(oldPod, newPod *v1.Pod) error {
435428
}
436429

437430
// Assumes that lock is already acquired.
438-
// Removes a pod from the cached node info. When a node is removed, some pod
439-
// deletion events might arrive later. This is not a problem, as the pods in
440-
// the node are assumed to be removed already.
431+
// Removes a pod from the cached node info. If the node information was already
432+
// removed and there are no more pods left in the node, cleans up the node from
433+
// the cache.
441434
func (cache *schedulerCache) removePod(pod *v1.Pod) error {
442435
n, ok := cache.nodes[pod.Spec.NodeName]
443436
if !ok {
437+
klog.Errorf("node %v not found when trying to remove pod %v", pod.Spec.NodeName, pod.Name)
444438
return nil
445439
}
446440
if err := n.info.RemovePod(pod); err != nil {
447441
return err
448442
}
449-
cache.moveNodeInfoToHead(pod.Spec.NodeName)
443+
if len(n.info.Pods) == 0 && n.info.Node() == nil {
444+
cache.removeNodeInfoFromList(pod.Spec.NodeName)
445+
} else {
446+
cache.moveNodeInfoToHead(pod.Spec.NodeName)
447+
}
450448
return nil
451449
}
452450

@@ -616,21 +614,30 @@ func (cache *schedulerCache) UpdateNode(oldNode, newNode *v1.Node) error {
616614
return n.info.SetNode(newNode)
617615
}
618616

619-
// RemoveNode removes a node from the cache.
620-
// Some nodes might still have pods because their deletion events didn't arrive
621-
// yet. For most intents and purposes, those pods are removed from the cache,
622-
// having it's source of truth in the cached nodes.
623-
// However, some information on pods (assumedPods, podStates) persist. These
624-
// caches will be eventually consistent as pod deletion events arrive.
617+
// RemoveNode removes a node from the cache's tree.
618+
// The node might still have pods because their deletion events didn't arrive
619+
// yet. Those pods are considered removed from the cache, being the node tree
620+
// the source of truth.
621+
// However, we keep a ghost node with the list of pods until all pod deletion
622+
// events have arrived. A ghost node is skipped from snapshots.
625623
func (cache *schedulerCache) RemoveNode(node *v1.Node) error {
626624
cache.mu.Lock()
627625
defer cache.mu.Unlock()
628626

629-
_, ok := cache.nodes[node.Name]
627+
n, ok := cache.nodes[node.Name]
630628
if !ok {
631629
return fmt.Errorf("node %v is not found", node.Name)
632630
}
633-
cache.removeNodeInfoFromList(node.Name)
631+
n.info.RemoveNode()
632+
// We remove NodeInfo for this node only if there aren't any pods on this node.
633+
// We can't do it unconditionally, because notifications about pods are delivered
634+
// in a different watch, and thus can potentially be observed later, even though
635+
// they happened before node removal.
636+
if len(n.info.Pods) == 0 {
637+
cache.removeNodeInfoFromList(node.Name)
638+
} else {
639+
cache.moveNodeInfoToHead(node.Name)
640+
}
634641
if err := cache.nodeTree.removeNode(node); err != nil {
635642
return err
636643
}

pkg/scheduler/internal/cache/cache_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func TestExpirePod(t *testing.T) {
268268
{pod: testPods[0], finishBind: true, assumedTime: now},
269269
},
270270
cleanupTime: now.Add(2 * ttl),
271-
wNodeInfo: framework.NewNodeInfo(),
271+
wNodeInfo: nil,
272272
}, { // first one would expire, second and third would not.
273273
pods: []*testExpirePodStruct{
274274
{pod: testPods[0], finishBind: true, assumedTime: now},
@@ -1142,10 +1142,12 @@ func TestNodeOperators(t *testing.T) {
11421142
if err := cache.RemoveNode(node); err != nil {
11431143
t.Error(err)
11441144
}
1145-
if _, err := cache.getNodeInfo(node.Name); err == nil {
1146-
t.Errorf("The node %v should be removed.", node.Name)
1145+
if n, err := cache.getNodeInfo(node.Name); err != nil {
1146+
t.Errorf("The node %v should still have a ghost entry: %v", node.Name, err)
1147+
} else if n != nil {
1148+
t.Errorf("The node object for %v should be nil", node.Name)
11471149
}
1148-
// Check node is removed from nodeTree as well.
1150+
// Check node is removed from nodeTree.
11491151
if cache.nodeTree.numNodes != 0 || cache.nodeTree.next() != "" {
11501152
t.Errorf("unexpected cache.nodeTree after removing node: %v", node.Name)
11511153
}
@@ -1466,7 +1468,7 @@ func TestSchedulerCache_UpdateSnapshot(t *testing.T) {
14661468
var i int
14671469
// Check that cache is in the expected state.
14681470
for node := cache.headNode; node != nil; node = node.next {
1469-
if node.info.Node().Name != test.expected[i].Name {
1471+
if node.info.Node() != nil && node.info.Node().Name != test.expected[i].Name {
14701472
t.Errorf("unexpected node. Expected: %v, got: %v, index: %v", test.expected[i].Name, node.info.Node().Name, i)
14711473
}
14721474
i++

pkg/scheduler/internal/cache/debugger/dumper.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func (d *CacheDumper) DumpAll() {
4545
func (d *CacheDumper) dumpNodes() {
4646
dump := d.cache.Dump()
4747
klog.Info("Dump of cached NodeInfo")
48-
for _, nodeInfo := range dump.Nodes {
49-
klog.Info(d.printNodeInfo(nodeInfo))
48+
for name, nodeInfo := range dump.Nodes {
49+
klog.Info(d.printNodeInfo(name, nodeInfo))
5050
}
5151
}
5252

@@ -61,16 +61,16 @@ func (d *CacheDumper) dumpSchedulingQueue() {
6161
}
6262

6363
// printNodeInfo writes parts of NodeInfo to a string.
64-
func (d *CacheDumper) printNodeInfo(n *framework.NodeInfo) string {
64+
func (d *CacheDumper) printNodeInfo(name string, n *framework.NodeInfo) string {
6565
var nodeData strings.Builder
66-
nodeData.WriteString(fmt.Sprintf("\nNode name: %+v\nRequested Resources: %+v\nAllocatable Resources:%+v\nScheduled Pods(number: %v):\n",
67-
n.Node().Name, n.Requested, n.Allocatable, len(n.Pods)))
66+
nodeData.WriteString(fmt.Sprintf("\nNode name: %s\nDeleted: %t\nRequested Resources: %+v\nAllocatable Resources:%+v\nScheduled Pods(number: %v):\n",
67+
name, n.Node() == nil, n.Requested, n.Allocatable, len(n.Pods)))
6868
// Dumping Pod Info
6969
for _, p := range n.Pods {
7070
nodeData.WriteString(printPod(p.Pod))
7171
}
7272
// Dumping nominated pods info on the node
73-
nominatedPods := d.podQueue.NominatedPodsForNode(n.Node().Name)
73+
nominatedPods := d.podQueue.NominatedPodsForNode(name)
7474
if len(nominatedPods) != 0 {
7575
nodeData.WriteString(fmt.Sprintf("Nominated Pods(number: %v):\n", len(nominatedPods)))
7676
for _, p := range nominatedPods {

0 commit comments

Comments
 (0)