From 0a882431e87af0c804770c1e49d175b336c9b174 Mon Sep 17 00:00:00 2001 From: Aayush Chouhan Date: Thu, 26 Sep 2024 13:38:23 +0530 Subject: [PATCH] Added a validation check for quota config in bucket and some code fixes Signed-off-by: Aayush Chouhan --- pkg/bucket/bucket.go | 60 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/pkg/bucket/bucket.go b/pkg/bucket/bucket.go index e8f3bcb8d..b78d9752f 100644 --- a/pkg/bucket/bucket.go +++ b/pkg/bucket/bucket.go @@ -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" ) @@ -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: "a}) - if err != nil { - log.Fatal(err) - } + // calling updateBucketAPI to update quota for bucket + err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, Quota: "a}) + if err != nil { + log.Fatal(err) } } @@ -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 = "a } @@ -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 }