Skip to content

Commit 37c1a6e

Browse files
committed
fixed BucketClass quota ignore during OBC update
Signed-off-by: VershaAgrawal <vershaagrawal98@gmail.com>
1 parent 3ffa97c commit 37c1a6e

3 files changed

Lines changed: 157 additions & 17 deletions

File tree

pkg/obc/obc_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package obc_test
1+
package obc
22

33
import (
44
"testing"

pkg/obc/obc_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
55
. "github.com/onsi/ginkgo/v2"
66
. "github.com/onsi/gomega"
7+
"github.com/sirupsen/logrus"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"sigs.k8s.io/controller-runtime/pkg/client"
910
)
@@ -160,6 +161,103 @@ var _ = Describe("OBC referenced BucketClass", func() {
160161
})
161162
})
162163

164+
var _ = Describe("getBucketClassByName", func() {
165+
Context("When a bucketclass exists in the same namespace that of OBC", func() {
166+
It("should return the BucketClass with namespace to be the same as that of OBC", func() {
167+
obcNS := "obc-ns"
168+
systemNS := "test"
169+
170+
bc, ok := getBucketClassByName(
171+
"bc1",
172+
obcNS,
173+
systemNS,
174+
func(o client.Object) bool {
175+
return o.GetNamespace() == obcNS
176+
},
177+
)
178+
179+
Expect(ok).To(BeTrue())
180+
Expect(bc.GetNamespace()).To(Equal(obcNS))
181+
})
182+
})
183+
184+
Context("When a bucketclass exists in the system namespace", func() {
185+
It("should return the BucketClass's namespace to be the same as that of system", func() {
186+
obcNS := "obc-ns"
187+
systemNS := "test"
188+
189+
bc, ok := getBucketClassByName(
190+
"bc1",
191+
obcNS,
192+
systemNS,
193+
func(o client.Object) bool {
194+
return o.GetNamespace() == systemNS
195+
},
196+
)
197+
198+
Expect(ok).To(BeTrue())
199+
Expect(bc.GetNamespace()).To(Equal(systemNS))
200+
})
201+
})
202+
203+
Context("When a bucketclass exists in the system namespace as well as OBC namespace", func() {
204+
It("should return the BucketClass's namespace to be the same as that of OBC", func() {
205+
obcNS := "obc-ns"
206+
systemNS := "test"
207+
208+
bc, ok := getBucketClassByName(
209+
"bc1",
210+
obcNS,
211+
systemNS,
212+
func(o client.Object) bool {
213+
return o.GetNamespace() == systemNS || o.GetNamespace() == obcNS
214+
},
215+
)
216+
217+
Expect(ok).To(BeTrue())
218+
Expect(bc.GetNamespace()).To(Equal(obcNS))
219+
})
220+
})
221+
222+
Context("When a bucketclass does not exists in system namespace or OBC namespace", func() {
223+
It("should return the BucketClass's namespace to be the same as that of system namespace with \"ok\" set to false", func() {
224+
obcNS := "obc-ns"
225+
systemNS := "test"
226+
227+
bc, ok := getBucketClassByName(
228+
"bc1",
229+
obcNS,
230+
systemNS,
231+
func(o client.Object) bool {
232+
return false
233+
},
234+
)
235+
236+
Expect(ok).To(BeFalse())
237+
Expect(bc.GetNamespace()).To(Equal(systemNS))
238+
})
239+
})
240+
241+
Context("When a bucketclass name is empty", func() {
242+
It("should return empty string for namespace and \"exists\" set to false", func() {
243+
obcNS := "obc-ns"
244+
systemNS := "test"
245+
246+
bc, ok := getBucketClassByName(
247+
"",
248+
obcNS,
249+
systemNS,
250+
func(o client.Object) bool {
251+
return false
252+
},
253+
)
254+
255+
Expect(ok).To(BeFalse())
256+
Expect(bc.GetNamespace()).To(Equal(""))
257+
})
258+
})
259+
})
260+
163261
var _ = Describe("getExternalDNSDetails", func() {
164262
noobaaWithExternalDNS := func(s3URLs, vectorsURLs []string) *nbv1.NooBaa {
165263
return &nbv1.NooBaa{
@@ -249,3 +347,23 @@ var _ = Describe("getExternalDNSDetails", func() {
249347
}
250348
})
251349
})
350+
351+
var _ = Describe("GetQuotaConfig", func() {
352+
It("uses the minimum maxObjects when BucketClass and OBC both specify a limit", func() {
353+
bcSpec := &nbv1.BucketClassSpec{
354+
Quota: &nbv1.Quota{
355+
MaxObjects: "1000",
356+
},
357+
}
358+
obConfig := map[string]string{
359+
"maxObjects": "2000",
360+
}
361+
log := logrus.NewEntry(logrus.New())
362+
363+
quota, err := GetQuotaConfig("test-bucket", bcSpec, obConfig, log)
364+
365+
Expect(err).NotTo(HaveOccurred())
366+
Expect(quota.Quantity).NotTo(BeNil())
367+
Expect(quota.Quantity.Value).To(Equal(1000))
368+
})
369+
})

pkg/obc/provisioner.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,11 @@ func NewBucketRequest(
454454
r.AccountName = ob.Spec.AdditionalState["account"]
455455
bucketClassName := ob.Spec.AdditionalState["bucketclass"]
456456

457-
bucketClass, exists := getBucketClass(r.OBC, bucketOptions, p.Namespace, util.KubeCheck)
457+
obcNamespace := ""
458+
if ob.Spec.ClaimRef != nil {
459+
obcNamespace = ob.Spec.ClaimRef.Namespace
460+
}
461+
bucketClass, exists := getBucketClassByName(bucketClassName, obcNamespace, p.Namespace, util.KubeCheck)
458462
if !exists {
459463
p.Logger.Warnf("BucketClass %q not found in namespace %q", bucketClassName, p.Namespace)
460464
}
@@ -910,16 +914,10 @@ func (r *BucketRequest) updateReplicationPolicy(ob *nbv1.ObjectBucket) error {
910914
return nil
911915
}
912916

913-
// getBucketClass takes an OBC, bucketoptions and provisioner namespace and returns the bucketClass
914-
//
915-
// If BucketClass name is not specified in the OBC, then the empty string is returned with exists=false
916-
// If BucketClass name is specified in the OBC, then:
917-
// - if the bucketclass is found in the obc namespace, then that bucketclass is returned
918-
// with exists=true
919-
// - if the bucketclass is found in the provisioner namespace, then that buckeclass is
920-
// returned with exists=true
921-
// - if the bucketclass is not found in the obc namespace or the provisioner namespace, then the
922-
// bucketclass with namespace set to provisioner namespace is returned with exists=false
917+
// getBucketClass extracts the bucket class name from an OBC (or StorageClass parameters)
918+
// and delegates to getBucketClassByName. Used by the create/provision path where a full
919+
// ObjectBucketClaim is available. On the update path (ObjectBucket only), callers should
920+
// use getBucketClassByName directly with values from AdditionalState and ClaimRef.
923921
func getBucketClass(
924922
obc *nbv1.ObjectBucketClaim,
925923
bucketOptions *obAPI.BucketOptions,
@@ -942,16 +940,40 @@ func getBucketClass(
942940
if bucketclassName == "" && bucketOptions != nil {
943941
bucketclassName = bucketOptions.Parameters["bucketclass"]
944942
}
945-
if bucketclassName == "" {
943+
return getBucketClassByName(bucketclassName, obc.Namespace, provisionerNS, checkExists)
944+
}
945+
946+
// getBucketClassByName looks up a BucketClass by name in the OBC namespace first,
947+
// then in the provisioner (NooBaa system) namespace.
948+
//
949+
// If bucketClassName is empty, returns exists=false.
950+
// If found in obcNamespace, returns that BucketClass with exists=true.
951+
// If found in provisionerNS, returns that BucketClass with exists=true.
952+
// If not found, returns a BucketClass with namespace set to provisionerNS and exists=false.
953+
func getBucketClassByName(
954+
bucketClassName, obcNamespace, provisionerNS string,
955+
checkExists func(client.Object) bool,
956+
) (bc *nbv1.BucketClass, exists bool) {
957+
bucketClass := &nbv1.BucketClass{
958+
TypeMeta: metav1.TypeMeta{Kind: "BucketClass"},
959+
ObjectMeta: metav1.ObjectMeta{
960+
Name: "",
961+
Namespace: "",
962+
},
963+
}
964+
965+
if bucketClassName == "" {
946966
return bucketClass, false
947967
}
948968

949-
bucketClass.SetName(bucketclassName)
969+
bucketClass.SetName(bucketClassName)
950970

951971
// Find the bucketclass in the same namespace as the OBC
952-
bucketClass.SetNamespace(obc.Namespace)
953-
if checkExists(bucketClass) {
954-
return bucketClass, true
972+
if obcNamespace != "" {
973+
bucketClass.SetNamespace(obcNamespace)
974+
if checkExists(bucketClass) {
975+
return bucketClass, true
976+
}
955977
}
956978

957979
// Find the bucketclass in the provisioner namespace

0 commit comments

Comments
 (0)