qos.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // NOTE: DO NOT use those helper functions through client-go, the
  14. // package path will be changed in the future.
  15. package qos
  16. import (
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/kubernetes/pkg/apis/core"
  20. )
  21. var supportedQoSComputeResources = sets.NewString(string(core.ResourceCPU), string(core.ResourceMemory))
  22. func isSupportedQoSComputeResource(name core.ResourceName) bool {
  23. return supportedQoSComputeResources.Has(string(name))
  24. }
  25. // GetPodQOS returns the QoS class of a pod.
  26. // A pod is besteffort if none of its containers have specified any requests or limits.
  27. // A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
  28. // A pod is burstable if limits and requests do not match across all containers.
  29. func GetPodQOS(pod *core.Pod) core.PodQOSClass {
  30. requests := core.ResourceList{}
  31. limits := core.ResourceList{}
  32. zeroQuantity := resource.MustParse("0")
  33. isGuaranteed := true
  34. allContainers := []core.Container{}
  35. allContainers = append(allContainers, pod.Spec.Containers...)
  36. allContainers = append(allContainers, pod.Spec.InitContainers...)
  37. for _, container := range allContainers {
  38. // process requests
  39. for name, quantity := range container.Resources.Requests {
  40. if !isSupportedQoSComputeResource(name) {
  41. continue
  42. }
  43. if quantity.Cmp(zeroQuantity) == 1 {
  44. delta := quantity.DeepCopy()
  45. if _, exists := requests[name]; !exists {
  46. requests[name] = delta
  47. } else {
  48. delta.Add(requests[name])
  49. requests[name] = delta
  50. }
  51. }
  52. }
  53. // process limits
  54. qosLimitsFound := sets.NewString()
  55. for name, quantity := range container.Resources.Limits {
  56. if !isSupportedQoSComputeResource(name) {
  57. continue
  58. }
  59. if quantity.Cmp(zeroQuantity) == 1 {
  60. qosLimitsFound.Insert(string(name))
  61. delta := quantity.DeepCopy()
  62. if _, exists := limits[name]; !exists {
  63. limits[name] = delta
  64. } else {
  65. delta.Add(limits[name])
  66. limits[name] = delta
  67. }
  68. }
  69. }
  70. if !qosLimitsFound.HasAll(string(core.ResourceMemory), string(core.ResourceCPU)) {
  71. isGuaranteed = false
  72. }
  73. }
  74. if len(requests) == 0 && len(limits) == 0 {
  75. return core.PodQOSBestEffort
  76. }
  77. // Check is requests match limits for all resources.
  78. if isGuaranteed {
  79. for name, req := range requests {
  80. if lim, exists := limits[name]; !exists || lim.Cmp(req) != 0 {
  81. isGuaranteed = false
  82. break
  83. }
  84. }
  85. }
  86. if isGuaranteed &&
  87. len(requests) == len(limits) {
  88. return core.PodQOSGuaranteed
  89. }
  90. return core.PodQOSBurstable
  91. }