Skip to content

Commit

Permalink
feat: some refactoring after comments
Browse files Browse the repository at this point in the history
  • Loading branch information
defrox committed Feb 1, 2022
1 parent d3537c7 commit 0ad1b0b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ reclaimPolicy: Retain
parameters:
mounter: rclone
bucket: some-existing-bucket-name
# 'usePrefix' must be true in order to enable the prefix feature and to avoid the removal of the bucket
# 'usePrefix' must be true in order to enable the prefix feature and to avoid the removal of the prefix or bucket
usePrefix: "true"
# 'prefix' can be empty (it will mount on the root of the bucket), an existing prefix or a new one.
prefix: custom-prefix
```
**Note:** all volumes created with this `StorageClass` will always be mounted to the same bucket and path, meaning they will be identical.

### Mounter

Expand Down
7 changes: 4 additions & 3 deletions pkg/driver/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
volumeID := sanitizeVolumeID(req.GetName())
bucketName := volumeID
prefix := ""
usePrefix := params[mounter.UsePrefix]
usePrefix, usePrefixError := strconv.ParseBool(params[mounter.UsePrefix])
defaultFsPath := defaultFsPath

// check if bucket name is overridden
Expand All @@ -62,7 +62,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
}

// check if volume prefix is overridden
if overridePrefix, err := strconv.ParseBool(usePrefix); err == nil && overridePrefix {
if overridePrefix := usePrefix; usePrefixError == nil && overridePrefix {
prefix = ""
defaultFsPath = ""
if prefixOverride, ok := params[mounter.VolumePrefix]; ok && prefixOverride != "" {
Expand Down Expand Up @@ -167,9 +167,10 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
}

var deleteErr error
if usePrefix, err := strconv.ParseBool(client.GetFSMetaField(meta, mounter.UsePrefix)); err == nil && usePrefix {
if meta.UsePrefix {
// UsePrefix is true, we do not delete anything
glog.V(4).Infof("Nothing to remove for %s", bucketName)
return &csi.DeleteVolumeResponse{}, nil
} else if prefix == "" {
// prefix is empty, we delete the whole bucket
if err := client.RemoveBucket(bucketName); err != nil {
Expand Down
16 changes: 4 additions & 12 deletions pkg/s3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"path"
"reflect"

"github.com/golang/glog"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"io"
"net/url"
"path"
)

const (
Expand All @@ -37,7 +35,7 @@ type Config struct {
type FSMeta struct {
BucketName string `json:"Name"`
Prefix string `json:"Prefix"`
UsePrefix string `json:"UsePrefix"`
UsePrefix bool `json:"UsePrefix"`
Mounter string `json:"Mounter"`
FSPath string `json:"FSPath"`
CapacityBytes int64 `json:"CapacityBytes"`
Expand Down Expand Up @@ -252,9 +250,3 @@ func (client *s3Client) GetFSMeta(bucketName, prefix string) (*FSMeta, error) {
err = json.Unmarshal(b, &meta)
return &meta, err
}

func (client *s3Client) GetFSMetaField(meta *FSMeta, field string) string {
r := reflect.ValueOf(meta)
f := reflect.Indirect(r).FieldByName(field)
return f.String()
}

0 comments on commit 0ad1b0b

Please sign in to comment.