resource_allocation.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2017 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. v1 "k8s.io/api/core/v1"
  16. utilfeature "k8s.io/apiserver/pkg/util/feature"
  17. "k8s.io/klog"
  18. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  19. "k8s.io/kubernetes/pkg/features"
  20. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  21. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  22. schedutil "k8s.io/kubernetes/pkg/scheduler/util"
  23. )
  24. // resourceToWeightMap contains resource name and weight.
  25. type resourceToWeightMap map[v1.ResourceName]int64
  26. // defaultRequestedRatioResources is used to set default requestToWeight map for CPU and memory
  27. var defaultRequestedRatioResources = resourceToWeightMap{v1.ResourceMemory: 1, v1.ResourceCPU: 1}
  28. // resourceAllocationScorer contains information to calculate resource allocation score.
  29. type resourceAllocationScorer struct {
  30. Name string
  31. scorer func(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64
  32. resourceToWeightMap resourceToWeightMap
  33. }
  34. // resourceToValueMap contains resource name and score.
  35. type resourceToValueMap map[v1.ResourceName]int64
  36. // score will use `scorer` function to calculate the score.
  37. func (r *resourceAllocationScorer) score(
  38. pod *v1.Pod,
  39. nodeInfo *schedulernodeinfo.NodeInfo) (int64, *framework.Status) {
  40. node := nodeInfo.Node()
  41. if node == nil {
  42. return 0, framework.NewStatus(framework.Error, "node not found")
  43. }
  44. if r.resourceToWeightMap == nil {
  45. return 0, framework.NewStatus(framework.Error, "resources not found")
  46. }
  47. requested := make(resourceToValueMap, len(r.resourceToWeightMap))
  48. allocatable := make(resourceToValueMap, len(r.resourceToWeightMap))
  49. for resource := range r.resourceToWeightMap {
  50. allocatable[resource], requested[resource] = calculateResourceAllocatableRequest(nodeInfo, pod, resource)
  51. }
  52. var score int64
  53. // Check if the pod has volumes and this could be added to scorer function for balanced resource allocation.
  54. if len(pod.Spec.Volumes) >= 0 && utilfeature.DefaultFeatureGate.Enabled(features.BalanceAttachedNodeVolumes) && nodeInfo.TransientInfo != nil {
  55. score = r.scorer(requested, allocatable, true, nodeInfo.TransientInfo.TransNodeInfo.RequestedVolumes, nodeInfo.TransientInfo.TransNodeInfo.AllocatableVolumesCount)
  56. } else {
  57. score = r.scorer(requested, allocatable, false, 0, 0)
  58. }
  59. if klog.V(10) {
  60. if len(pod.Spec.Volumes) >= 0 && utilfeature.DefaultFeatureGate.Enabled(features.BalanceAttachedNodeVolumes) && nodeInfo.TransientInfo != nil {
  61. klog.Infof(
  62. "%v -> %v: %v, map of allocatable resources %v, map of requested resources %v , allocatable volumes %d, requested volumes %d, score %d",
  63. pod.Name, node.Name, r.Name,
  64. allocatable, requested, nodeInfo.TransientInfo.TransNodeInfo.AllocatableVolumesCount,
  65. nodeInfo.TransientInfo.TransNodeInfo.RequestedVolumes,
  66. score,
  67. )
  68. } else {
  69. klog.Infof(
  70. "%v -> %v: %v, map of allocatable resources %v, map of requested resources %v ,score %d,",
  71. pod.Name, node.Name, r.Name,
  72. allocatable, requested, score,
  73. )
  74. }
  75. }
  76. return score, nil
  77. }
  78. // calculateResourceAllocatableRequest returns resources Allocatable and Requested values
  79. func calculateResourceAllocatableRequest(nodeInfo *schedulernodeinfo.NodeInfo, pod *v1.Pod, resource v1.ResourceName) (int64, int64) {
  80. allocatable := nodeInfo.AllocatableResource()
  81. requested := nodeInfo.RequestedResource()
  82. podRequest := calculatePodResourceRequest(pod, resource)
  83. switch resource {
  84. case v1.ResourceCPU:
  85. return allocatable.MilliCPU, (nodeInfo.NonZeroRequest().MilliCPU + podRequest)
  86. case v1.ResourceMemory:
  87. return allocatable.Memory, (nodeInfo.NonZeroRequest().Memory + podRequest)
  88. case v1.ResourceEphemeralStorage:
  89. return allocatable.EphemeralStorage, (requested.EphemeralStorage + podRequest)
  90. default:
  91. if v1helper.IsScalarResourceName(resource) {
  92. return allocatable.ScalarResources[resource], (requested.ScalarResources[resource] + podRequest)
  93. }
  94. }
  95. if klog.V(10) {
  96. klog.Infof("requested resource %v not considered for node score calculation",
  97. resource,
  98. )
  99. }
  100. return 0, 0
  101. }
  102. // calculatePodResourceRequest returns the total non-zero requests. If Overhead is defined for the pod and the
  103. // PodOverhead feature is enabled, the Overhead is added to the result.
  104. func calculatePodResourceRequest(pod *v1.Pod, resource v1.ResourceName) int64 {
  105. var podRequest int64
  106. for i := range pod.Spec.Containers {
  107. container := &pod.Spec.Containers[i]
  108. value := schedutil.GetNonzeroRequestForResource(resource, &container.Resources.Requests)
  109. podRequest += value
  110. }
  111. // If Overhead is being utilized, add to the total requests for the pod
  112. if pod.Spec.Overhead != nil && utilfeature.DefaultFeatureGate.Enabled(features.PodOverhead) {
  113. if quantity, found := pod.Spec.Overhead[resource]; found {
  114. podRequest += quantity.Value()
  115. }
  116. }
  117. return podRequest
  118. }