resources.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright 2016 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 quota
  14. import (
  15. "k8s.io/kubernetes/pkg/api"
  16. "k8s.io/kubernetes/pkg/api/resource"
  17. "k8s.io/kubernetes/pkg/util/sets"
  18. )
  19. // Equals returns true if the two lists are equivalent
  20. func Equals(a api.ResourceList, b api.ResourceList) bool {
  21. for key, value1 := range a {
  22. value2, found := b[key]
  23. if !found {
  24. return false
  25. }
  26. if value1.Cmp(value2) != 0 {
  27. return false
  28. }
  29. }
  30. for key, value1 := range b {
  31. value2, found := a[key]
  32. if !found {
  33. return false
  34. }
  35. if value1.Cmp(value2) != 0 {
  36. return false
  37. }
  38. }
  39. return true
  40. }
  41. // LessThanOrEqual returns true if a < b for each key in b
  42. // If false, it returns the keys in a that exceeded b
  43. func LessThanOrEqual(a api.ResourceList, b api.ResourceList) (bool, []api.ResourceName) {
  44. result := true
  45. resourceNames := []api.ResourceName{}
  46. for key, value := range b {
  47. if other, found := a[key]; found {
  48. if other.Cmp(value) > 0 {
  49. result = false
  50. resourceNames = append(resourceNames, key)
  51. }
  52. }
  53. }
  54. return result, resourceNames
  55. }
  56. // Max returns the result of Max(a, b) for each named resource
  57. func Max(a api.ResourceList, b api.ResourceList) api.ResourceList {
  58. result := api.ResourceList{}
  59. for key, value := range a {
  60. if other, found := b[key]; found {
  61. if value.Cmp(other) <= 0 {
  62. result[key] = *other.Copy()
  63. continue
  64. }
  65. }
  66. result[key] = *value.Copy()
  67. }
  68. for key, value := range b {
  69. if _, found := result[key]; !found {
  70. result[key] = *value.Copy()
  71. }
  72. }
  73. return result
  74. }
  75. // Add returns the result of a + b for each named resource
  76. func Add(a api.ResourceList, b api.ResourceList) api.ResourceList {
  77. result := api.ResourceList{}
  78. for key, value := range a {
  79. quantity := *value.Copy()
  80. if other, found := b[key]; found {
  81. quantity.Add(other)
  82. }
  83. result[key] = quantity
  84. }
  85. for key, value := range b {
  86. if _, found := result[key]; !found {
  87. quantity := *value.Copy()
  88. result[key] = quantity
  89. }
  90. }
  91. return result
  92. }
  93. // Subtract returns the result of a - b for each named resource
  94. func Subtract(a api.ResourceList, b api.ResourceList) api.ResourceList {
  95. result := api.ResourceList{}
  96. for key, value := range a {
  97. quantity := *value.Copy()
  98. if other, found := b[key]; found {
  99. quantity.Sub(other)
  100. }
  101. result[key] = quantity
  102. }
  103. for key, value := range b {
  104. if _, found := result[key]; !found {
  105. quantity := *value.Copy()
  106. quantity.Neg()
  107. result[key] = quantity
  108. }
  109. }
  110. return result
  111. }
  112. // Mask returns a new resource list that only has the values with the specified names
  113. func Mask(resources api.ResourceList, names []api.ResourceName) api.ResourceList {
  114. nameSet := ToSet(names)
  115. result := api.ResourceList{}
  116. for key, value := range resources {
  117. if nameSet.Has(string(key)) {
  118. result[key] = *value.Copy()
  119. }
  120. }
  121. return result
  122. }
  123. // ResourceNames returns a list of all resource names in the ResourceList
  124. func ResourceNames(resources api.ResourceList) []api.ResourceName {
  125. result := []api.ResourceName{}
  126. for resourceName := range resources {
  127. result = append(result, resourceName)
  128. }
  129. return result
  130. }
  131. // Contains returns true if the specified item is in the list of items
  132. func Contains(items []api.ResourceName, item api.ResourceName) bool {
  133. return ToSet(items).Has(string(item))
  134. }
  135. // Intersection returns the intersection of both list of resources
  136. func Intersection(a []api.ResourceName, b []api.ResourceName) []api.ResourceName {
  137. setA := ToSet(a)
  138. setB := ToSet(b)
  139. setC := setA.Intersection(setB)
  140. result := []api.ResourceName{}
  141. for _, resourceName := range setC.List() {
  142. result = append(result, api.ResourceName(resourceName))
  143. }
  144. return result
  145. }
  146. // IsZero returns true if each key maps to the quantity value 0
  147. func IsZero(a api.ResourceList) bool {
  148. zero := resource.MustParse("0")
  149. for _, v := range a {
  150. if v.Cmp(zero) != 0 {
  151. return false
  152. }
  153. }
  154. return true
  155. }
  156. // IsNegative returns the set of resource names that have a negative value.
  157. func IsNegative(a api.ResourceList) []api.ResourceName {
  158. results := []api.ResourceName{}
  159. zero := resource.MustParse("0")
  160. for k, v := range a {
  161. if v.Cmp(zero) < 0 {
  162. results = append(results, k)
  163. }
  164. }
  165. return results
  166. }
  167. // ToSet takes a list of resource names and converts to a string set
  168. func ToSet(resourceNames []api.ResourceName) sets.String {
  169. result := sets.NewString()
  170. for _, resourceName := range resourceNames {
  171. result.Insert(string(resourceName))
  172. }
  173. return result
  174. }
  175. // CalculateUsage calculates and returns the requested ResourceList usage
  176. func CalculateUsage(namespaceName string, scopes []api.ResourceQuotaScope, hardLimits api.ResourceList, registry Registry) (api.ResourceList, error) {
  177. // find the intersection between the hard resources on the quota
  178. // and the resources this controller can track to know what we can
  179. // look to measure updated usage stats for
  180. hardResources := ResourceNames(hardLimits)
  181. potentialResources := []api.ResourceName{}
  182. evaluators := registry.Evaluators()
  183. for _, evaluator := range evaluators {
  184. potentialResources = append(potentialResources, evaluator.MatchesResources()...)
  185. }
  186. matchedResources := Intersection(hardResources, potentialResources)
  187. // sum the observed usage from each evaluator
  188. newUsage := api.ResourceList{}
  189. usageStatsOptions := UsageStatsOptions{Namespace: namespaceName, Scopes: scopes}
  190. for _, evaluator := range evaluators {
  191. // only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything
  192. if intersection := Intersection(evaluator.MatchesResources(), matchedResources); len(intersection) == 0 {
  193. continue
  194. }
  195. stats, err := evaluator.UsageStats(usageStatsOptions)
  196. if err != nil {
  197. return nil, err
  198. }
  199. newUsage = Add(newUsage, stats.Used)
  200. }
  201. // mask the observed usage to only the set of resources tracked by this quota
  202. // merge our observed usage with the quota usage status
  203. // if the new usage is different than the last usage, we will need to do an update
  204. newUsage = Mask(newUsage, matchedResources)
  205. return newUsage, nil
  206. }