From a8b84c8cb37c2da7fc524408af3d602c7555dde4 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Mon, 16 May 2022 17:08:14 +0200 Subject: [PATCH] fix: using sentinel error for non limited custom resource --- api/v1beta1/custom_resource_quota.go | 14 +++++++++++++- pkg/webhook/tenant/custom_resource_quota.go | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/v1beta1/custom_resource_quota.go b/api/v1beta1/custom_resource_quota.go index ea5fd4e5..91b1334e 100644 --- a/api/v1beta1/custom_resource_quota.go +++ b/api/v1beta1/custom_resource_quota.go @@ -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) diff --git a/pkg/webhook/tenant/custom_resource_quota.go b/pkg/webhook/tenant/custom_resource_quota.go index 0eba2dbe..e997e1de 100644 --- a/pkg/webhook/tenant/custom_resource_quota.go +++ b/pkg/webhook/tenant/custom_resource_quota.go @@ -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)