diff --git a/go.sum b/go.sum index 21e5cb8..e45ef21 100644 --- a/go.sum +++ b/go.sum @@ -175,6 +175,7 @@ gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= k8s.io/apimachinery v0.0.0-20180714051327-705cfa51a97f h1:mjXiDUfs+4mhzRTLNTkAfQS9lqJCXQN/fIcMysNGW/Y= k8s.io/apimachinery v0.0.0-20180714051327-705cfa51a97f/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0= diff --git a/pkg/s3/controllerserver.go b/pkg/s3/controllerserver.go index 1b25c6d..e7cf296 100644 --- a/pkg/s3/controllerserver.go +++ b/pkg/s3/controllerserver.go @@ -37,7 +37,12 @@ type controllerServer struct { } func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { + params := req.GetParameters() + volumeID := sanitizeVolumeID(req.GetName()) + if bucketName, bucketExists := params[bucketKey]; bucketExists { + volumeID = sanitizeVolumeID(bucketName) + } if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil { glog.V(3).Infof("invalid create volume req: %v", req) @@ -53,11 +58,10 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol } capacityBytes := int64(req.GetCapacityRange().GetRequiredBytes()) - params := req.GetParameters() + mounter := params[mounterTypeKey] glog.V(4).Infof("Got a request to create volume %s", volumeID) - s3, err := newS3ClientFromSecrets(req.GetSecrets()) if err != nil { return nil, fmt.Errorf("failed to initialize S3 client: %s", err) @@ -69,12 +73,19 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol if exists { var b *bucket b, err = s3.getBucket(volumeID) + // TODO 如果 bucket 已经存在了,为什么要去检查它是否有 metadata 和 capacity 呢? + // 或者说 metadata 的作用是什么? if err != nil { - return nil, fmt.Errorf("failed to get bucket metadata of bucket %s: %v", volumeID, err) - } - // Check if volume capacity requested is bigger than the already existing capacity - if capacityBytes > b.CapacityBytes { - return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("Volume with the same name: %s but with smaller size already exist", volumeID)) + glog.Warningf("failed to get bucket metadata of bucket %s: %v", volumeID, err) + // return nil, fmt.Errorf("failed to get bucket metadata of bucket %s: %v", volumeID, err) + if err = s3.createPrefix(volumeID, fsPrefix); err != nil { + return nil, fmt.Errorf("failed to create prefix %s: %v", fsPrefix, err) + } + } else { + // Check if volume capacity requested is bigger than the already existing capacity + if capacityBytes > b.CapacityBytes { + return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("Volume with the same name: %s but with smaller size already exist", volumeID)) + } } } else { if err = s3.createBucket(volumeID); err != nil { diff --git a/pkg/s3/mounter.go b/pkg/s3/mounter.go index 0ac2bf9..bda6dbe 100644 --- a/pkg/s3/mounter.go +++ b/pkg/s3/mounter.go @@ -23,6 +23,7 @@ const ( s3backerMounterType = "s3backer" rcloneMounterType = "rclone" mounterTypeKey = "mounter" + bucketKey = "bucket" ) // newMounter returns a new mounter depending on the mounterType parameter diff --git a/test/Dockerfile b/test/Dockerfile index 3cb4237..efe8afb 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -7,9 +7,9 @@ RUN apt-get update && \ git wget make && \ rm -rf /var/lib/apt/lists/* -RUN wget -q https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz && \ - tar -xf go1.12.5.linux-amd64.tar.gz && \ - rm go1.12.5.linux-amd64.tar.gz && \ +RUN wget -q https://golang.org/dl/go1.15.7.linux-amd64.tar.gz && \ + tar -xf go1.15.7.linux-amd64.tar.gz && \ + rm go1.15.7.linux-amd64.tar.gz && \ mv go /usr/local ENV GOROOT /usr/local/go