resource_helpers.go 6.1 KB

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