diff --git a/plugin/pkg/scheduler/algorithm/predicates/predicates.go b/plugin/pkg/scheduler/algorithm/predicates/predicates.go index 7665bcc5572..a50a51ae9be 100644 --- a/plugin/pkg/scheduler/algorithm/predicates/predicates.go +++ b/plugin/pkg/scheduler/algorithm/predicates/predicates.go @@ -1174,11 +1174,6 @@ func isPodBestEffort(pod *v1.Pod) bool { // CheckNodeMemoryPressurePredicate checks if a pod can be scheduled on a node // reporting memory pressure condition. func CheckNodeMemoryPressurePredicate(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) { - node := nodeInfo.Node() - if node == nil { - return false, nil, fmt.Errorf("node not found") - } - var podBestEffort bool if predicateMeta, ok := meta.(*predicateMetadata); ok { podBestEffort = predicateMeta.podBestEffort @@ -1186,36 +1181,24 @@ func CheckNodeMemoryPressurePredicate(pod *v1.Pod, meta interface{}, nodeInfo *s // We couldn't parse metadata - fallback to computing it. podBestEffort = isPodBestEffort(pod) } - // pod is not BestEffort pod if !podBestEffort { return true, nil, nil } - // is node under pressure? - for _, cond := range node.Status.Conditions { - if cond.Type == v1.NodeMemoryPressure && cond.Status == v1.ConditionTrue { - return false, []algorithm.PredicateFailureReason{ErrNodeUnderMemoryPressure}, nil - } + // is node under presure? + if nodeInfo.MemoryPressureCondition() == v1.ConditionTrue { + return false, []algorithm.PredicateFailureReason{ErrNodeUnderMemoryPressure}, nil } - return true, nil, nil } // CheckNodeDiskPressurePredicate checks if a pod can be scheduled on a node // reporting disk pressure condition. func CheckNodeDiskPressurePredicate(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) { - node := nodeInfo.Node() - if node == nil { - return false, nil, fmt.Errorf("node not found") + // is node under presure? + if nodeInfo.DiskPressureCondition() == v1.ConditionTrue { + return false, []algorithm.PredicateFailureReason{ErrNodeUnderDiskPressure}, nil } - - // is node under pressure? - for _, cond := range node.Status.Conditions { - if cond.Type == v1.NodeDiskPressure && cond.Status == v1.ConditionTrue { - return false, []algorithm.PredicateFailureReason{ErrNodeUnderDiskPressure}, nil - } - } - return true, nil, nil } diff --git a/plugin/pkg/scheduler/schedulercache/node_info.go b/plugin/pkg/scheduler/schedulercache/node_info.go index 4a173b6b2ee..4fceb567f21 100644 --- a/plugin/pkg/scheduler/schedulercache/node_info.go +++ b/plugin/pkg/scheduler/schedulercache/node_info.go @@ -49,6 +49,10 @@ type NodeInfo struct { // explicitly as int, to avoid conversions and improve performance. allowedPodNumber int + // Cached conditions of node for faster lookup. + memoryPressureCondition v1.ConditionStatus + diskPressureCondition v1.ConditionStatus + // Whenever NodeInfo changes, generation is bumped. // This is used to avoid cloning it if the object didn't change. generation int64 @@ -122,6 +126,20 @@ func (n *NodeInfo) AllowedPodNumber() int { return n.allowedPodNumber } +func (n *NodeInfo) MemoryPressureCondition() v1.ConditionStatus { + if n == nil { + return v1.ConditionUnknown + } + return n.memoryPressureCondition +} + +func (n *NodeInfo) DiskPressureCondition() v1.ConditionStatus { + if n == nil { + return v1.ConditionUnknown + } + return n.diskPressureCondition +} + // RequestedResource returns aggregated resource request of pods on this node. func (n *NodeInfo) RequestedResource() Resource { if n == nil { @@ -148,12 +166,14 @@ func (n *NodeInfo) AllocatableResource() Resource { func (n *NodeInfo) Clone() *NodeInfo { clone := &NodeInfo{ - node: n.node, - requestedResource: &(*n.requestedResource), - nonzeroRequest: &(*n.nonzeroRequest), - allocatableResource: &(*n.allocatableResource), - allowedPodNumber: n.allowedPodNumber, - generation: n.generation, + node: n.node, + requestedResource: &(*n.requestedResource), + nonzeroRequest: &(*n.nonzeroRequest), + allocatableResource: &(*n.allocatableResource), + allowedPodNumber: n.allowedPodNumber, + memoryPressureCondition: n.memoryPressureCondition, + diskPressureCondition: n.diskPressureCondition, + generation: n.generation, } if len(n.pods) > 0 { clone.pods = append([]*v1.Pod(nil), n.pods...) @@ -306,6 +326,17 @@ func (n *NodeInfo) SetNode(node *v1.Node) error { } } } + for i := range node.Status.Conditions { + cond := &node.Status.Conditions[i] + switch cond.Type { + case v1.NodeMemoryPressure: + n.memoryPressureCondition = cond.Status + case v1.NodeDiskPressure: + n.diskPressureCondition = cond.Status + default: + // We ignore other conditions. + } + } n.generation++ return nil } @@ -319,6 +350,8 @@ func (n *NodeInfo) RemoveNode(node *v1.Node) error { n.node = nil n.allocatableResource = &Resource{} n.allowedPodNumber = 0 + n.memoryPressureCondition = v1.ConditionUnknown + n.diskPressureCondition = v1.ConditionUnknown n.generation++ return nil }