| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package v1 |
| 18 |
|
| 19 |
import ( |
| 20 |
corev1 "k8s.io/api/core/v1" |
| 21 |
"k8s.io/apimachinery/pkg/runtime" |
| 22 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 23 |
"k8s.io/apiserver/pkg/admission" |
| 24 |
"k8s.io/client-go/tools/cache" |
| 25 |
) |
| 26 |
|
| 27 |
// UsageStatsOptions is an options structs that describes how stats should be calculated |
| 28 |
type UsageStatsOptions struct { |
| 29 |
// Namespace where stats should be calculate |
| 30 |
Namespace string |
| 31 |
// Scopes that must match counted objects |
| 32 |
Scopes []corev1.ResourceQuotaScope |
| 33 |
// Resources are the set of resources to include in the measurement |
| 34 |
Resources []corev1.ResourceName |
| 35 |
ScopeSelector *corev1.ScopeSelector |
| 36 |
} |
| 37 |
|
| 38 |
// UsageStats is result of measuring observed resource use in the system |
| 39 |
type UsageStats struct { |
| 40 |
// Used maps resource to quantity used |
| 41 |
Used corev1.ResourceList |
| 42 |
} |
| 43 |
|
| 44 |
// Evaluator knows how to evaluate quota usage for a particular group resource |
| 45 |
type Evaluator interface { |
| 46 |
// Constraints ensures that each required resource is present on item |
| 47 |
Constraints(required []corev1.ResourceName, item runtime.Object) error |
| 48 |
// GroupResource returns the groupResource that this object knows how to evaluate |
| 49 |
GroupResource() schema.GroupResource |
| 50 |
// Handles determines if quota could be impacted by the specified attribute. |
| 51 |
// If true, admission control must perform quota processing for the operation, otherwise it is safe to ignore quota. |
| 52 |
Handles(operation admission.Attributes) bool |
| 53 |
// Matches returns true if the specified quota matches the input item |
| 54 |
Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error) |
| 55 |
// MatchingScopes takes the input specified list of scopes and input object and returns the set of scopes that matches input object. |
| 56 |
MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) |
| 57 |
// 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 don't have a corresponding covering quota scope |
| 58 |
UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) |
| 59 |
// MatchingResources takes the input specified list of resources and returns the set of resources evaluator matches. |
| 60 |
MatchingResources(input []corev1.ResourceName) []corev1.ResourceName |
| 61 |
// Usage returns the resource usage for the specified object |
| 62 |
Usage(item runtime.Object) (corev1.ResourceList, error) |
| 63 |
// UsageStats calculates latest observed usage stats for all objects |
| 64 |
UsageStats(options UsageStatsOptions) (UsageStats, error) |
| 65 |
} |
| 66 |
|
| 67 |
// Configuration defines how the quota system is configured. |
| 68 |
type Configuration interface { |
| 69 |
// IgnoredResources are ignored by quota. |
| 70 |
IgnoredResources() map[schema.GroupResource]struct{} |
| 71 |
// Evaluators for quota evaluation. |
| 72 |
Evaluators() []Evaluator |
| 73 |
} |
| 74 |
|
| 75 |
// Registry maintains a list of evaluators |
| 76 |
type Registry interface { |
| 77 |
// Add to registry |
| 78 |
Add(e Evaluator) |
| 79 |
// Remove from registry |
| 80 |
Remove(e Evaluator) |
| 81 |
// Get by group resource |
| 82 |
Get(gr schema.GroupResource) Evaluator |
| 83 |
// List from registry |
| 84 |
List() []Evaluator |
| 85 |
} |
| 86 |
|
| 87 |
// ListerForResourceFunc knows how to get a lister for a specific resource |
| 88 |
type ListerForResourceFunc func(schema.GroupVersionResource) (cache.GenericLister, error) |