diff --git a/plugin/pkg/scheduler/algorithm/priorities/priorities.go b/plugin/pkg/scheduler/algorithm/priorities/priorities.go index 44e874a722a..e42ea0fa49f 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/priorities.go +++ b/plugin/pkg/scheduler/algorithm/priorities/priorities.go @@ -39,9 +39,9 @@ func calculateScore(requested, capacity int64, node string) int { return int(((capacity - requested) * 10) / capacity) } -// Calculate the occupancy on a node. 'node' has information about the resources on the node. +// Calculate the resource occupancy on a node. 'node' has information about the resources on the node. // 'pods' is a list of pods currently scheduled on the node. -func calculateOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority { +func calculateResourceOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority { totalMilliCPU := int64(0) totalMemory := int64(0) for _, existingPod := range pods { @@ -89,7 +89,42 @@ func LeastRequestedPriority(pod *api.Pod, podLister algorithm.PodLister, minionL list := algorithm.HostPriorityList{} for _, node := range nodes.Items { - list = append(list, calculateOccupancy(pod, node, podsToMachines[node.Name])) + list = append(list, calculateResourceOccupancy(pod, node, podsToMachines[node.Name])) + } + return list, nil +} + +func min(l, r int64) (m int64) { + m = r + if l < r { + m = l + } + return m +} + +// See comment for DumbSpreadingPriority() +const dumbSpreadingDenominator int64 = 10 + +// DumbSpreadingPriority is a priority function that favors nodes with fewer pods. +// It works like LeastRequestedPeriority but instead of using 10 * percentage of machine free by resource, +// it uses 10 * percentage of machine free by pod, with "percentage of machine free by pod" claculated as +// (dumbSpreadingDenominator - number of pods already on the node + 1) / dumbSpreadingDenominator. +// dumbSpreadingDenominator serves like the machine capacity in LeasRequestedPriority but is chosen +// so that we equate one pod with a reasonable amount of resources when we combine all the scores together. +func DumbSpreadingPriority(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) { + nodes, err := minionLister.List() + if err != nil { + return algorithm.HostPriorityList{}, err + } + podsToMachines, err := predicates.MapPodsToMachines(podLister) + + list := algorithm.HostPriorityList{} + for _, node := range nodes.Items { + npods := int64(len(podsToMachines[node.Name])) + list = append(list, algorithm.HostPriority{ + Host: node.Name, + Score: calculateScore(min(npods+1, dumbSpreadingDenominator), dumbSpreadingDenominator, node.Name), + }) } return list, nil } diff --git a/plugin/pkg/scheduler/algorithm/priorities/spreading.go b/plugin/pkg/scheduler/algorithm/priorities/service_spreading.go similarity index 100% rename from plugin/pkg/scheduler/algorithm/priorities/spreading.go rename to plugin/pkg/scheduler/algorithm/priorities/service_spreading.go diff --git a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go index 1546412b78a..5863b62b4c7 100644 --- a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go +++ b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go @@ -65,6 +65,8 @@ func defaultPriorities() util.StringSet { factory.RegisterPriorityFunction("LeastRequestedPriority", priorities.LeastRequestedPriority, 1), // Prioritizes nodes to help achieve balanced resource usage factory.RegisterPriorityFunction("BalancedResourceAllocation", priorities.BalancedResourceAllocation, 1), + // Prioritizes nodes to achieve approximately equal number of pods per node + factory.RegisterPriorityFunction("DumbSpreadingPriority", priorities.DumbSpreadingPriority, 2), // spreads pods by minimizing the number of pods (belonging to the same service) on the same minion. factory.RegisterPriorityConfigFactory( "ServiceSpreadingPriority",