interfaces.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/admission"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. )
  20. // UsageStatsOptions is an options structs that describes how stats should be calculated
  21. type UsageStatsOptions struct {
  22. // Namespace where stats should be calculate
  23. Namespace string
  24. // Scopes that must match counted objects
  25. Scopes []api.ResourceQuotaScope
  26. }
  27. // UsageStats is result of measuring observed resource use in the system
  28. type UsageStats struct {
  29. // Used maps resource to quantity used
  30. Used api.ResourceList
  31. }
  32. // Evaluator knows how to evaluate quota usage for a particular group kind
  33. type Evaluator interface {
  34. // Constraints ensures that each required resource is present on item
  35. Constraints(required []api.ResourceName, item runtime.Object) error
  36. // Get returns the object with specified namespace and name
  37. Get(namespace, name string) (runtime.Object, error)
  38. // GroupKind returns the groupKind that this object knows how to evaluate
  39. GroupKind() unversioned.GroupKind
  40. // MatchesResources is the list of resources that this evaluator matches
  41. MatchesResources() []api.ResourceName
  42. // Matches returns true if the specified quota matches the input item
  43. Matches(resourceQuota *api.ResourceQuota, item runtime.Object) bool
  44. // OperationResources returns the set of resources that could be updated for the
  45. // specified operation for this kind. If empty, admission control will ignore
  46. // quota processing for the operation.
  47. OperationResources(operation admission.Operation) []api.ResourceName
  48. // Usage returns the resource usage for the specified object
  49. Usage(object runtime.Object) api.ResourceList
  50. // UsageStats calculates latest observed usage stats for all objects
  51. UsageStats(options UsageStatsOptions) (UsageStats, error)
  52. }
  53. // Registry holds the list of evaluators associated to a particular group kind
  54. type Registry interface {
  55. // Evaluators returns the set Evaluator objects registered to a groupKind
  56. Evaluators() map[unversioned.GroupKind]Evaluator
  57. }
  58. // UnionRegistry combines multiple registries. Order matters because first registry to claim a GroupKind
  59. // is the "winner"
  60. type UnionRegistry []Registry
  61. func (r UnionRegistry) Evaluators() map[unversioned.GroupKind]Evaluator {
  62. ret := map[unversioned.GroupKind]Evaluator{}
  63. for i := len(r) - 1; i >= 0; i-- {
  64. for k, v := range r[i].Evaluators() {
  65. ret[k] = v
  66. }
  67. }
  68. return ret
  69. }