resources.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. "sort"
  16. "strings"
  17. corev1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  20. "k8s.io/apimachinery/pkg/util/sets"
  21. )
  22. // Equals returns true if the two lists are equivalent
  23. func Equals(a corev1.ResourceList, b corev1.ResourceList) bool {
  24. if len(a) != len(b) {
  25. return false
  26. }
  27. for key, value1 := range a {
  28. value2, found := b[key]
  29. if !found {
  30. return false
  31. }
  32. if value1.Cmp(value2) != 0 {
  33. return false
  34. }
  35. }
  36. return true
  37. }
  38. // V1Equals returns true if the two lists are equivalent
  39. func V1Equals(a corev1.ResourceList, b corev1.ResourceList) bool {
  40. if len(a) != len(b) {
  41. return false
  42. }
  43. for key, value1 := range a {
  44. value2, found := b[key]
  45. if !found {
  46. return false
  47. }
  48. if value1.Cmp(value2) != 0 {
  49. return false
  50. }
  51. }
  52. return true
  53. }
  54. // LessThanOrEqual returns true if a < b for each key in b
  55. // If false, it returns the keys in a that exceeded b
  56. func LessThanOrEqual(a corev1.ResourceList, b corev1.ResourceList) (bool, []corev1.ResourceName) {
  57. result := true
  58. resourceNames := []corev1.ResourceName{}
  59. for key, value := range b {
  60. if other, found := a[key]; found {
  61. if other.Cmp(value) > 0 {
  62. result = false
  63. resourceNames = append(resourceNames, key)
  64. }
  65. }
  66. }
  67. return result, resourceNames
  68. }
  69. // Max returns the result of Max(a, b) for each named resource
  70. func Max(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
  71. result := corev1.ResourceList{}
  72. for key, value := range a {
  73. if other, found := b[key]; found {
  74. if value.Cmp(other) <= 0 {
  75. result[key] = *other.Copy()
  76. continue
  77. }
  78. }
  79. result[key] = *value.Copy()
  80. }
  81. for key, value := range b {
  82. if _, found := result[key]; !found {
  83. result[key] = *value.Copy()
  84. }
  85. }
  86. return result
  87. }
  88. // Add returns the result of a + b for each named resource
  89. func Add(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
  90. result := corev1.ResourceList{}
  91. for key, value := range a {
  92. quantity := *value.Copy()
  93. if other, found := b[key]; found {
  94. quantity.Add(other)
  95. }
  96. result[key] = quantity
  97. }
  98. for key, value := range b {
  99. if _, found := result[key]; !found {
  100. quantity := *value.Copy()
  101. result[key] = quantity
  102. }
  103. }
  104. return result
  105. }
  106. // SubtractWithNonNegativeResult - subtracts and returns result of a - b but
  107. // makes sure we don't return negative values to prevent negative resource usage.
  108. func SubtractWithNonNegativeResult(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
  109. zero := resource.MustParse("0")
  110. result := corev1.ResourceList{}
  111. for key, value := range a {
  112. quantity := *value.Copy()
  113. if other, found := b[key]; found {
  114. quantity.Sub(other)
  115. }
  116. if quantity.Cmp(zero) > 0 {
  117. result[key] = quantity
  118. } else {
  119. result[key] = zero
  120. }
  121. }
  122. for key := range b {
  123. if _, found := result[key]; !found {
  124. result[key] = zero
  125. }
  126. }
  127. return result
  128. }
  129. // Subtract returns the result of a - b for each named resource
  130. func Subtract(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
  131. result := corev1.ResourceList{}
  132. for key, value := range a {
  133. quantity := *value.Copy()
  134. if other, found := b[key]; found {
  135. quantity.Sub(other)
  136. }
  137. result[key] = quantity
  138. }
  139. for key, value := range b {
  140. if _, found := result[key]; !found {
  141. quantity := *value.Copy()
  142. quantity.Neg()
  143. result[key] = quantity
  144. }
  145. }
  146. return result
  147. }
  148. // Mask returns a new resource list that only has the values with the specified names
  149. func Mask(resources corev1.ResourceList, names []corev1.ResourceName) corev1.ResourceList {
  150. nameSet := ToSet(names)
  151. result := corev1.ResourceList{}
  152. for key, value := range resources {
  153. if nameSet.Has(string(key)) {
  154. result[key] = *value.Copy()
  155. }
  156. }
  157. return result
  158. }
  159. // ResourceNames returns a list of all resource names in the ResourceList
  160. func ResourceNames(resources corev1.ResourceList) []corev1.ResourceName {
  161. result := []corev1.ResourceName{}
  162. for resourceName := range resources {
  163. result = append(result, resourceName)
  164. }
  165. return result
  166. }
  167. // Contains returns true if the specified item is in the list of items
  168. func Contains(items []corev1.ResourceName, item corev1.ResourceName) bool {
  169. for _, i := range items {
  170. if i == item {
  171. return true
  172. }
  173. }
  174. return false
  175. }
  176. // ContainsPrefix returns true if the specified item has a prefix that contained in given prefix Set
  177. func ContainsPrefix(prefixSet []string, item corev1.ResourceName) bool {
  178. for _, prefix := range prefixSet {
  179. if strings.HasPrefix(string(item), prefix) {
  180. return true
  181. }
  182. }
  183. return false
  184. }
  185. // Intersection returns the intersection of both list of resources, deduped and sorted
  186. func Intersection(a []corev1.ResourceName, b []corev1.ResourceName) []corev1.ResourceName {
  187. result := make([]corev1.ResourceName, 0, len(a))
  188. for _, item := range a {
  189. if Contains(result, item) {
  190. continue
  191. }
  192. if !Contains(b, item) {
  193. continue
  194. }
  195. result = append(result, item)
  196. }
  197. sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
  198. return result
  199. }
  200. // Difference returns the list of resources resulting from a-b, deduped and sorted
  201. func Difference(a []corev1.ResourceName, b []corev1.ResourceName) []corev1.ResourceName {
  202. result := make([]corev1.ResourceName, 0, len(a))
  203. for _, item := range a {
  204. if Contains(b, item) || Contains(result, item) {
  205. continue
  206. }
  207. result = append(result, item)
  208. }
  209. sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
  210. return result
  211. }
  212. // IsZero returns true if each key maps to the quantity value 0
  213. func IsZero(a corev1.ResourceList) bool {
  214. zero := resource.MustParse("0")
  215. for _, v := range a {
  216. if v.Cmp(zero) != 0 {
  217. return false
  218. }
  219. }
  220. return true
  221. }
  222. // IsNegative returns the set of resource names that have a negative value.
  223. func IsNegative(a corev1.ResourceList) []corev1.ResourceName {
  224. results := []corev1.ResourceName{}
  225. zero := resource.MustParse("0")
  226. for k, v := range a {
  227. if v.Cmp(zero) < 0 {
  228. results = append(results, k)
  229. }
  230. }
  231. return results
  232. }
  233. // ToSet takes a list of resource names and converts to a string set
  234. func ToSet(resourceNames []corev1.ResourceName) sets.String {
  235. result := sets.NewString()
  236. for _, resourceName := range resourceNames {
  237. result.Insert(string(resourceName))
  238. }
  239. return result
  240. }
  241. // CalculateUsage calculates and returns the requested ResourceList usage.
  242. // If an error is returned, usage only contains the resources which encountered no calculation errors.
  243. func CalculateUsage(namespaceName string, scopes []corev1.ResourceQuotaScope, hardLimits corev1.ResourceList, registry Registry, scopeSelector *corev1.ScopeSelector) (corev1.ResourceList, error) {
  244. // find the intersection between the hard resources on the quota
  245. // and the resources this controller can track to know what we can
  246. // look to measure updated usage stats for
  247. hardResources := ResourceNames(hardLimits)
  248. potentialResources := []corev1.ResourceName{}
  249. evaluators := registry.List()
  250. for _, evaluator := range evaluators {
  251. potentialResources = append(potentialResources, evaluator.MatchingResources(hardResources)...)
  252. }
  253. // NOTE: the intersection just removes duplicates since the evaluator match intersects with hard
  254. matchedResources := Intersection(hardResources, potentialResources)
  255. errors := []error{}
  256. // sum the observed usage from each evaluator
  257. newUsage := corev1.ResourceList{}
  258. for _, evaluator := range evaluators {
  259. // only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything
  260. intersection := evaluator.MatchingResources(matchedResources)
  261. if len(intersection) == 0 {
  262. continue
  263. }
  264. usageStatsOptions := UsageStatsOptions{Namespace: namespaceName, Scopes: scopes, Resources: intersection, ScopeSelector: scopeSelector}
  265. stats, err := evaluator.UsageStats(usageStatsOptions)
  266. if err != nil {
  267. // remember the error
  268. errors = append(errors, err)
  269. // exclude resources which encountered calculation errors
  270. matchedResources = Difference(matchedResources, intersection)
  271. continue
  272. }
  273. newUsage = Add(newUsage, stats.Used)
  274. }
  275. // mask the observed usage to only the set of resources tracked by this quota
  276. // merge our observed usage with the quota usage status
  277. // if the new usage is different than the last usage, we will need to do an update
  278. newUsage = Mask(newUsage, matchedResources)
  279. return newUsage, utilerrors.NewAggregate(errors)
  280. }