qos.go 2.8 KB

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