-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep track of remaining pods when a node is deleted #93938
Merged
k8s-ci-robot
merged 2 commits into
kubernetes:master
from
alculquicondor:revert-node-delete
Aug 13, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ import ( | |
"time" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
utilfeature "k8s.io/apiserver/pkg/util/feature" | ||
|
@@ -315,7 +314,9 @@ func (cache *schedulerCache) removeDeletedNodesFromSnapshot(snapshot *Snapshot) | |
} | ||
} | ||
|
||
func (cache *schedulerCache) ListPods(selector labels.Selector) ([]*v1.Pod, error) { | ||
// PodCount returns the number of pods in the cache (including those from deleted nodes). | ||
// DO NOT use outside of tests. | ||
func (cache *schedulerCache) PodCount() (int, error) { | ||
cache.mu.RLock() | ||
defer cache.mu.RUnlock() | ||
// podFilter is expected to return true for most or all of the pods. We | ||
|
@@ -325,15 +326,11 @@ func (cache *schedulerCache) ListPods(selector labels.Selector) ([]*v1.Pod, erro | |
for _, n := range cache.nodes { | ||
maxSize += len(n.info.Pods) | ||
} | ||
pods := make([]*v1.Pod, 0, maxSize) | ||
count := 0 | ||
for _, n := range cache.nodes { | ||
for _, p := range n.info.Pods { | ||
if selector.Matches(labels.Set(p.Pod.Labels)) { | ||
pods = append(pods, p.Pod) | ||
} | ||
} | ||
count += len(n.info.Pods) | ||
} | ||
return pods, nil | ||
return count, nil | ||
} | ||
|
||
func (cache *schedulerCache) AssumePod(pod *v1.Pod) error { | ||
|
@@ -423,13 +420,6 @@ func (cache *schedulerCache) addPod(pod *v1.Pod) { | |
|
||
// Assumes that lock is already acquired. | ||
func (cache *schedulerCache) updatePod(oldPod, newPod *v1.Pod) error { | ||
if _, ok := cache.nodes[newPod.Spec.NodeName]; !ok { | ||
// The node might have been deleted already. | ||
// This is not a problem in the case where a pod update arrives before the | ||
// node creation, because we will always have a create pod event before | ||
// that, which will create the placeholder node item. | ||
return nil | ||
} | ||
if err := cache.removePod(oldPod); err != nil { | ||
return err | ||
} | ||
|
@@ -438,18 +428,23 @@ func (cache *schedulerCache) updatePod(oldPod, newPod *v1.Pod) error { | |
} | ||
|
||
// Assumes that lock is already acquired. | ||
// Removes a pod from the cached node info. When a node is removed, some pod | ||
// deletion events might arrive later. This is not a problem, as the pods in | ||
// the node are assumed to be removed already. | ||
// Removes a pod from the cached node info. If the node information was already | ||
// removed and there are no more pods left in the node, cleans up the node from | ||
// the cache. | ||
func (cache *schedulerCache) removePod(pod *v1.Pod) error { | ||
n, ok := cache.nodes[pod.Spec.NodeName] | ||
if !ok { | ||
klog.Errorf("node %v not found when trying to remove pod %v", pod.Spec.NodeName, pod.Name) | ||
return nil | ||
} | ||
if err := n.info.RemovePod(pod); err != nil { | ||
return err | ||
} | ||
cache.moveNodeInfoToHead(pod.Spec.NodeName) | ||
if len(n.info.Pods) == 0 && n.info.Node() == nil { | ||
cache.removeNodeInfoFromList(pod.Spec.NodeName) | ||
} else { | ||
cache.moveNodeInfoToHead(pod.Spec.NodeName) | ||
} | ||
return nil | ||
} | ||
|
||
|
@@ -619,21 +614,30 @@ func (cache *schedulerCache) UpdateNode(oldNode, newNode *v1.Node) error { | |
return n.info.SetNode(newNode) | ||
} | ||
|
||
// RemoveNode removes a node from the cache. | ||
// Some nodes might still have pods because their deletion events didn't arrive | ||
// yet. For most intents and purposes, those pods are removed from the cache, | ||
// having it's source of truth in the cached nodes. | ||
// However, some information on pods (assumedPods, podStates) persist. These | ||
// caches will be eventually consistent as pod deletion events arrive. | ||
// RemoveNode removes a node from the cache's tree. | ||
// The node might still have pods because their deletion events didn't arrive | ||
// yet. Those pods are considered removed from the cache, being the node tree | ||
// the source of truth. | ||
// However, we keep a ghost node with the list of pods until all pod deletion | ||
// events have arrived. A ghost node is skipped from snapshots. | ||
func (cache *schedulerCache) RemoveNode(node *v1.Node) error { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
|
||
_, ok := cache.nodes[node.Name] | ||
n, ok := cache.nodes[node.Name] | ||
if !ok { | ||
return fmt.Errorf("node %v is not found", node.Name) | ||
} | ||
cache.removeNodeInfoFromList(node.Name) | ||
n.info.RemoveNode() | ||
// We remove NodeInfo for this node only if there aren't any pods on this node. | ||
// We can't do it unconditionally, because notifications about pods are delivered | ||
// in a different watch, and thus can potentially be observed later, even though | ||
// they happened before node removal. | ||
if len(n.info.Pods) == 0 { | ||
cache.removeNodeInfoFromList(node.Name) | ||
} else { | ||
cache.moveNodeInfoToHead(node.Name) | ||
} | ||
if err := cache.nodeTree.removeNode(node); err != nil { | ||
return err | ||
} | ||
|
@@ -736,19 +740,6 @@ func (cache *schedulerCache) expirePod(key string, ps *podState) error { | |
return nil | ||
} | ||
|
||
// GetNodeInfo returns cached data for the node name. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think 2 functions in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
func (cache *schedulerCache) GetNodeInfo(nodeName string) (*v1.Node, error) { | ||
cache.mu.RLock() | ||
defer cache.mu.RUnlock() | ||
|
||
n, ok := cache.nodes[nodeName] | ||
if !ok { | ||
return nil, fmt.Errorf("node %q not found in cache", nodeName) | ||
} | ||
|
||
return n.info.Node(), nil | ||
} | ||
|
||
// updateMetrics updates cache size metric values for pods, assumed pods, and nodes | ||
func (cache *schedulerCache) updateMetrics() { | ||
metrics.CacheSize.WithLabelValues("assumed_pods").Set(float64(len(cache.assumedPods))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recalled the original logic returned an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did. But returning nil is actually safer in the case of extraneous update events that might arrive before a node is created, and after the original node was completely removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to returning nil and just logging an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the usage of
removePod()
, there are still a number of callers rely on the returned value. So I'd suggest to revert to the original state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detail for each caller:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, for expirePod and ForgetPod, the node shouldn't have been removed because it still had pods assigned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. That's fair.