most_allocated.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // MostAllocated is a score plugin that favors nodes with high allocation based on requested resources.
  22. type MostAllocated struct {
  23. handle framework.FrameworkHandle
  24. resourceAllocationScorer
  25. }
  26. var _ = framework.ScorePlugin(&MostAllocated{})
  27. // MostAllocatedName is the name of the plugin used in the plugin registry and configurations.
  28. const MostAllocatedName = "NodeResourcesMostAllocated"
  29. // Name returns name of the plugin. It is used in logs, etc.
  30. func (ma *MostAllocated) Name() string {
  31. return MostAllocatedName
  32. }
  33. // Score invoked at the Score extension point.
  34. func (ma *MostAllocated) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
  35. nodeInfo, err := ma.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
  36. if err != nil || nodeInfo.Node() == nil {
  37. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v, node is nil: %v", nodeName, err, nodeInfo.Node() == nil))
  38. }
  39. // ma.score favors nodes with most requested resources.
  40. // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
  41. // based on the maximum of the average of the fraction of requested to capacity.
  42. // Details: (cpu(10 * sum(requested) / capacity) + memory(10 * sum(requested) / capacity)) / 2
  43. return ma.score(pod, nodeInfo)
  44. }
  45. // ScoreExtensions of the Score plugin.
  46. func (ma *MostAllocated) ScoreExtensions() framework.ScoreExtensions {
  47. return nil
  48. }
  49. // NewMostAllocated initializes a new plugin and returns it.
  50. func NewMostAllocated(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  51. return &MostAllocated{
  52. handle: h,
  53. resourceAllocationScorer: resourceAllocationScorer{
  54. MostAllocatedName,
  55. mostResourceScorer,
  56. defaultRequestedRatioResources,
  57. },
  58. }, nil
  59. }
  60. func mostResourceScorer(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
  61. var nodeScore, weightSum int64
  62. for resource, weight := range defaultRequestedRatioResources {
  63. resourceScore := mostRequestedScore(requested[resource], allocable[resource])
  64. nodeScore += resourceScore * weight
  65. weightSum += weight
  66. }
  67. return (nodeScore / weightSum)
  68. }
  69. // The used capacity is calculated on a scale of 0-10
  70. // 0 being the lowest priority and 10 being the highest.
  71. // The more resources are used the higher the score is. This function
  72. // is almost a reversed version of least_requested_priority.calculateUnusedScore
  73. // (10 - calculateUnusedScore). The main difference is in rounding. It was added to
  74. // keep the final formula clean and not to modify the widely used (by users
  75. // in their default scheduling policies) calculateUsedScore.
  76. func mostRequestedScore(requested, capacity int64) int64 {
  77. if capacity == 0 {
  78. return 0
  79. }
  80. if requested > capacity {
  81. return 0
  82. }
  83. return (requested * framework.MaxNodeScore) / capacity
  84. }