2018-07-14 08:48:22 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2019-02-06 19:02:28 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2018-07-14 08:48:22 +00:00
|
|
|
"fmt"
|
2019-02-06 19:02:28 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
2018-07-14 08:48:22 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2019-02-28 18:34:03 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2019-03-10 11:19:02 +00:00
|
|
|
csicommon "github.com/kubernetes-csi/drivers/pkg/csi-common"
|
2018-07-14 08:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type controllerServer struct {
|
|
|
|
*csicommon.DefaultControllerServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
2019-02-06 19:02:28 +00:00
|
|
|
volumeID := sanitizeVolumeID(req.GetName())
|
2018-07-27 10:56:28 +00:00
|
|
|
|
2018-07-14 08:48:22 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
|
|
|
glog.V(3).Infof("invalid create volume req: %v", req)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check arguments
|
2018-07-27 10:56:28 +00:00
|
|
|
if len(volumeID) == 0 {
|
2018-07-14 08:48:22 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
|
|
|
|
}
|
|
|
|
if req.GetVolumeCapabilities() == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities missing in request")
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:56:28 +00:00
|
|
|
capacityBytes := int64(req.GetCapacityRange().GetRequiredBytes())
|
2018-07-29 08:00:53 +00:00
|
|
|
params := req.GetParameters()
|
|
|
|
mounter := params[mounterTypeKey]
|
2018-07-27 10:56:28 +00:00
|
|
|
|
2018-07-29 14:19:26 +00:00
|
|
|
glog.V(4).Infof("Got a request to create volume %s", volumeID)
|
2018-07-14 08:48:22 +00:00
|
|
|
|
2019-03-10 11:19:02 +00:00
|
|
|
s3, err := newS3ClientFromSecrets(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize S3 client: %s", err)
|
|
|
|
}
|
|
|
|
exists, err := s3.bucketExists(volumeID)
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
2019-01-19 13:39:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to check if bucket %s exists: %v", volumeID, err)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2018-07-27 10:56:28 +00:00
|
|
|
if exists {
|
|
|
|
var b *bucket
|
2019-03-10 11:19:02 +00:00
|
|
|
b, err = s3.getBucket(volumeID)
|
2018-07-27 10:56:28 +00:00
|
|
|
if err != nil {
|
2019-01-19 13:39:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to get bucket metadata of bucket %s: %v", volumeID, err)
|
2018-07-27 10:56:28 +00:00
|
|
|
}
|
|
|
|
// 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 {
|
2019-03-10 11:19:02 +00:00
|
|
|
if err = s3.createBucket(volumeID); err != nil {
|
2019-01-19 13:39:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to create volume %s: %v", volumeID, err)
|
2018-07-27 19:37:32 +00:00
|
|
|
}
|
2019-03-10 11:19:02 +00:00
|
|
|
if err = s3.createPrefix(volumeID, fsPrefix); err != nil {
|
2019-01-19 13:39:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to create prefix %s: %v", fsPrefix, err)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-27 10:56:28 +00:00
|
|
|
b := &bucket{
|
|
|
|
Name: volumeID,
|
2018-07-29 08:00:53 +00:00
|
|
|
Mounter: mounter,
|
2018-07-27 10:56:28 +00:00
|
|
|
CapacityBytes: capacityBytes,
|
2018-07-27 19:37:32 +00:00
|
|
|
FSPath: fsPrefix,
|
2018-07-27 10:56:28 +00:00
|
|
|
}
|
2019-03-10 11:19:02 +00:00
|
|
|
if err := s3.setBucket(b); err != nil {
|
2018-07-27 19:37:32 +00:00
|
|
|
return nil, fmt.Errorf("Error setting bucket metadata: %v", err)
|
2018-07-27 10:56:28 +00:00
|
|
|
}
|
2018-07-21 13:23:11 +00:00
|
|
|
|
2018-07-14 08:48:22 +00:00
|
|
|
glog.V(4).Infof("create volume %s", volumeID)
|
|
|
|
s3Vol := s3Volume{}
|
2018-07-27 10:56:28 +00:00
|
|
|
s3Vol.VolName = volumeID
|
2018-07-14 08:48:22 +00:00
|
|
|
s3Vol.VolID = volumeID
|
|
|
|
return &csi.CreateVolumeResponse{
|
|
|
|
Volume: &csi.Volume{
|
2019-02-28 18:34:03 +00:00
|
|
|
VolumeId: volumeID,
|
2018-07-27 10:56:28 +00:00
|
|
|
CapacityBytes: capacityBytes,
|
2019-02-28 18:34:03 +00:00
|
|
|
VolumeContext: req.GetParameters(),
|
2018-07-14 08:48:22 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
|
2018-07-27 10:56:28 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
2018-07-14 08:48:22 +00:00
|
|
|
|
|
|
|
// Check arguments
|
2018-07-27 10:56:28 +00:00
|
|
|
if len(volumeID) == 0 {
|
2018-07-14 08:48:22 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
|
|
|
glog.V(3).Infof("Invalid delete volume req: %v", req)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Deleting volume %s", volumeID)
|
|
|
|
|
2019-03-10 11:19:02 +00:00
|
|
|
s3, err := newS3ClientFromSecrets(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize S3 client: %s", err)
|
|
|
|
}
|
|
|
|
exists, err := s3.bucketExists(volumeID)
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if exists {
|
2019-03-10 11:19:02 +00:00
|
|
|
if err := s3.removeBucket(volumeID); err != nil {
|
2019-05-14 19:01:54 +00:00
|
|
|
glog.V(3).Infof("Failed to remove volume %s: %v", volumeID, err)
|
2018-07-14 08:48:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(5).Infof("Bucket %s does not exist, ignoring request", volumeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
|
|
|
|
|
|
|
|
// Check arguments
|
|
|
|
if len(req.GetVolumeId()) == 0 {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
|
|
|
|
}
|
|
|
|
if req.GetVolumeCapabilities() == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Volume capabilities missing in request")
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:19:02 +00:00
|
|
|
s3, err := newS3ClientFromSecrets(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize S3 client: %s", err)
|
|
|
|
}
|
|
|
|
exists, err := s3.bucketExists(req.GetVolumeId())
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
// return an error if the volume requested does not exist
|
|
|
|
return nil, status.Error(codes.NotFound, fmt.Sprintf("Volume with id %s does not exist", req.GetVolumeId()))
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:09:56 +00:00
|
|
|
// We currently only support RWO
|
|
|
|
supportedAccessMode := &csi.VolumeCapability_AccessMode{
|
|
|
|
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cap := range req.VolumeCapabilities {
|
|
|
|
if cap.GetAccessMode().GetMode() != supportedAccessMode.GetMode() {
|
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Message: "Only single node writer is supported"}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{
|
|
|
|
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
|
|
|
|
VolumeCapabilities: []*csi.VolumeCapability{
|
|
|
|
{
|
|
|
|
AccessMode: supportedAccessMode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2019-02-06 19:02:28 +00:00
|
|
|
|
2019-05-14 19:01:54 +00:00
|
|
|
func (cs *controllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
|
|
|
|
return &csi.ControllerExpandVolumeResponse{}, status.Error(codes.Unimplemented, "ControllerExpandVolume is not implemented")
|
|
|
|
}
|
|
|
|
|
2019-02-06 19:02:28 +00:00
|
|
|
func sanitizeVolumeID(volumeID string) string {
|
|
|
|
volumeID = strings.ToLower(volumeID)
|
|
|
|
if len(volumeID) > 63 {
|
|
|
|
h := sha1.New()
|
|
|
|
io.WriteString(h, volumeID)
|
|
|
|
volumeID = hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
return volumeID
|
|
|
|
}
|