Skip to content

Commit

Permalink
Merge pull request #1418 from achouhan09/quota-fix
Browse files Browse the repository at this point in the history
Added a validation check for quota config in bucket
  • Loading branch information
achouhan09 authored Sep 26, 2024
2 parents c95af10 + 0a88243 commit c6727ce
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions pkg/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/noobaa/noobaa-operator/v5/pkg/options"
"github.com/noobaa/noobaa-operator/v5/pkg/system"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -116,17 +117,19 @@ func RunCreate(cmd *cobra.Command, args []string) {
maxSize, _ := cmd.Flags().GetString("max-size")
maxObjects, _ := cmd.Flags().GetString("max-objects")

quota, err := prepareQuotaConfig(bucketName, maxSize, maxObjects)
if err != nil {
log.Fatalf(`❌ Could not create bucket "%q" quota validation failed %q`, bucketName, err)
}

err = nbClient.CreateBucketAPI(nb.CreateBucketParams{Name: bucketName, Tiering: tierName, ForceMd5Etag: forceMd5EtagPtr})
if err != nil {
log.Fatal(err)
}

quota, updateQuota := prepareQuotaConfig(maxSize, maxObjects)
if updateQuota {
err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, Quota: &quota})
if err != nil {
log.Fatal(err)
}
// calling updateBucketAPI to update quota for bucket
err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, Quota: &quota})
if err != nil {
log.Fatal(err)
}
}

Expand All @@ -150,8 +153,11 @@ func RunUpdate(cmd *cobra.Command, args []string) {
ForceMd5Etag: forceMd5EtagPtr,
}

quota, updateQuota := prepareQuotaConfig(maxSize, maxObjects)
if updateQuota {
if maxSize != "" || maxObjects != "" {
quota, err := prepareQuotaConfig(bucketName, maxSize, maxObjects)
if err != nil {
log.Fatalf(`❌ Could not update bucket "%q" quota validation failed %q`, bucketName, err)
}
updateParams.Quota = &quota
}

Expand Down Expand Up @@ -251,25 +257,29 @@ func RunList(cmd *cobra.Command, args []string) {
fmt.Printf("\n")
}

func prepareQuotaConfig(maxSize string, maxObjects string) (nb.QuotaConfig, bool) {
func prepareQuotaConfig(bucketName string, maxSize string, maxObjects string) (nb.QuotaConfig, error) {
var bucketMaxSize, bucketMaxObjects int64
updateQuota := false
quota := nb.QuotaConfig{}

if len(maxSize) > 0 {
bucketMaxSize, _ = strconv.ParseInt(maxSize, 10, 64)
updateQuota = true
}
if len(maxObjects) > 0 {
bucketMaxObjects, _ = strconv.ParseInt(maxObjects, 10, 64)
updateQuota = true
// validate quota config for bucket
if err := util.ValidateQuotaConfig(bucketName, maxSize, maxObjects); err != nil {
return quota, err
}
quota := nb.QuotaConfig{}
if bucketMaxSize > 0 {
f, u := nb.GetBytesAndUnits(bucketMaxSize, 2)
quota.Size = &nb.SizeQuotaConfig{Value: f, Unit: u}

if maxSize != "" {
quantity, _ := resource.ParseQuantity(maxSize)
bucketMaxSize = quantity.Value()
if bucketMaxSize > 0 {
f, u := nb.GetBytesAndUnits(bucketMaxSize, 2)
quota.Size = &nb.SizeQuotaConfig{Value: f, Unit: u}
}
}
if bucketMaxObjects > 0 {
quota.Quantity = &nb.QuantityQuotaConfig{Value: int(bucketMaxObjects)}
if maxObjects != "" {
bucketMaxObjects, _ = strconv.ParseInt(maxObjects, 10, 64)
if bucketMaxObjects > 0 {
quota.Quantity = &nb.QuantityQuotaConfig{Value: int(bucketMaxObjects)}
}
}
return quota, updateQuota

return quota, nil
}

0 comments on commit c6727ce

Please sign in to comment.