fix: using sentinel error for non limited custom resource

This commit is contained in:
Dario Tranchitella
2022-05-16 15:51:07 +00:00
parent 8c0c8c653d
commit a8b84c8cb3
2 changed files with 18 additions and 2 deletions
+13 -1
View File
@@ -32,10 +32,22 @@ func GetUsedResourceFromTenant(tenant Tenant, kindGroup string) (int64, error) {
return used, nil
}
type NonLimitedResourceError struct {
kindGroup string
}
func NewNonLimitedResourceError(kindGroup string) *NonLimitedResourceError {
return &NonLimitedResourceError{kindGroup: kindGroup}
}
func (n NonLimitedResourceError) Error() string {
return fmt.Sprintf("resource %s is not limited for the current tenant", n.kindGroup)
}
func GetLimitResourceFromTenant(tenant Tenant, kindGroup string) (int64, error) {
limitStr, ok := tenant.GetAnnotations()[LimitAnnotationForResource(kindGroup)]
if !ok {
return 0, fmt.Errorf("resource %s is not limited for the current tenant", kindGroup)
return 0, NewNonLimitedResourceError(kindGroup)
}
limit, err := strconv.ParseInt(limitStr, 10, 10)
+5 -1
View File
@@ -77,7 +77,11 @@ func (r *resourceCounterHandler) OnCreate(clt client.Client, decoder *admission.
}
if limit, retryErr = capsulev1beta1.GetLimitResourceFromTenant(*tnt, kgv); retryErr != nil {
return nil
if errors.As(err, &capsulev1beta1.NonLimitedResourceError{}) {
return nil
}
return err
}
used, _ := capsulev1beta1.GetUsedResourceFromTenant(*tnt, kgv)