Skip to content

Commit

Permalink
[Fix]: Update cluster resource quota to support namespaces (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoptical authored Nov 10, 2023
1 parent c52412e commit 161c945
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 26 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/s3userclaim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Reconciler struct {
readonlyCephUserId string
readonlyCephUserFullId string
desiredSubusersStringList []string

namespaceUsedQuota *s3v1alpha1.UserQuota
// configurations
clusterName string
rgwAccessKey string
Expand Down
50 changes: 42 additions & 8 deletions internal/controllers/s3userclaim/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ func (r *Reconciler) updateNamespaceQuotaStatusExclusive(ctx context.Context) (*
}

func (r *Reconciler) updateNamespaceQuotaStatus(ctx context.Context, addCurrentQuota bool) (*ctrl.Result, error) {
var err error
// sum up all quotas in the namespace
totalUsedQuota, err := s3v1alpha1.CalculateNamespaceUsedQuota(ctx, r.uncachedReader, r.s3UserClaim, addCurrentQuota)
r.namespaceUsedQuota, err = s3v1alpha1.CalculateNamespaceUsedQuota(ctx, r.uncachedReader, r.s3UserClaim, addCurrentQuota)
if err != nil {
r.logger.Error(err, "failed to calculate namespace used quota")
return subreconciler.Requeue()
Expand All @@ -280,9 +281,8 @@ func (r *Reconciler) updateNamespaceQuotaStatus(ctx context.Context, addCurrentQ
if status.Used == nil {
status.Used = corev1.ResourceList{}
}
status.Used[consts.ResourceNameS3MaxObjects] = totalUsedQuota.MaxObjects
status.Used[consts.ResourceNameS3MaxSize] = totalUsedQuota.MaxSize
status.Used[consts.ResourceNameS3MaxBuckets] = *resource.NewQuantity(totalUsedQuota.MaxBuckets, resource.DecimalSI)
assignUsedQuotaToResourceStatus(status, r.namespaceUsedQuota)

if !apiequality.Semantic.DeepEqual(quota.Status, *status) {
quota.Status = *status
if err := r.Status().Update(ctx, &quota); err != nil {
Expand Down Expand Up @@ -323,14 +323,16 @@ func (r *Reconciler) updateClusterQuotaStatus(ctx context.Context, addCurrentQuo
r.logger.Error(err, "failed to get clusterQuota")
return subreconciler.Requeue()
}

status := clusterQuota.Status.DeepCopy()
if status.Total.Used == nil {
status.Total.Used = corev1.ResourceList{}
}
status.Total.Used[consts.ResourceNameS3MaxObjects] = totalClusterUsedQuota.MaxObjects
status.Total.Used[consts.ResourceNameS3MaxSize] = totalClusterUsedQuota.MaxSize
status.Total.Used[consts.ResourceNameS3MaxBuckets] = *resource.NewQuantity(totalClusterUsedQuota.MaxBuckets, resource.DecimalSI)
// update total field of the status
assignUsedQuotaToResourceStatus(&status.Total, totalClusterUsedQuota)

// update namespace field of the status
status.Namespaces = r.assignNamespaceQuotaToResourceStatus(status.Namespaces)

if !apiequality.Semantic.DeepEqual(clusterQuota.Status, *status) {
clusterQuota.Status = *status
if err := r.Status().Update(ctx, clusterQuota); err != nil {
Expand All @@ -345,6 +347,38 @@ func (r *Reconciler) updateClusterQuotaStatus(ctx context.Context, addCurrentQuo

return subreconciler.ContinueReconciling()
}

func (r *Reconciler) assignNamespaceQuotaToResourceStatus(statusNamespaces openshiftquota.ResourceQuotasStatusByNamespace) openshiftquota.ResourceQuotasStatusByNamespace {
if r.namespaceUsedQuota == nil {
r.logger.Info("Warning: unable to find the namespace used quota while updating the cluster resource quota",
"namespace", r.s3UserClaimNamespace)
r.namespaceUsedQuota = &s3v1alpha1.UserQuota{}
}
// update the namespace status in cluster resource quota if it's there
for i, namespaceQuota := range statusNamespaces {
if namespaceQuota.Namespace == r.s3UserClaimNamespace {
assignUsedQuotaToResourceStatus(&statusNamespaces[i].Status, r.namespaceUsedQuota)
return statusNamespaces
}
}
// create a new item for the current namespace if it's not already there
namepaceQuotaStatus := corev1.ResourceQuotaStatus{}
assignUsedQuotaToResourceStatus(&namepaceQuotaStatus, r.namespaceUsedQuota)
namespaceQuota := openshiftquota.ResourceQuotaStatusByNamespace{Namespace: r.s3UserClaimNamespace,
Status: namepaceQuotaStatus}
statusNamespaces = append(statusNamespaces, namespaceQuota)
return statusNamespaces
}

func assignUsedQuotaToResourceStatus(resourceQuotaStatus *corev1.ResourceQuotaStatus, usedQuota *s3v1alpha1.UserQuota) {
if resourceQuotaStatus.Used == nil {
resourceQuotaStatus.Used = corev1.ResourceList{}
}
resourceQuotaStatus.Used[consts.ResourceNameS3MaxObjects] = usedQuota.MaxObjects
resourceQuotaStatus.Used[consts.ResourceNameS3MaxSize] = usedQuota.MaxSize
resourceQuotaStatus.Used[consts.ResourceNameS3MaxBuckets] = *resource.NewQuantity(usedQuota.MaxBuckets, resource.DecimalSI)
}

func (r *Reconciler) addCleanupFinalizer(ctx context.Context) (*ctrl.Result, error) {
if objUpdated := controllerutil.AddFinalizer(r.s3UserClaim, consts.S3UserClaimCleanupFinalizer); objUpdated {
if err := r.Update(ctx, r.s3UserClaim); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion testing/e2e/01-install-ceph-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ commands:
- command: kubectl apply -f ../../config/external-crd
- command: kubectl create namespace s3-test
ignoreFailure: true
- command: kubectl create namespace s3-test2
ignoreFailure: true
# Create namespace s3-operator-system
- command: kubectl create namespace s3-operator-system
ignoreFailure: true
Expand All @@ -13,4 +15,5 @@ commands:
# Setup the operator
- command: make -C ../../ deploy-for-e2e-test IMG=s3-operator:latest
# The namespace should have the team label for the cluster resource quota
- command: kubectl label namespace s3-test snappcloud.io/team=myteam
- command: kubectl label namespace s3-test snappcloud.io/team=myteam
- command: kubectl label namespace s3-test2 snappcloud.io/team=myteam
30 changes: 22 additions & 8 deletions testing/e2e/02-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ apiVersion: s3.snappcloud.io/v1alpha1
kind: S3UserClaim
metadata:
name: s3userclaim-extra
namespace: s3-test
namespace: s3-test2
status:
quota:
maxSize: 5k
Expand All @@ -74,7 +74,7 @@ status:
apiVersion: s3.snappcloud.io/v1alpha1
kind: S3User
metadata:
name: s3-test.s3userclaim-extra
name: s3-test2.s3userclaim-extra
spec:
quota:
maxSize: 5k
Expand All @@ -84,14 +84,14 @@ spec:
apiVersion: s3.snappcloud.io/v1alpha1
kind: S3UserClaim
name: s3userclaim-extra
namespace: s3-test
namespace: s3-test2

---
apiVersion: v1
kind: Secret
metadata:
name: s3-extra-admin-secret
namespace: s3-test
namespace: s3-test2
ownerReferences:
- apiVersion: s3.snappcloud.io/v1alpha1
blockOwnerDeletion: true
Expand All @@ -105,7 +105,7 @@ apiVersion: v1
kind: Secret
metadata:
name: s3-extra-readonly-secret
namespace: s3-test
namespace: s3-test2
ownerReferences:
- apiVersion: s3.snappcloud.io/v1alpha1
blockOwnerDeletion: true
Expand All @@ -126,9 +126,9 @@ status:
s3/objects: 1k
s3/size: 20k
used:
s3/buckets: "10"
s3/objects: 1k
s3/size: 15k
s3/buckets: "5"
s3/objects: "500"
s3/size: 10k

---
apiVersion: quota.openshift.io/v1
Expand All @@ -141,3 +141,17 @@ status:
s3/buckets: "10"
s3/objects: 1k
s3/size: 15k
namespaces:
- namespace: s3-test
status:
used:
s3/buckets: "5"
s3/objects: "500"
s3/size: 10k
- namespace: s3-test2
status:
used:
s3/buckets: "5"
s3/objects: "500"
s3/size: 5k

2 changes: 1 addition & 1 deletion testing/e2e/02-create-userclaim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ apiVersion: s3.snappcloud.io/v1alpha1
kind: S3UserClaim
metadata:
name: s3userclaim-extra
namespace: s3-test
namespace: s3-test2
spec:
s3UserClass: ceph-default
readonlySecret: s3-extra-readonly-secret
Expand Down
2 changes: 1 addition & 1 deletion testing/e2e/03-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ apiVersion: s3.snappcloud.io/v1alpha1
kind: S3Bucket
metadata:
name: s3bucket-extra-delete
namespace: s3-test
namespace: s3-test2
status:
created: true
2 changes: 1 addition & 1 deletion testing/e2e/03-create-bucket.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apiVersion: s3.snappcloud.io/v1alpha1
kind: S3Bucket
metadata:
name: s3bucket-extra-delete
namespace: s3-test
namespace: s3-test2
spec:
s3UserRef: s3userclaim-extra
s3DeletionPolicy: delete
Expand Down
13 changes: 13 additions & 0 deletions testing/e2e/10-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ kind: ClusterResourceQuota
metadata:
name: myteam
status:
namespaces:
- namespace: s3-test
status:
used:
s3/buckets: "0"
s3/objects: "0"
s3/size: "0"
- namespace: s3-test2
status:
used:
s3/buckets: "0"
s3/objects: "0"
s3/size: "0"
total:
used:
s3/buckets: "0"
Expand Down
4 changes: 2 additions & 2 deletions testing/e2e/10-delete-extra-user-bucket.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: kubectl delete s3bucket s3bucket-extra-delete -n s3-test
- command: kubectl delete s3userclaim s3userclaim-extra -n s3-test
- command: kubectl delete s3bucket s3bucket-extra-delete -n s3-test2
- command: kubectl delete s3userclaim s3userclaim-extra -n s3-test2
6 changes: 3 additions & 3 deletions testing/e2e/10-errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ apiVersion: s3.snappcloud.io/v1alpha1
kind: S3Bucket
metadata:
name: s3bucket-extra-delete
namespace: s3-test
namespace: s3-test2

---

apiVersion: s3.snappcloud.io/v1alpha1
kind: S3UserClaim
metadata:
name: s3userclaim-extra
namespace: s3-test
namespace: s3-test2

---
apiVersion: s3.snappcloud.io/v1alpha1
kind: S3User
metadata:
name: s3-test.s3userclaim-extra
name: s3-test2.s3userclaim-extra

0 comments on commit 161c945

Please sign in to comment.