most_requested.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. mostResourcePriority = &ResourceAllocationPriority{"MostResourceAllocation", mostResourceScorer}
  20. // MostRequestedPriorityMap is a priority function that favors nodes with most requested resources.
  21. // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
  22. // based on the maximum of the average of the fraction of requested to capacity.
  23. // Details: (cpu(10 * sum(requested) / capacity) + memory(10 * sum(requested) / capacity)) / 2
  24. MostRequestedPriorityMap = mostResourcePriority.PriorityMap
  25. )
  26. func mostResourceScorer(requested, allocable *schedulernodeinfo.Resource, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
  27. return (mostRequestedScore(requested.MilliCPU, allocable.MilliCPU) +
  28. mostRequestedScore(requested.Memory, allocable.Memory)) / 2
  29. }
  30. // The used capacity is calculated on a scale of 0-10
  31. // 0 being the lowest priority and 10 being the highest.
  32. // The more resources are used the higher the score is. This function
  33. // is almost a reversed version of least_requested_priority.calculateUnusedScore
  34. // (10 - calculateUnusedScore). The main difference is in rounding. It was added to
  35. // keep the final formula clean and not to modify the widely used (by users
  36. // in their default scheduling policies) calculateUsedScore.
  37. func mostRequestedScore(requested, capacity int64) int64 {
  38. if capacity == 0 {
  39. return 0
  40. }
  41. if requested > capacity {
  42. return 0
  43. }
  44. return (requested * schedulerapi.MaxPriority) / capacity
  45. }