interfaces.go 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. corev1 "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/apiserver/pkg/admission"
  19. "k8s.io/client-go/tools/cache"
  20. )
  21. // UsageStatsOptions is an options structs that describes how stats should be calculated
  22. type UsageStatsOptions struct {
  23. // Namespace where stats should be calculate
  24. Namespace string
  25. // Scopes that must match counted objects
  26. Scopes []corev1.ResourceQuotaScope
  27. // Resources are the set of resources to include in the measurement
  28. Resources []corev1.ResourceName
  29. ScopeSelector *corev1.ScopeSelector
  30. }
  31. // UsageStats is result of measuring observed resource use in the system
  32. type UsageStats struct {
  33. // Used maps resource to quantity used
  34. Used corev1.ResourceList
  35. }
  36. // Evaluator knows how to evaluate quota usage for a particular group resource
  37. type Evaluator interface {
  38. // Constraints ensures that each required resource is present on item
  39. Constraints(required []corev1.ResourceName, item runtime.Object) error
  40. // GroupResource returns the groupResource that this object knows how to evaluate
  41. GroupResource() schema.GroupResource
  42. // Handles determines if quota could be impacted by the specified attribute.
  43. // If true, admission control must perform quota processing for the operation, otherwise it is safe to ignore quota.
  44. Handles(operation admission.Attributes) bool
  45. // Matches returns true if the specified quota matches the input item
  46. Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error)
  47. // MatchingScopes takes the input specified list of scopes and input object and returns the set of scopes that matches input object.
  48. MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error)
  49. // UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes. It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
  50. UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error)
  51. // MatchingResources takes the input specified list of resources and returns the set of resources evaluator matches.
  52. MatchingResources(input []corev1.ResourceName) []corev1.ResourceName
  53. // Usage returns the resource usage for the specified object
  54. Usage(item runtime.Object) (corev1.ResourceList, error)
  55. // UsageStats calculates latest observed usage stats for all objects
  56. UsageStats(options UsageStatsOptions) (UsageStats, error)
  57. }
  58. // Configuration defines how the quota system is configured.
  59. type Configuration interface {
  60. // IgnoredResources are ignored by quota.
  61. IgnoredResources() map[schema.GroupResource]struct{}
  62. // Evaluators for quota evaluation.
  63. Evaluators() []Evaluator
  64. }
  65. // Registry maintains a list of evaluators
  66. type Registry interface {
  67. // Add to registry
  68. Add(e Evaluator)
  69. // Remove from registry
  70. Remove(e Evaluator)
  71. // Get by group resource
  72. Get(gr schema.GroupResource) Evaluator
  73. // List from registry
  74. List() []Evaluator
  75. }
  76. // ListerForResourceFunc knows how to get a lister for a specific resource
  77. type ListerForResourceFunc func(schema.GroupVersionResource) (cache.GenericLister, error)