mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Merge pull request #84264 from ahg-g/ahg-antiaffinity
Optimize interpod affinity priority function
This commit is contained in:
commit
2905275a08
@ -18,7 +18,7 @@ package priorities
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync/atomic"
|
"sync"
|
||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -47,17 +47,20 @@ func NewInterPodAffinityPriority(nodeLister schedulerlisters.NodeLister, hardPod
|
|||||||
return interPodAffinity.CalculateInterPodAffinityPriority
|
return interPodAffinity.CalculateInterPodAffinityPriority
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type topologyPairToScore map[string]map[string]int64
|
||||||
|
|
||||||
type podAffinityPriorityMap struct {
|
type podAffinityPriorityMap struct {
|
||||||
// nodes contain all nodes that should be considered.
|
// nodes contain all nodes that should be considered.
|
||||||
nodes []*v1.Node
|
nodes []*v1.Node
|
||||||
// counts store the so-far computed score for each node.
|
// tracks a topology pair score so far.
|
||||||
counts []int64
|
topologyScore topologyPairToScore
|
||||||
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPodAffinityPriorityMap(nodes []*v1.Node) *podAffinityPriorityMap {
|
func newPodAffinityPriorityMap(nodes []*v1.Node) *podAffinityPriorityMap {
|
||||||
return &podAffinityPriorityMap{
|
return &podAffinityPriorityMap{
|
||||||
nodes: nodes,
|
nodes: nodes,
|
||||||
counts: make([]int64, len(nodes)),
|
topologyScore: make(topologyPairToScore),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,13 +70,19 @@ func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefini
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if len(fixedNode.Labels) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
match := priorityutil.PodMatchesTermsNamespaceAndSelector(podToCheck, namespaces, selector)
|
match := priorityutil.PodMatchesTermsNamespaceAndSelector(podToCheck, namespaces, selector)
|
||||||
if match {
|
tpValue, tpValueExist := fixedNode.Labels[term.TopologyKey]
|
||||||
for i, node := range p.nodes {
|
if match && tpValueExist {
|
||||||
if priorityutil.NodesHaveSameTopologyKey(node, fixedNode, term.TopologyKey) {
|
p.Lock()
|
||||||
atomic.AddInt64(&p.counts[i], weight)
|
if p.topologyScore[term.TopologyKey] == nil {
|
||||||
}
|
p.topologyScore[term.TopologyKey] = make(map[string]int64)
|
||||||
}
|
}
|
||||||
|
p.topologyScore[term.TopologyKey][tpValue] += weight
|
||||||
|
p.Unlock()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -203,12 +212,20 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
counts := make([]int64, len(nodes))
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
if pm.counts[i] > maxCount {
|
if nodes[i].Labels != nil {
|
||||||
maxCount = pm.counts[i]
|
for tpKey, tpValues := range pm.topologyScore {
|
||||||
|
if v, exist := nodes[i].Labels[tpKey]; exist {
|
||||||
|
counts[i] += tpValues[v]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if pm.counts[i] < minCount {
|
if counts[i] > maxCount {
|
||||||
minCount = pm.counts[i]
|
maxCount = counts[i]
|
||||||
|
}
|
||||||
|
if counts[i] < minCount {
|
||||||
|
minCount = counts[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +235,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
|
|||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
fScore := float64(0)
|
fScore := float64(0)
|
||||||
if maxMinDiff > 0 {
|
if maxMinDiff > 0 {
|
||||||
fScore = float64(framework.MaxNodeScore) * (float64(pm.counts[i]-minCount) / float64(maxCount-minCount))
|
fScore = float64(framework.MaxNodeScore) * (float64(counts[i]-minCount) / float64(maxCount-minCount))
|
||||||
}
|
}
|
||||||
result = append(result, framework.NodeScore{Name: node.Name, Score: int64(fScore)})
|
result = append(result, framework.NodeScore{Name: node.Name, Score: int64(fScore)})
|
||||||
if klog.V(10) {
|
if klog.V(10) {
|
||||||
|
Loading…
Reference in New Issue
Block a user