Added support for a custom prefix

This commit is contained in:
Nuwan Goonasekera 2022-05-21 23:29:55 +05:30
parent fd5d022ca8
commit 247a636105
5 changed files with 35 additions and 2 deletions

View file

@ -26,6 +26,10 @@ stringData:
endpoint: https://storage.yandexcloud.net endpoint: https://storage.yandexcloud.net
# For AWS set it to AWS region # For AWS set it to AWS region
#region: "" #region: ""
# '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
``` ```
The region can be empty if you are using some other S3 compatible storage. The region can be empty if you are using some other S3 compatible storage.

View file

@ -9,3 +9,7 @@ stringData:
secretAccessKey: {{ .Values.secret.secretKey }} secretAccessKey: {{ .Values.secret.secretKey }}
endpoint: {{ .Values.secret.endpoint }} endpoint: {{ .Values.secret.endpoint }}
{{- end -}} {{- end -}}
{{- if .Values.secret.usePrefix }}
usePrefix: "true"
{{- end }}
prefix: "{{ .Values.secret.prefix }}"

View file

@ -37,3 +37,7 @@ secret:
secretKey: "" secretKey: ""
# Endpoint # Endpoint
endpoint: https://storage.yandexcloud.net endpoint: https://storage.yandexcloud.net
# 'usePrefix' must be true in order to enable the prefix feature and to avoid the removal of the prefix or bucket
usePrefix:
# 'prefix' can be empty (it will mount on the root of the bucket), an existing prefix or a new one.
prefix:

View file

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"io" "io"
"path" "path"
"strconv"
"strings" "strings"
"github.com/yandex-cloud/k8s-csi-s3/pkg/mounter" "github.com/yandex-cloud/k8s-csi-s3/pkg/mounter"
@ -41,10 +42,12 @@ type controllerServer struct {
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
params := req.GetParameters() params := req.GetParameters()
secrets := req.GetSecrets()
capacityBytes := int64(req.GetCapacityRange().GetRequiredBytes()) capacityBytes := int64(req.GetCapacityRange().GetRequiredBytes())
volumeID := sanitizeVolumeID(req.GetName()) volumeID := sanitizeVolumeID(req.GetName())
bucketName := volumeID bucketName := volumeID
prefix := "" prefix := ""
usePrefix, usePrefixError := strconv.ParseBool(secrets[mounter.UsePrefix])
// check if bucket name is overridden // check if bucket name is overridden
if params[mounter.BucketKey] != "" { if params[mounter.BucketKey] != "" {
@ -53,6 +56,15 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
volumeID = path.Join(bucketName, prefix) volumeID = path.Join(bucketName, prefix)
} }
// check if volume prefix is overridden
if usePrefixError == nil && usePrefix {
prefix = ""
if prefixOverride, ok := secrets[mounter.VolumePrefix]; ok && prefixOverride != "" {
prefix = prefixOverride
}
volumeID = path.Join(bucketName, prefix)
}
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil { if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid create volume req: %v", req) glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err return nil, err
@ -85,7 +97,9 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
} }
if err = client.CreatePrefix(bucketName, prefix); err != nil { if err = client.CreatePrefix(bucketName, prefix); err != nil {
return nil, fmt.Errorf("failed to create prefix %s: %v", prefix, err) if usePrefixError != nil || !usePrefix {
return nil, fmt.Errorf("failed to create prefix %s: %v", prefix, err)
}
} }
glog.V(4).Infof("create volume %s", volumeID) glog.V(4).Infof("create volume %s", volumeID)
@ -126,7 +140,12 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
} }
var deleteErr error var deleteErr error
if prefix == "" { usePrefix, usePrefixError := strconv.ParseBool(req.GetSecrets()[mounter.UsePrefix])
if usePrefixError == nil && 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 // prefix is empty, we delete the whole bucket
if err := client.RemoveBucket(bucketName); err != nil && err.Error() != "The specified bucket does not exist" { if err := client.RemoveBucket(bucketName); err != nil && err.Error() != "The specified bucket does not exist" {
deleteErr = err deleteErr = err

View file

@ -32,6 +32,8 @@ const (
TypeKey = "mounter" TypeKey = "mounter"
BucketKey = "bucket" BucketKey = "bucket"
OptionsKey = "options" OptionsKey = "options"
VolumePrefix = "prefix"
UsePrefix = "usePrefix"
) )
// New returns a new mounter depending on the mounterType parameter // New returns a new mounter depending on the mounterType parameter