resource_limits.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  21. )
  22. // ResourceLimits is a score plugin that increases score of input node by 1 if the node satisfies
  23. // input pod's resource limits
  24. type ResourceLimits struct {
  25. handle framework.FrameworkHandle
  26. }
  27. var _ = framework.PreScorePlugin(&ResourceLimits{})
  28. var _ = framework.ScorePlugin(&ResourceLimits{})
  29. const (
  30. // ResourceLimitsName is the name of the plugin used in the plugin registry and configurations.
  31. ResourceLimitsName = "NodeResourceLimits"
  32. // preScoreStateKey is the key in CycleState to NodeResourceLimits pre-computed data.
  33. // Using the name of the plugin will likely help us avoid collisions with other plugins.
  34. preScoreStateKey = "PreScore" + ResourceLimitsName
  35. )
  36. // preScoreState computed at PreScore and used at Score.
  37. type preScoreState struct {
  38. podResourceRequest *schedulernodeinfo.Resource
  39. }
  40. // Clone the preScore state.
  41. func (s *preScoreState) Clone() framework.StateData {
  42. return s
  43. }
  44. // Name returns name of the plugin. It is used in logs, etc.
  45. func (rl *ResourceLimits) Name() string {
  46. return ResourceLimitsName
  47. }
  48. // PreScore builds and writes cycle state used by Score and NormalizeScore.
  49. func (rl *ResourceLimits) PreScore(
  50. pCtx context.Context,
  51. cycleState *framework.CycleState,
  52. pod *v1.Pod,
  53. nodes []*v1.Node,
  54. ) *framework.Status {
  55. if len(nodes) == 0 {
  56. // No nodes to score.
  57. return nil
  58. }
  59. if rl.handle.SnapshotSharedLister() == nil {
  60. return framework.NewStatus(framework.Error, fmt.Sprintf("empty shared lister"))
  61. }
  62. s := &preScoreState{
  63. podResourceRequest: getResourceLimits(pod),
  64. }
  65. cycleState.Write(preScoreStateKey, s)
  66. return nil
  67. }
  68. func getPodResource(cycleState *framework.CycleState) (*schedulernodeinfo.Resource, error) {
  69. c, err := cycleState.Read(preScoreStateKey)
  70. if err != nil {
  71. return nil, fmt.Errorf("Error reading %q from cycleState: %v", preScoreStateKey, err)
  72. }
  73. s, ok := c.(*preScoreState)
  74. if !ok {
  75. return nil, fmt.Errorf("%+v convert to ResourceLimits.preScoreState error", c)
  76. }
  77. return s.podResourceRequest, nil
  78. }
  79. // Score invoked at the Score extension point.
  80. // The "score" returned in this function is the matching number of pods on the `nodeName`.
  81. // Currently works as follows:
  82. // If a node does not publish its allocatable resources (cpu and memory both), the node score is not affected.
  83. // If a pod does not specify its cpu and memory limits both, the node score is not affected.
  84. // If one or both of cpu and memory limits of the pod are satisfied, the node is assigned a score of 1.
  85. // Rationale of choosing the lowest score of 1 is that this is mainly selected to break ties between nodes that have
  86. // same scores assigned by one of least and most requested priority functions.
  87. func (rl *ResourceLimits) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
  88. nodeInfo, err := rl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
  89. if err != nil || nodeInfo.Node() == nil {
  90. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v, node is nil: %v", nodeName, err, nodeInfo.Node() == nil))
  91. }
  92. allocatableResources := nodeInfo.AllocatableResource()
  93. podLimits, err := getPodResource(state)
  94. if err != nil {
  95. return 0, framework.NewStatus(framework.Error, err.Error())
  96. }
  97. cpuScore := computeScore(podLimits.MilliCPU, allocatableResources.MilliCPU)
  98. memScore := computeScore(podLimits.Memory, allocatableResources.Memory)
  99. score := int64(0)
  100. if cpuScore == 1 || memScore == 1 {
  101. score = 1
  102. }
  103. return score, nil
  104. }
  105. // ScoreExtensions of the Score plugin.
  106. func (rl *ResourceLimits) ScoreExtensions() framework.ScoreExtensions {
  107. return nil
  108. }
  109. // NewResourceLimits initializes a new plugin and returns it.
  110. func NewResourceLimits(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  111. return &ResourceLimits{handle: h}, nil
  112. }
  113. // getResourceLimits computes resource limits for input pod.
  114. // The reason to create this new function is to be consistent with other
  115. // priority functions because most or perhaps all priority functions work
  116. // with schedulernodeinfo.Resource.
  117. func getResourceLimits(pod *v1.Pod) *schedulernodeinfo.Resource {
  118. result := &schedulernodeinfo.Resource{}
  119. for _, container := range pod.Spec.Containers {
  120. result.Add(container.Resources.Limits)
  121. }
  122. // take max_resource(sum_pod, any_init_container)
  123. for _, container := range pod.Spec.InitContainers {
  124. result.SetMaxResource(container.Resources.Limits)
  125. }
  126. return result
  127. }
  128. // computeScore returns 1 if limit value is less than or equal to allocatable
  129. // value, otherwise it returns 0.
  130. func computeScore(limit, allocatable int64) int64 {
  131. if limit != 0 && allocatable != 0 && limit <= allocatable {
  132. return 1
  133. }
  134. return 0
  135. }