resource_helpers.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 api
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. // Returns string version of ResourceName.
  20. func (self ResourceName) String() string {
  21. return string(self)
  22. }
  23. // Returns the CPU limit if specified.
  24. func (self *ResourceList) Cpu() *resource.Quantity {
  25. if val, ok := (*self)[ResourceCPU]; ok {
  26. return &val
  27. }
  28. return &resource.Quantity{Format: resource.DecimalSI}
  29. }
  30. // Returns the Memory limit if specified.
  31. func (self *ResourceList) Memory() *resource.Quantity {
  32. if val, ok := (*self)[ResourceMemory]; ok {
  33. return &val
  34. }
  35. return &resource.Quantity{Format: resource.BinarySI}
  36. }
  37. func (self *ResourceList) Pods() *resource.Quantity {
  38. if val, ok := (*self)[ResourcePods]; ok {
  39. return &val
  40. }
  41. return &resource.Quantity{}
  42. }
  43. func (self *ResourceList) NvidiaGPU() *resource.Quantity {
  44. if val, ok := (*self)[ResourceNvidiaGPU]; ok {
  45. return &val
  46. }
  47. return &resource.Quantity{}
  48. }
  49. func GetContainerStatus(statuses []ContainerStatus, name string) (ContainerStatus, bool) {
  50. for i := range statuses {
  51. if statuses[i].Name == name {
  52. return statuses[i], true
  53. }
  54. }
  55. return ContainerStatus{}, false
  56. }
  57. func GetExistingContainerStatus(statuses []ContainerStatus, name string) ContainerStatus {
  58. for i := range statuses {
  59. if statuses[i].Name == name {
  60. return statuses[i]
  61. }
  62. }
  63. return ContainerStatus{}
  64. }
  65. // IsPodAvailable returns true if a pod is available; false otherwise.
  66. // Precondition for an available pod is that it must be ready. On top
  67. // of that, there are two cases when a pod can be considered available:
  68. // 1. minReadySeconds == 0, or
  69. // 2. LastTransitionTime (is set) + minReadySeconds < current time
  70. func IsPodAvailable(pod *Pod, minReadySeconds int32, now metav1.Time) bool {
  71. if !IsPodReady(pod) {
  72. return false
  73. }
  74. c := GetPodReadyCondition(pod.Status)
  75. minReadySecondsDuration := time.Duration(minReadySeconds) * time.Second
  76. if minReadySeconds == 0 || !c.LastTransitionTime.IsZero() && c.LastTransitionTime.Add(minReadySecondsDuration).Before(now.Time) {
  77. return true
  78. }
  79. return false
  80. }
  81. // IsPodReady returns true if a pod is ready; false otherwise.
  82. func IsPodReady(pod *Pod) bool {
  83. return IsPodReadyConditionTrue(pod.Status)
  84. }
  85. // IsPodReady retruns true if a pod is ready; false otherwise.
  86. func IsPodReadyConditionTrue(status PodStatus) bool {
  87. condition := GetPodReadyCondition(status)
  88. return condition != nil && condition.Status == ConditionTrue
  89. }
  90. // Extracts the pod ready condition from the given status and returns that.
  91. // Returns nil if the condition is not present.
  92. func GetPodReadyCondition(status PodStatus) *PodCondition {
  93. _, condition := GetPodCondition(&status, PodReady)
  94. return condition
  95. }
  96. // GetPodCondition extracts the provided condition from the given status and returns that.
  97. // Returns nil and -1 if the condition is not present, and the index of the located condition.
  98. func GetPodCondition(status *PodStatus, conditionType PodConditionType) (int, *PodCondition) {
  99. if status == nil {
  100. return -1, nil
  101. }
  102. for i := range status.Conditions {
  103. if status.Conditions[i].Type == conditionType {
  104. return i, &status.Conditions[i]
  105. }
  106. }
  107. return -1, nil
  108. }
  109. // GetNodeCondition extracts the provided condition from the given status and returns that.
  110. // Returns nil and -1 if the condition is not present, and the index of the located condition.
  111. func GetNodeCondition(status *NodeStatus, conditionType NodeConditionType) (int, *NodeCondition) {
  112. if status == nil {
  113. return -1, nil
  114. }
  115. for i := range status.Conditions {
  116. if status.Conditions[i].Type == conditionType {
  117. return i, &status.Conditions[i]
  118. }
  119. }
  120. return -1, nil
  121. }
  122. // Updates existing pod condition or creates a new one. Sets LastTransitionTime to now if the
  123. // status has changed.
  124. // Returns true if pod condition has changed or has been added.
  125. func UpdatePodCondition(status *PodStatus, condition *PodCondition) bool {
  126. condition.LastTransitionTime = metav1.Now()
  127. // Try to find this pod condition.
  128. conditionIndex, oldCondition := GetPodCondition(status, condition.Type)
  129. if oldCondition == nil {
  130. // We are adding new pod condition.
  131. status.Conditions = append(status.Conditions, *condition)
  132. return true
  133. } else {
  134. // We are updating an existing condition, so we need to check if it has changed.
  135. if condition.Status == oldCondition.Status {
  136. condition.LastTransitionTime = oldCondition.LastTransitionTime
  137. }
  138. isEqual := condition.Status == oldCondition.Status &&
  139. condition.Reason == oldCondition.Reason &&
  140. condition.Message == oldCondition.Message &&
  141. condition.LastProbeTime.Equal(oldCondition.LastProbeTime) &&
  142. condition.LastTransitionTime.Equal(oldCondition.LastTransitionTime)
  143. status.Conditions[conditionIndex] = *condition
  144. // Return true if one of the fields have changed.
  145. return !isEqual
  146. }
  147. }
  148. // IsNodeReady returns true if a node is ready; false otherwise.
  149. func IsNodeReady(node *Node) bool {
  150. for _, c := range node.Status.Conditions {
  151. if c.Type == NodeReady {
  152. return c.Status == ConditionTrue
  153. }
  154. }
  155. return false
  156. }
  157. // PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
  158. // containers of the pod.
  159. func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, limits map[ResourceName]resource.Quantity, err error) {
  160. reqs, limits = map[ResourceName]resource.Quantity{}, map[ResourceName]resource.Quantity{}
  161. for _, container := range pod.Spec.Containers {
  162. for name, quantity := range container.Resources.Requests {
  163. if value, ok := reqs[name]; !ok {
  164. reqs[name] = *quantity.Copy()
  165. } else {
  166. value.Add(quantity)
  167. reqs[name] = value
  168. }
  169. }
  170. for name, quantity := range container.Resources.Limits {
  171. if value, ok := limits[name]; !ok {
  172. limits[name] = *quantity.Copy()
  173. } else {
  174. value.Add(quantity)
  175. limits[name] = value
  176. }
  177. }
  178. }
  179. // init containers define the minimum of any resource
  180. for _, container := range pod.Spec.InitContainers {
  181. for name, quantity := range container.Resources.Requests {
  182. value, ok := reqs[name]
  183. if !ok {
  184. reqs[name] = *quantity.Copy()
  185. continue
  186. }
  187. if quantity.Cmp(value) > 0 {
  188. reqs[name] = *quantity.Copy()
  189. }
  190. }
  191. for name, quantity := range container.Resources.Limits {
  192. value, ok := limits[name]
  193. if !ok {
  194. limits[name] = *quantity.Copy()
  195. continue
  196. }
  197. if quantity.Cmp(value) > 0 {
  198. limits[name] = *quantity.Copy()
  199. }
  200. }
  201. }
  202. return
  203. }