mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
Merge pull request #66866 from kgolab/kg-quota-refactor
Automatic merge from submit-queue (batch tested with PRs 67160, 67090, 67159, 66866, 62111). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Refactor checkRequest to allow it to be called from outside **What this PR does / why we need it**: Refactor checkRequest method to allow it to be called from outside of admission controller (most of its body does not depend on any quotaEvaluator properties). ```release-note NONE ```
This commit is contained in:
commit
524a81496b
@ -392,12 +392,16 @@ func getMatchedLimitedScopes(evaluator quota.Evaluator, inputObject runtime.Obje
|
|||||||
// checkRequest verifies that the request does not exceed any quota constraint. it returns a copy of quotas not yet persisted
|
// checkRequest verifies that the request does not exceed any quota constraint. it returns a copy of quotas not yet persisted
|
||||||
// that capture what the usage would be if the request succeeded. It return an error if there is insufficient quota to satisfy the request
|
// that capture what the usage would be if the request succeeded. It return an error if there is insufficient quota to satisfy the request
|
||||||
func (e *quotaEvaluator) checkRequest(quotas []api.ResourceQuota, a admission.Attributes) ([]api.ResourceQuota, error) {
|
func (e *quotaEvaluator) checkRequest(quotas []api.ResourceQuota, a admission.Attributes) ([]api.ResourceQuota, error) {
|
||||||
namespace := a.GetNamespace()
|
|
||||||
evaluator := e.registry.Get(a.GetResource().GroupResource())
|
evaluator := e.registry.Get(a.GetResource().GroupResource())
|
||||||
if evaluator == nil {
|
if evaluator == nil {
|
||||||
return quotas, nil
|
return quotas, nil
|
||||||
}
|
}
|
||||||
|
return CheckRequest(quotas, a, evaluator, e.config.LimitedResources)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckRequest is a static version of quotaEvaluator.checkRequest, possible to be called from outside.
|
||||||
|
func CheckRequest(quotas []api.ResourceQuota, a admission.Attributes, evaluator quota.Evaluator,
|
||||||
|
limited []resourcequotaapi.LimitedResource) ([]api.ResourceQuota, error) {
|
||||||
if !evaluator.Handles(a) {
|
if !evaluator.Handles(a) {
|
||||||
return quotas, nil
|
return quotas, nil
|
||||||
}
|
}
|
||||||
@ -406,14 +410,14 @@ func (e *quotaEvaluator) checkRequest(quotas []api.ResourceQuota, a admission.At
|
|||||||
inputObject := a.GetObject()
|
inputObject := a.GetObject()
|
||||||
|
|
||||||
// Check if object matches AdmissionConfiguration matchScopes
|
// Check if object matches AdmissionConfiguration matchScopes
|
||||||
limitedScopes, err := getMatchedLimitedScopes(evaluator, inputObject, e.config.LimitedResources)
|
limitedScopes, err := getMatchedLimitedScopes(evaluator, inputObject, limited)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return quotas, nil
|
return quotas, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine the set of resource names that must exist in a covering quota
|
// determine the set of resource names that must exist in a covering quota
|
||||||
limitedResourceNames := []api.ResourceName{}
|
limitedResourceNames := []api.ResourceName{}
|
||||||
limitedResources := filterLimitedResourcesByGroupResource(e.config.LimitedResources, a.GetResource().GroupResource())
|
limitedResources := filterLimitedResourcesByGroupResource(limited, a.GetResource().GroupResource())
|
||||||
if len(limitedResources) > 0 {
|
if len(limitedResources) > 0 {
|
||||||
deltaUsage, err := evaluator.Usage(inputObject)
|
deltaUsage, err := evaluator.Usage(inputObject)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -493,6 +497,7 @@ func (e *quotaEvaluator) checkRequest(quotas []api.ResourceQuota, a admission.At
|
|||||||
// the resource represents a number of unique references to external
|
// the resource represents a number of unique references to external
|
||||||
// resource. In such a case an evaluator needs to process other objects in
|
// resource. In such a case an evaluator needs to process other objects in
|
||||||
// the same namespace which needs to be known.
|
// the same namespace which needs to be known.
|
||||||
|
namespace := a.GetNamespace()
|
||||||
if accessor, err := meta.Accessor(inputObject); namespace != "" && err == nil {
|
if accessor, err := meta.Accessor(inputObject); namespace != "" && err == nil {
|
||||||
if accessor.GetNamespace() == "" {
|
if accessor.GetNamespace() == "" {
|
||||||
accessor.SetNamespace(namespace)
|
accessor.SetNamespace(namespace)
|
||||||
|
Loading…
Reference in New Issue
Block a user