From c40679936a94a40b942b2e2220976f9a1b73579e Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Tue, 14 Nov 2023 15:50:15 +0100 Subject: [PATCH] fix: sync quota values from tenant to resourcequota object Signed-off-by: Dario Tranchitella --- api/v1beta2/tenant_annotations.go | 7 +++++-- controllers/tenant/resourcequotas.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/api/v1beta2/tenant_annotations.go b/api/v1beta2/tenant_annotations.go index a6438f32f..519e58361 100644 --- a/api/v1beta2/tenant_annotations.go +++ b/api/v1beta2/tenant_annotations.go @@ -12,6 +12,9 @@ import ( const ( // Annotation name part must be no more than 63 characters. maxAnnotationLength = 63 + + HardCapsuleQuotaAnnotation = "quota.capsule.clastix.io/hard-" + UsedCapsuleQuotaAnnotation = "quota.capsule.clastix.io/used-" ) func createAnnotation(format string, resource fmt.Stringer) (string, error) { @@ -36,9 +39,9 @@ func createAnnotation(format string, resource fmt.Stringer) (string, error) { } func UsedQuotaFor(resource fmt.Stringer) (string, error) { - return createAnnotation("quota.capsule.clastix.io/used-", resource) + return createAnnotation(UsedCapsuleQuotaAnnotation, resource) } func HardQuotaFor(resource fmt.Stringer) (string, error) { - return createAnnotation("quota.capsule.clastix.io/hard-", resource) + return createAnnotation(HardCapsuleQuotaAnnotation, resource) } diff --git a/controllers/tenant/resourcequotas.go b/controllers/tenant/resourcequotas.go index 15d6bbf51..a945d3bf8 100644 --- a/controllers/tenant/resourcequotas.go +++ b/controllers/tenant/resourcequotas.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "strconv" + "strings" "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" @@ -236,6 +237,16 @@ func (r *Manager) resourceQuotasUpdate(ctx context.Context, resourceName corev1. if found.Annotations == nil { found.Annotations = make(map[string]string) } + // Pruning the Capsule quota annotations: + // if the ResourceQuota is updated by removing some objects, + // we could still have left-overs which could be misleading. + // This will not lead to a reconciliation loop since the whole code is idempotent. + for k := range found.Annotations { + if strings.HasPrefix(k, capsulev1beta2.HardCapsuleQuotaAnnotation) || strings.HasPrefix(k, capsulev1beta2.UsedCapsuleQuotaAnnotation) { + delete(found.Annotations, k) + } + } + found.Labels = rq.Labels if actualKey, keyErr := capsulev1beta2.UsedQuotaFor(resourceName); keyErr == nil { found.Annotations[actualKey] = actual.String()