Skip to content

Commit

Permalink
move quota-related functions to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
hoptical committed Nov 7, 2023
1 parent dde3021 commit 2c1ba7f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 98 deletions.
109 changes: 109 additions & 0 deletions api/v1alpha1/quota_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package v1alpha1

import (
"context"
"fmt"

"github.com/snapp-incubator/s3-operator/pkg/consts"

Check failure on line 7 in api/v1alpha1/quota_handler.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/snapp-incubator/s3-operator (goimports)
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func CalculateNamespaceUsedQuota(ctx context.Context, uncachedReader client.Reader,
suc *S3UserClaim, cleanPhase bool) (*TotalQuota, error) {
totalUsedQuota := TotalQuota{}
// List all s3UserClaims in the namespace
s3UserClaimList := &S3UserClaimList{}
if err := uncachedReader.List(ctx, s3UserClaimList, client.InNamespace(suc.Namespace)); err != nil {
return &totalUsedQuota, fmt.Errorf("failed to list s3 user claims, %w", err)
}

// Sum all resource requests
for _, claim := range s3UserClaimList.Items {
if claim.Name != suc.Name {
totalUsedQuota.MaxObjects.Add(claim.Spec.Quota.MaxObjects)
totalUsedQuota.MaxSize.Add(claim.Spec.Quota.MaxSize)
totalUsedQuota.MaxBuckets += int64(claim.Spec.Quota.MaxBuckets)
}
}
// Don't add the current user quota if the function is called by the cleaner
if !cleanPhase {
totalUsedQuota.MaxObjects.Add(suc.Spec.Quota.MaxObjects)
totalUsedQuota.MaxSize.Add(suc.Spec.Quota.MaxSize)
totalUsedQuota.MaxBuckets += int64(suc.Spec.Quota.MaxBuckets)
}
return &totalUsedQuota, nil
}

func CalculateClusterUsedQuota(ctx context.Context, runtimeClient client.Client,
suc *S3UserClaim, cleanPhase bool) (*TotalQuota, string, error) {
totalClusterUsedQuota := TotalQuota{}
// Find team's clusterResourceQuota
team, err := findTeam(ctx, runtimeClient, suc)
if err != nil {
return &totalClusterUsedQuota, "", fmt.Errorf("failed to find team, %w", err)
}

// Sum all resource requests in team's namespaces
namespaces, err := findTeamNamespaces(ctx, runtimeClient, team)
if err != nil {
return &totalClusterUsedQuota, team, fmt.Errorf("failed to find team namespaces, %w", err)
}
for _, ns := range namespaces {
s3UserClaimList := &S3UserClaimList{}
if err := uncachedReader.List(ctx, s3UserClaimList, client.InNamespace(ns)); err != nil {
return &totalClusterUsedQuota, team, fmt.Errorf("failed to list s3UserClaims, %w", err)
}

for _, claim := range s3UserClaimList.Items {
if claim.Name != suc.Name || claim.Namespace != suc.Namespace {
totalClusterUsedQuota.MaxObjects.Add(claim.Spec.Quota.MaxObjects)
totalClusterUsedQuota.MaxSize.Add(claim.Spec.Quota.MaxSize)
totalClusterUsedQuota.MaxBuckets += int64(claim.Spec.Quota.MaxBuckets)
}
}
}
// Don't add the current user quota if the function is called by the cleaner
if !cleanPhase {
totalClusterUsedQuota.MaxObjects.Add(suc.Spec.Quota.MaxObjects)
totalClusterUsedQuota.MaxSize.Add(suc.Spec.Quota.MaxSize)
totalClusterUsedQuota.MaxBuckets += int64(suc.Spec.Quota.MaxBuckets)
}
return &totalClusterUsedQuota, team, nil
}

func findTeam(ctx context.Context, runtimeClient client.Client, suc *S3UserClaim) (string, error) {
ns := &v1.Namespace{}
if err := runtimeClient.Get(ctx, types.NamespacedName{Name: suc.ObjectMeta.Namespace}, ns); err != nil {
return "", fmt.Errorf("failed to get namespace, %w", err)
}

team, ok := ns.ObjectMeta.Labels[consts.LabelTeam]
if !ok {
return "", fmt.Errorf("namespace %s doesn't have team label", ns.ObjectMeta.Name)
}

return team, nil
}

func findTeamNamespaces(ctx context.Context, runtimeClient client.Client, team string) ([]string, error) {
var namespaces []string

namespaceList := &v1.NamespaceList{}
if err := runtimeClient.List(ctx, namespaceList); err != nil {
return namespaces, fmt.Errorf("failed to list namespaces, %w", err)
}

for _, ns := range namespaceList.Items {
labels := ns.ObjectMeta.Labels
if labels == nil {
labels = map[string]string{}
}
if nsTeam := labels[consts.LabelTeam]; nsTeam == team {
namespaces = append(namespaces, ns.ObjectMeta.Name)
}
}

return namespaces, nil
}
98 changes: 0 additions & 98 deletions api/v1alpha1/s3userclaim_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,69 +154,6 @@ func validateQuota(suc *S3UserClaim, allErrs field.ErrorList) field.ErrorList {
return allErrs
}

func CalculateNamespaceUsedQuota(ctx context.Context, uncachedReader client.Reader,
suc *S3UserClaim, cleanPhase bool) (*TotalQuota, error) {
totalUsedQuota := TotalQuota{}
// List all s3UserClaims in the namespace
s3UserClaimList := &S3UserClaimList{}
if err := uncachedReader.List(ctx, s3UserClaimList, client.InNamespace(suc.Namespace)); err != nil {
return &totalUsedQuota, fmt.Errorf("failed to list s3 user claims, %w", err)
}

// Sum all resource requests
for _, claim := range s3UserClaimList.Items {
if claim.Name != suc.Name {
totalUsedQuota.MaxObjects.Add(claim.Spec.Quota.MaxObjects)
totalUsedQuota.MaxSize.Add(claim.Spec.Quota.MaxSize)
totalUsedQuota.MaxBuckets += int64(claim.Spec.Quota.MaxBuckets)
}
}
// Don't add the current user quota if the function is called by the cleaner
if !cleanPhase {
totalUsedQuota.MaxObjects.Add(suc.Spec.Quota.MaxObjects)
totalUsedQuota.MaxSize.Add(suc.Spec.Quota.MaxSize)
totalUsedQuota.MaxBuckets += int64(suc.Spec.Quota.MaxBuckets)
}
return &totalUsedQuota, nil
}

func CalculateClusterUsedQuota(ctx context.Context, runtimeClient client.Client,
suc *S3UserClaim, cleanPhase bool) (*TotalQuota, string, error) {
totalClusterUsedQuota := TotalQuota{}
// Find team's clusterResourceQuota
team, err := findTeam(ctx, runtimeClient, suc)
if err != nil {
return &totalClusterUsedQuota, "", fmt.Errorf("failed to find team, %w", err)
}

// Sum all resource requests in team's namespaces
namespaces, err := findTeamNamespaces(ctx, runtimeClient, team)
if err != nil {
return &totalClusterUsedQuota, team, fmt.Errorf("failed to find team namespaces, %w", err)
}
for _, ns := range namespaces {
s3UserClaimList := &S3UserClaimList{}
if err := uncachedReader.List(ctx, s3UserClaimList, client.InNamespace(ns)); err != nil {
return &totalClusterUsedQuota, team, fmt.Errorf("failed to list s3UserClaims, %w", err)
}

for _, claim := range s3UserClaimList.Items {
if claim.Name != suc.Name || claim.Namespace != suc.Namespace {
totalClusterUsedQuota.MaxObjects.Add(claim.Spec.Quota.MaxObjects)
totalClusterUsedQuota.MaxSize.Add(claim.Spec.Quota.MaxSize)
totalClusterUsedQuota.MaxBuckets += int64(claim.Spec.Quota.MaxBuckets)
}
}
}
// Don't add the current user quota if the function is called by the cleaner
if !cleanPhase {
totalClusterUsedQuota.MaxObjects.Add(suc.Spec.Quota.MaxObjects)
totalClusterUsedQuota.MaxSize.Add(suc.Spec.Quota.MaxSize)
totalClusterUsedQuota.MaxBuckets += int64(suc.Spec.Quota.MaxBuckets)
}
return &totalClusterUsedQuota, team, nil
}

func validateAgainstNamespaceQuota(ctx context.Context, suc *S3UserClaim) error {
totalUsedQuota, err := CalculateNamespaceUsedQuota(ctx, uncachedReader, suc, false)
if err != nil {
Expand Down Expand Up @@ -287,38 +224,3 @@ func validateAgainstClusterQuota(ctx context.Context, suc *S3UserClaim) error {

return nil
}

func findTeam(ctx context.Context, runtimeClient client.Client, suc *S3UserClaim) (string, error) {
ns := &v1.Namespace{}
if err := runtimeClient.Get(ctx, types.NamespacedName{Name: suc.ObjectMeta.Namespace}, ns); err != nil {
return "", fmt.Errorf("failed to get namespace, %w", err)
}

team, ok := ns.ObjectMeta.Labels[consts.LabelTeam]
if !ok {
return "", fmt.Errorf("namespace %s doesn't have team label", ns.ObjectMeta.Name)
}

return team, nil
}

func findTeamNamespaces(ctx context.Context, runtimeClient client.Client, team string) ([]string, error) {
var namespaces []string

namespaceList := &v1.NamespaceList{}
if err := runtimeClient.List(ctx, namespaceList); err != nil {
return namespaces, fmt.Errorf("failed to list namespaces, %w", err)
}

for _, ns := range namespaceList.Items {
labels := ns.ObjectMeta.Labels
if labels == nil {
labels = map[string]string{}
}
if nsTeam := labels[consts.LabelTeam]; nsTeam == team {
namespaces = append(namespaces, ns.ObjectMeta.Name)
}
}

return namespaces, nil
}

0 comments on commit 2c1ba7f

Please sign in to comment.