Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 354d932

Browse files
authored
Merge pull request #60 from defrox/issue-55
feat: enable mounting an existing bucket #55
2 parents 28af276 + 0ad1b0b commit 354d932

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ parameters:
104104

105105
If the bucket is specified, it will still be created if it does not exist on the backend. Every volume will get its own prefix within the bucket which matches the volume ID. When deleting a volume, also just the prefix will be deleted.
106106

107+
#### Using an existing bucket with custom prefix
108+
109+
If you have an existing bucket and with or without a prefix (subpath), you can specify to use a prefixed configuration by setting the parameters as:
110+
111+
```yaml
112+
kind: StorageClass
113+
apiVersion: storage.k8s.io/v1
114+
metadata:
115+
name: csi-s3-existing-bucket
116+
provisioner: ch.ctrox.csi.s3-driver
117+
reclaimPolicy: Retain
118+
parameters:
119+
mounter: rclone
120+
bucket: some-existing-bucket-name
121+
# 'usePrefix' must be true in order to enable the prefix feature and to avoid the removal of the prefix or bucket
122+
usePrefix: "true"
123+
# 'prefix' can be empty (it will mount on the root of the bucket), an existing prefix or a new one.
124+
prefix: custom-prefix
125+
```
126+
**Note:** all volumes created with this `StorageClass` will always be mounted to the same bucket and path, meaning they will be identical.
127+
107128
### Mounter
108129

109130
As S3 is not a real file system there are some limitations to consider here. Depending on what mounter you are using, you will have different levels of POSIX compability. Also depending on what S3 storage backend you are using there are not always [consistency guarantees](https://github.com/gaul/are-we-consistent-yet#observed-consistency).

pkg/driver/controllerserver.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"path"
25+
"strconv"
2526
"strings"
2627

2728
"github.com/ctrox/csi-s3/pkg/mounter"
@@ -50,6 +51,8 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
5051
volumeID := sanitizeVolumeID(req.GetName())
5152
bucketName := volumeID
5253
prefix := ""
54+
usePrefix, usePrefixError := strconv.ParseBool(params[mounter.UsePrefix])
55+
defaultFsPath := defaultFsPath
5356

5457
// check if bucket name is overridden
5558
if nameOverride, ok := params[mounter.BucketKey]; ok {
@@ -58,6 +61,16 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
5861
volumeID = path.Join(bucketName, prefix)
5962
}
6063

64+
// check if volume prefix is overridden
65+
if overridePrefix := usePrefix; usePrefixError == nil && overridePrefix {
66+
prefix = ""
67+
defaultFsPath = ""
68+
if prefixOverride, ok := params[mounter.VolumePrefix]; ok && prefixOverride != "" {
69+
prefix = prefixOverride
70+
}
71+
volumeID = path.Join(bucketName, prefix)
72+
}
73+
6174
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
6275
glog.V(3).Infof("invalid create volume req: %v", req)
6376
return nil, err
@@ -75,6 +88,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
7588

7689
meta := &s3.FSMeta{
7790
BucketName: bucketName,
91+
UsePrefix: usePrefix,
7892
Prefix: prefix,
7993
Mounter: mounterType,
8094
CapacityBytes: capacityBytes,
@@ -108,7 +122,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
108122
}
109123
}
110124

111-
if err = client.CreatePrefix(bucketName, path.Join(prefix, defaultFsPath)); err != nil {
125+
if err = client.CreatePrefix(bucketName, path.Join(prefix, defaultFsPath)); err != nil && prefix != "" {
112126
return nil, fmt.Errorf("failed to create prefix %s: %v", path.Join(prefix, defaultFsPath), err)
113127
}
114128

@@ -153,7 +167,11 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
153167
}
154168

155169
var deleteErr error
156-
if prefix == "" {
170+
if meta.UsePrefix {
171+
// UsePrefix is true, we do not delete anything
172+
glog.V(4).Infof("Nothing to remove for %s", bucketName)
173+
return &csi.DeleteVolumeResponse{}, nil
174+
} else if prefix == "" {
157175
// prefix is empty, we delete the whole bucket
158176
if err := client.RemoveBucket(bucketName); err != nil {
159177
deleteErr = err

pkg/driver/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type driver struct {
3333
}
3434

3535
var (
36-
vendorVersion = "v1.2.0-rc.1"
36+
vendorVersion = "v1.2.0-rc.2"
3737
driverName = "ch.ctrox.csi.s3-driver"
3838
)
3939

pkg/mounter/mounter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
rcloneMounterType = "rclone"
3232
TypeKey = "mounter"
3333
BucketKey = "bucket"
34+
VolumePrefix = "prefix"
35+
UsePrefix = "usePrefix"
3436
)
3537

3638
// New returns a new mounter depending on the mounterType parameter

pkg/s3/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io"
9-
"net/url"
10-
"path"
11-
128
"github.com/golang/glog"
139
"github.com/minio/minio-go/v7"
1410
"github.com/minio/minio-go/v7/pkg/credentials"
11+
"io"
12+
"net/url"
13+
"path"
1514
)
1615

1716
const (
@@ -36,6 +35,7 @@ type Config struct {
3635
type FSMeta struct {
3736
BucketName string `json:"Name"`
3837
Prefix string `json:"Prefix"`
38+
UsePrefix bool `json:"UsePrefix"`
3939
Mounter string `json:"Mounter"`
4040
FSPath string `json:"FSPath"`
4141
CapacityBytes int64 `json:"CapacityBytes"`

0 commit comments

Comments
 (0)