123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package priorities
- import (
- "fmt"
- v1 "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/labels"
- "k8s.io/kubernetes/pkg/scheduler/algorithm"
- schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
- schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
- utilnode "k8s.io/kubernetes/pkg/util/node"
- "k8s.io/klog"
- )
- const zoneWeighting float64 = 2.0 / 3.0
- type SelectorSpread struct {
- serviceLister algorithm.ServiceLister
- controllerLister algorithm.ControllerLister
- replicaSetLister algorithm.ReplicaSetLister
- statefulSetLister algorithm.StatefulSetLister
- }
- func NewSelectorSpreadPriority(
- serviceLister algorithm.ServiceLister,
- controllerLister algorithm.ControllerLister,
- replicaSetLister algorithm.ReplicaSetLister,
- statefulSetLister algorithm.StatefulSetLister) (PriorityMapFunction, PriorityReduceFunction) {
- selectorSpread := &SelectorSpread{
- serviceLister: serviceLister,
- controllerLister: controllerLister,
- replicaSetLister: replicaSetLister,
- statefulSetLister: statefulSetLister,
- }
- return selectorSpread.CalculateSpreadPriorityMap, selectorSpread.CalculateSpreadPriorityReduce
- }
- func (s *SelectorSpread) CalculateSpreadPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *schedulernodeinfo.NodeInfo) (schedulerapi.HostPriority, error) {
- var selectors []labels.Selector
- node := nodeInfo.Node()
- if node == nil {
- return schedulerapi.HostPriority{}, fmt.Errorf("node not found")
- }
- priorityMeta, ok := meta.(*priorityMetadata)
- if ok {
- selectors = priorityMeta.podSelectors
- } else {
- selectors = getSelectors(pod, s.serviceLister, s.controllerLister, s.replicaSetLister, s.statefulSetLister)
- }
- if len(selectors) == 0 {
- return schedulerapi.HostPriority{
- Host: node.Name,
- Score: float64(0),
- }, nil
- }
- count := countMatchingPods(pod.Namespace, selectors, nodeInfo)
- return schedulerapi.HostPriority{
- Host: node.Name,
- Score: float64(count),
- }, nil
- }
- func (s *SelectorSpread) CalculateSpreadPriorityReduce(pod *v1.Pod, meta interface{}, nodeNameToInfo map[string]*schedulernodeinfo.NodeInfo, result schedulerapi.HostPriorityList) error {
- countsByZone := make(map[string]int, 10)
- maxCountByZone := int(0)
- maxCountByNodeName := int(0)
- for i := range result {
- if int(result[i].Score) > maxCountByNodeName {
- maxCountByNodeName = int(result[i].Score)
- }
- zoneID := utilnode.GetZoneKey(nodeNameToInfo[result[i].Host].Node())
- if zoneID == "" {
- continue
- }
- countsByZone[zoneID] += int(result[i].Score)
- }
- for zoneID := range countsByZone {
- if countsByZone[zoneID] > maxCountByZone {
- maxCountByZone = countsByZone[zoneID]
- }
- }
- haveZones := len(countsByZone) != 0
- maxCountByNodeNameFloat64 := float64(maxCountByNodeName)
- maxCountByZoneFloat64 := float64(maxCountByZone)
- MaxPriorityFloat64 := float64(schedulerapi.MaxPriority)
- for i := range result {
-
- fScore := MaxPriorityFloat64
- if maxCountByNodeName > 0 {
- fScore = MaxPriorityFloat64 * (float64(maxCountByNodeName-int(result[i].Score)) / maxCountByNodeNameFloat64)
- }
-
- if haveZones {
- zoneID := utilnode.GetZoneKey(nodeNameToInfo[result[i].Host].Node())
- if zoneID != "" {
- zoneScore := MaxPriorityFloat64
- if maxCountByZone > 0 {
- zoneScore = MaxPriorityFloat64 * (float64(maxCountByZone-countsByZone[zoneID]) / maxCountByZoneFloat64)
- }
- fScore = (fScore * (1.0 - zoneWeighting)) + (zoneWeighting * zoneScore)
- }
- }
- result[i].Score = fScore
- if klog.V(10) {
- klog.Infof(
- "%v -> %v: SelectorSpreadPriority, Score: (%d)", pod.Name, result[i].Host, int(fScore),
- )
- }
- }
- return nil
- }
- type ServiceAntiAffinity struct {
- podLister algorithm.PodLister
- serviceLister algorithm.ServiceLister
- label string
- }
- func NewServiceAntiAffinityPriority(podLister algorithm.PodLister, serviceLister algorithm.ServiceLister, label string) (PriorityMapFunction, PriorityReduceFunction) {
- antiAffinity := &ServiceAntiAffinity{
- podLister: podLister,
- serviceLister: serviceLister,
- label: label,
- }
- return antiAffinity.CalculateAntiAffinityPriorityMap, antiAffinity.CalculateAntiAffinityPriorityReduce
- }
- func (s *ServiceAntiAffinity) getNodeClassificationByLabels(nodes []*v1.Node) (map[string]string, []string) {
- labeledNodes := map[string]string{}
- nonLabeledNodes := []string{}
- for _, node := range nodes {
- if labels.Set(node.Labels).Has(s.label) {
- label := labels.Set(node.Labels).Get(s.label)
- labeledNodes[node.Name] = label
- } else {
- nonLabeledNodes = append(nonLabeledNodes, node.Name)
- }
- }
- return labeledNodes, nonLabeledNodes
- }
- func countMatchingPods(namespace string, selectors []labels.Selector, nodeInfo *schedulernodeinfo.NodeInfo) int {
- if nodeInfo.Pods() == nil || len(nodeInfo.Pods()) == 0 || len(selectors) == 0 {
- return 0
- }
- count := 0
- for _, pod := range nodeInfo.Pods() {
-
-
- if namespace == pod.Namespace && pod.DeletionTimestamp == nil {
- matches := true
- for _, selector := range selectors {
- if !selector.Matches(labels.Set(pod.Labels)) {
- matches = false
- break
- }
- }
- if matches {
- count++
- }
- }
- }
- return count
- }
- func (s *ServiceAntiAffinity) CalculateAntiAffinityPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *schedulernodeinfo.NodeInfo) (schedulerapi.HostPriority, error) {
- var firstServiceSelector labels.Selector
- node := nodeInfo.Node()
- if node == nil {
- return schedulerapi.HostPriority{}, fmt.Errorf("node not found")
- }
- priorityMeta, ok := meta.(*priorityMetadata)
- if ok {
- firstServiceSelector = priorityMeta.podFirstServiceSelector
- } else {
- firstServiceSelector = getFirstServiceSelector(pod, s.serviceLister)
- }
-
- var selectors []labels.Selector
- if firstServiceSelector != nil {
- selectors = append(selectors, firstServiceSelector)
- }
- score := countMatchingPods(pod.Namespace, selectors, nodeInfo)
- return schedulerapi.HostPriority{
- Host: node.Name,
- Score: float64(score),
- }, nil
- }
- func (s *ServiceAntiAffinity) CalculateAntiAffinityPriorityReduce(pod *v1.Pod, meta interface{}, nodeNameToInfo map[string]*schedulernodeinfo.NodeInfo, result schedulerapi.HostPriorityList) error {
- var numServicePods int
- var label string
- podCounts := map[string]int{}
- labelNodesStatus := map[string]string{}
- maxPriorityFloat64 := float64(schedulerapi.MaxPriority)
- for _, hostPriority := range result {
- numServicePods += int(hostPriority.Score)
- if !labels.Set(nodeNameToInfo[hostPriority.Host].Node().Labels).Has(s.label) {
- continue
- }
- label = labels.Set(nodeNameToInfo[hostPriority.Host].Node().Labels).Get(s.label)
- labelNodesStatus[hostPriority.Host] = label
- podCounts[label] += int(hostPriority.Score)
- }
-
-
- for i, hostPriority := range result {
- label, ok := labelNodesStatus[hostPriority.Host]
- if !ok {
- result[i].Host = hostPriority.Host
- result[i].Score = 0
- continue
- }
-
- fScore := maxPriorityFloat64
- if numServicePods > 0 {
- fScore = maxPriorityFloat64 * (float64(numServicePods-podCounts[label]) / float64(numServicePods))
- }
- result[i].Host = hostPriority.Host
- result[i].Score = fScore
- }
- return nil
- }
|