Skip to content

Commit

Permalink
fix: using sentinel error for non limited custom resource
Browse files Browse the repository at this point in the history
  • Loading branch information
prometherion committed May 16, 2022
1 parent 8c0c8c6 commit a8b84c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion api/v1beta1/custom_resource_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pkg/webhook/tenant/custom_resource_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit a8b84c8

Please sign in to comment.