least_allocated.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2019 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 noderesources
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  20. )
  21. // LeastAllocated is a score plugin that favors nodes with fewer allocation requested resources based on requested resources.
  22. type LeastAllocated struct {
  23. handle framework.FrameworkHandle
  24. resourceAllocationScorer
  25. }
  26. var _ = framework.ScorePlugin(&LeastAllocated{})
  27. // LeastAllocatedName is the name of the plugin used in the plugin registry and configurations.
  28. const LeastAllocatedName = "NodeResourcesLeastAllocated"
  29. // Name returns name of the plugin. It is used in logs, etc.
  30. func (la *LeastAllocated) Name() string {
  31. return LeastAllocatedName
  32. }
  33. // Score invoked at the score extension point.
  34. func (la *LeastAllocated) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
  35. nodeInfo, err := la.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
  36. if err != nil {
  37. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
  38. }
  39. // la.score favors nodes with fewer requested resources.
  40. // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and
  41. // prioritizes based on the minimum of the average of the fraction of requested to capacity.
  42. //
  43. // Details:
  44. // (cpu((capacity-sum(requested))*10/capacity) + memory((capacity-sum(requested))*10/capacity))/2
  45. return la.score(pod, nodeInfo)
  46. }
  47. // ScoreExtensions of the Score plugin.
  48. func (la *LeastAllocated) ScoreExtensions() framework.ScoreExtensions {
  49. return nil
  50. }
  51. // NewLeastAllocated initializes a new plugin and returns it.
  52. func NewLeastAllocated(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  53. return &LeastAllocated{
  54. handle: h,
  55. resourceAllocationScorer: resourceAllocationScorer{
  56. LeastAllocatedName,
  57. leastResourceScorer,
  58. defaultRequestedRatioResources,
  59. },
  60. }, nil
  61. }
  62. func leastResourceScorer(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
  63. var nodeScore, weightSum int64
  64. for resource, weight := range defaultRequestedRatioResources {
  65. resourceScore := leastRequestedScore(requested[resource], allocable[resource])
  66. nodeScore += resourceScore * weight
  67. weightSum += weight
  68. }
  69. return nodeScore / weightSum
  70. }
  71. // The unused capacity is calculated on a scale of 0-10
  72. // 0 being the lowest priority and 10 being the highest.
  73. // The more unused resources the higher the score is.
  74. func leastRequestedScore(requested, capacity int64) int64 {
  75. if capacity == 0 {
  76. return 0
  77. }
  78. if requested > capacity {
  79. return 0
  80. }
  81. return ((capacity - requested) * int64(framework.MaxNodeScore)) / capacity
  82. }