-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffbased_prioritizer.go
78 lines (66 loc) · 2.94 KB
/
diffbased_prioritizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package prioritizer
import (
"context"
"fmt"
"sort"
policyv1alpha1 "github.com/Congrool/nodes-grouping/pkg/apis/policy/v1alpha1"
"github.com/Congrool/nodes-grouping/pkg/schedulerextender/constants"
"github.com/Congrool/nodes-grouping/pkg/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
extenderv1 "k8s.io/kube-scheduler/extender/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ PrioritizerPlugin = &diffBasedPrioritizePlugin{}
type nodeGroupItem struct {
nodeGroupName string
podsNum int32
rank int
}
type nodeGroupItemSlice []nodeGroupItem
func (c nodeGroupItemSlice) Len() int { return len(c) }
func (c nodeGroupItemSlice) Less(i, j int) bool { return c[i].podsNum < c[j].podsNum }
func (c nodeGroupItemSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
type diffBasedPrioritizePlugin struct{}
func (p *diffBasedPrioritizePlugin) Name() string {
return constants.DiffBasedPrioritizePluginName
}
// TODO:
// figure out how to score nodes when the number of the candicates is too small.
func (p *diffBasedPrioritizePlugin) PrioritizeNodes(ctx context.Context, client client.Client, pod *corev1.Pod, args *extenderv1.ExtenderArgs, policy *policyv1alpha1.PropagationPolicy) (extenderv1.HostPriorityList, error) {
relativeDeploy, err := utils.GetRelativeDeployment(ctx, client, pod, policy)
if err != nil {
return nil, fmt.Errorf("failed to get relative deployment for pod %s/%s when prioritizing nodes for it, %v",
pod.Namespace, pod.Name, err)
}
desiredPodsNumOfEachNodeGroup := utils.DesiredPodsNumInTargetNodeGroups(policy.Spec.Placement.StaticWeightList, *relativeDeploy.Spec.Replicas)
currentPodsNumOfEachNodeGroup, nodesInNodeGroup, err := utils.CurrentPodsNumInTargetNodeGroups(ctx, client, relativeDeploy, policy)
if err != nil {
return nil, fmt.Errorf("failed to get current pods number in nodegroup for pod %s/%s with policy %s/%s, %v",
pod.Namespace, pod.Name, policy.Namespace, policy.Name, err)
}
var diffList nodeGroupItemSlice
for nodegroup, desiredNum := range desiredPodsNumOfEachNodeGroup {
diffPodsNum := desiredNum - currentPodsNumOfEachNodeGroup[nodegroup]
diffList = append(diffList, nodeGroupItem{nodeGroupName: nodegroup, podsNum: diffPodsNum})
}
diffList = sort.Reverse(diffList).(nodeGroupItemSlice)
diffMap := make(map[string]nodeGroupItem, diffList.Len())
for i := range diffList {
diffList[i].rank = i + 1
nodeGroupName := diffList[i].nodeGroupName
diffMap[nodeGroupName] = diffList[i]
}
priorityList := extenderv1.HostPriorityList{}
for _, nodename := range *args.NodeNames {
nodeGroup, ok := nodesInNodeGroup[nodename]
if !ok {
klog.Errorf("extender prioritizer get node %s which is not in target nodegroup, ignore it")
continue
}
ratio := (float64)(diffMap[nodeGroup].rank) / (float64)(diffList.Len())
score := (int64)(10 - 10*ratio)
priorityList = append(priorityList, extenderv1.HostPriority{Host: nodename, Score: score})
}
return priorityList, nil
}