least_requested.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package priorities
  14. import (
  15. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  16. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  17. )
  18. var (
  19. leastResourcePriority = &ResourceAllocationPriority{"LeastResourceAllocation", leastResourceScorer}
  20. // LeastRequestedPriorityMap is a priority function that favors nodes with fewer requested resources.
  21. // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and
  22. // prioritizes based on the minimum of the average of the fraction of requested to capacity.
  23. //
  24. // Details:
  25. // (cpu((capacity-sum(requested))*10/capacity) + memory((capacity-sum(requested))*10/capacity))/2
  26. LeastRequestedPriorityMap = leastResourcePriority.PriorityMap
  27. )
  28. func leastResourceScorer(requested, allocable *schedulernodeinfo.Resource, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
  29. return (leastRequestedScore(requested.MilliCPU, allocable.MilliCPU) +
  30. leastRequestedScore(requested.Memory, allocable.Memory)) / 2
  31. }
  32. // The unused capacity is calculated on a scale of 0-10
  33. // 0 being the lowest priority and 10 being the highest.
  34. // The more unused resources the higher the score is.
  35. func leastRequestedScore(requested, capacity int64) int64 {
  36. if capacity == 0 {
  37. return 0
  38. }
  39. if requested > capacity {
  40. return 0
  41. }
  42. return ((capacity - requested) * int64(schedulerapi.MaxPriority)) / capacity
  43. }