qos.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. for _, container := range pod.Spec.Containers {
  35. // process requests
  36. for name, quantity := range container.Resources.Requests {
  37. if !isSupportedQoSComputeResource(name) {
  38. continue
  39. }
  40. if quantity.Cmp(zeroQuantity) == 1 {
  41. delta := quantity.Copy()
  42. if _, exists := requests[name]; !exists {
  43. requests[name] = *delta
  44. } else {
  45. delta.Add(requests[name])
  46. requests[name] = *delta
  47. }
  48. }
  49. }
  50. // process limits
  51. qosLimitsFound := sets.NewString()
  52. for name, quantity := range container.Resources.Limits {
  53. if !isSupportedQoSComputeResource(name) {
  54. continue
  55. }
  56. if quantity.Cmp(zeroQuantity) == 1 {
  57. qosLimitsFound.Insert(string(name))
  58. delta := quantity.Copy()
  59. if _, exists := limits[name]; !exists {
  60. limits[name] = *delta
  61. } else {
  62. delta.Add(limits[name])
  63. limits[name] = *delta
  64. }
  65. }
  66. }
  67. if !qosLimitsFound.HasAll(string(core.ResourceMemory), string(core.ResourceCPU)) {
  68. isGuaranteed = false
  69. }
  70. }
  71. if len(requests) == 0 && len(limits) == 0 {
  72. return core.PodQOSBestEffort
  73. }
  74. // Check is requests match limits for all resources.
  75. if isGuaranteed {
  76. for name, req := range requests {
  77. if lim, exists := limits[name]; !exists || lim.Cmp(req) != 0 {
  78. isGuaranteed = false
  79. break
  80. }
  81. }
  82. }
  83. if isGuaranteed &&
  84. len(requests) == len(limits) {
  85. return core.PodQOSGuaranteed
  86. }
  87. return core.PodQOSBurstable
  88. }