helpers.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2014 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 resource
  14. import (
  15. "fmt"
  16. "math"
  17. "strconv"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. )
  21. // addResourceList adds the resources in newList to list
  22. func addResourceList(list, new api.ResourceList) {
  23. for name, quantity := range new {
  24. if value, ok := list[name]; !ok {
  25. list[name] = *quantity.Copy()
  26. } else {
  27. value.Add(quantity)
  28. list[name] = value
  29. }
  30. }
  31. }
  32. // maxResourceList sets list to the greater of list/newList for every resource
  33. // either list
  34. func maxResourceList(list, new api.ResourceList) {
  35. for name, quantity := range new {
  36. if value, ok := list[name]; !ok {
  37. list[name] = *quantity.Copy()
  38. continue
  39. } else {
  40. if quantity.Cmp(value) > 0 {
  41. list[name] = *quantity.Copy()
  42. }
  43. }
  44. }
  45. }
  46. // PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
  47. // containers of the pod.
  48. func PodRequestsAndLimits(pod *api.Pod) (reqs api.ResourceList, limits api.ResourceList) {
  49. reqs, limits = api.ResourceList{}, api.ResourceList{}
  50. for _, container := range pod.Spec.Containers {
  51. addResourceList(reqs, container.Resources.Requests)
  52. addResourceList(limits, container.Resources.Limits)
  53. }
  54. // init containers define the minimum of any resource
  55. for _, container := range pod.Spec.InitContainers {
  56. maxResourceList(reqs, container.Resources.Requests)
  57. maxResourceList(limits, container.Resources.Limits)
  58. }
  59. return
  60. }
  61. // ExtractContainerResourceValue extracts the value of a resource
  62. // in an already known container
  63. func ExtractContainerResourceValue(fs *api.ResourceFieldSelector, container *api.Container) (string, error) {
  64. divisor := resource.Quantity{}
  65. if divisor.Cmp(fs.Divisor) == 0 {
  66. divisor = resource.MustParse("1")
  67. } else {
  68. divisor = fs.Divisor
  69. }
  70. switch fs.Resource {
  71. case "limits.cpu":
  72. return convertResourceCPUToString(container.Resources.Limits.Cpu(), divisor)
  73. case "limits.memory":
  74. return convertResourceMemoryToString(container.Resources.Limits.Memory(), divisor)
  75. case "limits.ephemeral-storage":
  76. return convertResourceEphemeralStorageToString(container.Resources.Limits.StorageEphemeral(), divisor)
  77. case "requests.cpu":
  78. return convertResourceCPUToString(container.Resources.Requests.Cpu(), divisor)
  79. case "requests.memory":
  80. return convertResourceMemoryToString(container.Resources.Requests.Memory(), divisor)
  81. case "requests.ephemeral-storage":
  82. return convertResourceEphemeralStorageToString(container.Resources.Requests.StorageEphemeral(), divisor)
  83. }
  84. return "", fmt.Errorf("unsupported container resource : %v", fs.Resource)
  85. }
  86. // convertResourceCPUToString converts cpu value to the format of divisor and returns
  87. // ceiling of the value.
  88. func convertResourceCPUToString(cpu *resource.Quantity, divisor resource.Quantity) (string, error) {
  89. c := int64(math.Ceil(float64(cpu.MilliValue()) / float64(divisor.MilliValue())))
  90. return strconv.FormatInt(c, 10), nil
  91. }
  92. // convertResourceMemoryToString converts memory value to the format of divisor and returns
  93. // ceiling of the value.
  94. func convertResourceMemoryToString(memory *resource.Quantity, divisor resource.Quantity) (string, error) {
  95. m := int64(math.Ceil(float64(memory.Value()) / float64(divisor.Value())))
  96. return strconv.FormatInt(m, 10), nil
  97. }
  98. // convertResourceEphemeralStorageToString converts ephemeral storage value to the format of divisor and returns
  99. // ceiling of the value.
  100. func convertResourceEphemeralStorageToString(ephemeralStorage *resource.Quantity, divisor resource.Quantity) (string, error) {
  101. m := int64(math.Ceil(float64(ephemeralStorage.Value()) / float64(divisor.Value())))
  102. return strconv.FormatInt(m, 10), nil
  103. }