Skip to content

Commit

Permalink
feat(oss): impl deletions logic for bucket (#308)
Browse files Browse the repository at this point in the history
Signed-off-by: maslow <[email protected]>
  • Loading branch information
maslow authored Sep 2, 2022
1 parent d854642 commit 9c669a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
28 changes: 25 additions & 3 deletions controllers/oss/controllers/bucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"github.com/labring/laf/controllers/oss/driver"
"github.com/minio/minio-go/v7"
"laf/pkg/util"
"time"

Expand Down Expand Up @@ -53,7 +54,6 @@ type BucketReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

// get the bucket
var bucket ossv1.Bucket
Expand Down Expand Up @@ -114,14 +114,36 @@ func (r *BucketReconciler) apply(ctx context.Context, bucket *ossv1.Bucket) (ctr
_log.Info("Set bucket quota", "bucket", bucket.Name, "quota", bucket.Spec.Storage.String())
}

// TODO: reconcile the bucket capacity
// TODO: sync the bucket capacity

return ctrl.Result{RequeueAfter: time.Minute * 15}, nil
}

// delete the bucket
func (r *BucketReconciler) delete(ctx context.Context, bucket *ossv1.Bucket) (ctrl.Result, error) {
// TODO: delete the bucket
_log := log.FromContext(ctx)

// create the minio admin client
mca, err := r.createMinioClient(ctx, bucket)
if err != nil {
return ctrl.Result{}, err
}

// delete the bucket
if err := mca.DeleteBucket(bucket.Name); err != nil {
// reject if the bucket is not exists
if minio.ToErrorResponse(err).Code != "NoSuchBucket" {
return ctrl.Result{}, nil
}
}

// remove the finalizer
bucket.ObjectMeta.Finalizers = util.RemoveString(bucket.ObjectMeta.Finalizers, bucketFinalizer)
if err := r.Update(ctx, bucket); err != nil {
return ctrl.Result{}, err
}

_log.Info("Deleted bucket", "bucket", bucket.Name)
return ctrl.Result{}, nil
}

Expand Down
15 changes: 14 additions & 1 deletion controllers/oss/driver/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,23 @@ func (mca *MinioClientAdmin) CreateBucket(bucketName string, region string, igno

// DeleteBucket - deletes a bucket in minio.
func (mca *MinioClientAdmin) DeleteBucket(bucketName string) error {
err := mca.s3Client.RemoveBucket(mca.context, bucketName)
err := mca.s3Client.RemoveBucketWithOptions(mca.context, bucketName, minio.RemoveBucketOptions{
ForceDelete: true,
})

return err
}

// DataUsageInfo - returns the bucket info.
func (mca *MinioClientAdmin) DataUsageInfo(bucketName string) (*madmin.DataUsageInfo, error) {
stats, err := mca.adminClient.DataUsageInfo(mca.context)
if err != nil {
return nil, err
}

return &stats, nil
}

// SetBucketPolicy - sets a bucket policy in minio.
func (mca *MinioClientAdmin) SetBucketPolicy(bucketName string, policy v1.BucketPolicy) error {
var policyString string
Expand Down

0 comments on commit 9c669a9

Please sign in to comment.