mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Performance improvement for affinity term matching.
When a PodAffinityTerm uses TopologyKey=kubernetes.io/hostname, we can avoid searching the entire cluster for a match by only listing pods on the given node.
This commit is contained in:
parent
d7e5bd194a
commit
732e785e0a
@ -1116,7 +1116,7 @@ func (c *PodAffinityChecker) InterPodAffinityMatches(pod *v1.Pod, meta algorithm
|
|||||||
// First return value indicates whether a matching pod exists on a node that matches the topology key,
|
// First return value indicates whether a matching pod exists on a node that matches the topology key,
|
||||||
// while the second return value indicates whether a matching pod exists anywhere.
|
// while the second return value indicates whether a matching pod exists anywhere.
|
||||||
// TODO: Do we really need any pod matching, or all pods matching? I think the latter.
|
// TODO: Do we really need any pod matching, or all pods matching? I think the latter.
|
||||||
func (c *PodAffinityChecker) anyPodMatchesPodAffinityTerm(pod *v1.Pod, allPods []*v1.Pod, node *v1.Node, term *v1.PodAffinityTerm) (bool, bool, error) {
|
func (c *PodAffinityChecker) anyPodMatchesPodAffinityTerm(pod *v1.Pod, pods []*v1.Pod, nodeInfo *schedulercache.NodeInfo, term *v1.PodAffinityTerm) (bool, bool, error) {
|
||||||
if len(term.TopologyKey) == 0 {
|
if len(term.TopologyKey) == 0 {
|
||||||
return false, false, fmt.Errorf("empty topologyKey is not allowed except for PreferredDuringScheduling pod anti-affinity")
|
return false, false, fmt.Errorf("empty topologyKey is not allowed except for PreferredDuringScheduling pod anti-affinity")
|
||||||
}
|
}
|
||||||
@ -1126,7 +1126,12 @@ func (c *PodAffinityChecker) anyPodMatchesPodAffinityTerm(pod *v1.Pod, allPods [
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, false, err
|
return false, false, err
|
||||||
}
|
}
|
||||||
for _, existingPod := range allPods {
|
// Special case: When the topological domain is node, we can limit our
|
||||||
|
// search to pods on that node without searching the entire cluster.
|
||||||
|
if term.TopologyKey == kubeletapis.LabelHostname {
|
||||||
|
pods = nodeInfo.Pods()
|
||||||
|
}
|
||||||
|
for _, existingPod := range pods {
|
||||||
match := priorityutil.PodMatchesTermsNamespaceAndSelector(existingPod, namespaces, selector)
|
match := priorityutil.PodMatchesTermsNamespaceAndSelector(existingPod, namespaces, selector)
|
||||||
if match {
|
if match {
|
||||||
matchingPodExists = true
|
matchingPodExists = true
|
||||||
@ -1134,7 +1139,7 @@ func (c *PodAffinityChecker) anyPodMatchesPodAffinityTerm(pod *v1.Pod, allPods [
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, matchingPodExists, err
|
return false, matchingPodExists, err
|
||||||
}
|
}
|
||||||
if priorityutil.NodesHaveSameTopologyKey(node, existingPodNode, term.TopologyKey) {
|
if priorityutil.NodesHaveSameTopologyKey(nodeInfo.Node(), existingPodNode, term.TopologyKey) {
|
||||||
return true, matchingPodExists, nil
|
return true, matchingPodExists, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1334,7 +1339,7 @@ func (c *PodAffinityChecker) satisfiesPodsAffinityAntiAffinity(pod *v1.Pod, node
|
|||||||
|
|
||||||
// Check all affinity terms.
|
// Check all affinity terms.
|
||||||
for _, term := range GetPodAffinityTerms(affinity.PodAffinity) {
|
for _, term := range GetPodAffinityTerms(affinity.PodAffinity) {
|
||||||
termMatches, matchingPodExists, err := c.anyPodMatchesPodAffinityTerm(pod, filteredPods, node, &term)
|
termMatches, matchingPodExists, err := c.anyPodMatchesPodAffinityTerm(pod, filteredPods, nodeInfo, &term)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMessage := fmt.Sprintf("Cannot schedule pod %+v onto node %v, because of PodAffinityTerm %v, err: %v", podName(pod), node.Name, term, err)
|
errMessage := fmt.Sprintf("Cannot schedule pod %+v onto node %v, because of PodAffinityTerm %v, err: %v", podName(pod), node.Name, term, err)
|
||||||
glog.Error(errMessage)
|
glog.Error(errMessage)
|
||||||
@ -1367,7 +1372,7 @@ func (c *PodAffinityChecker) satisfiesPodsAffinityAntiAffinity(pod *v1.Pod, node
|
|||||||
|
|
||||||
// Check all anti-affinity terms.
|
// Check all anti-affinity terms.
|
||||||
for _, term := range GetPodAntiAffinityTerms(affinity.PodAntiAffinity) {
|
for _, term := range GetPodAntiAffinityTerms(affinity.PodAntiAffinity) {
|
||||||
termMatches, _, err := c.anyPodMatchesPodAffinityTerm(pod, filteredPods, node, &term)
|
termMatches, _, err := c.anyPodMatchesPodAffinityTerm(pod, filteredPods, nodeInfo, &term)
|
||||||
if err != nil || termMatches {
|
if err != nil || termMatches {
|
||||||
glog.V(10).Infof("Cannot schedule pod %+v onto node %v, because of PodAntiAffinityTerm %v, err: %v",
|
glog.V(10).Infof("Cannot schedule pod %+v onto node %v, because of PodAntiAffinityTerm %v, err: %v",
|
||||||
podName(pod), node.Name, term, err)
|
podName(pod), node.Name, term, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user