2018-01-09 18:59:50 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-01-09 18:59:50 +00:00
|
|
|
|
|
|
|
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 rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-08 05:42:17 +00:00
|
|
|
"os/exec"
|
2019-02-26 13:19:00 +00:00
|
|
|
"sort"
|
2019-02-18 08:22:52 +00:00
|
|
|
"strconv"
|
2018-08-08 05:42:17 +00:00
|
|
|
"syscall"
|
2018-01-09 18:59:50 +00:00
|
|
|
|
2019-03-04 13:32:10 +00:00
|
|
|
csicommon "github.com/ceph/ceph-csi/pkg/csi-common"
|
2018-12-19 14:26:16 +00:00
|
|
|
"github.com/ceph/ceph-csi/pkg/util"
|
2019-02-18 11:30:28 +00:00
|
|
|
|
2019-01-15 16:20:41 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2019-01-25 13:39:35 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-11-24 19:18:24 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2019-02-08 07:50:21 +00:00
|
|
|
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
|
2018-01-09 18:59:50 +00:00
|
|
|
"github.com/pborman/uuid"
|
2019-03-07 12:56:47 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-01-09 18:59:50 +00:00
|
|
|
"golang.org/x/net/context"
|
2018-03-06 22:33:57 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-02-04 13:05:07 +00:00
|
|
|
"k8s.io/klog"
|
2018-01-09 18:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
oneGB = 1073741824
|
|
|
|
)
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ControllerServer struct of rbd CSI driver with supported methods of CSI
|
|
|
|
// controller server spec.
|
2019-01-17 07:51:06 +00:00
|
|
|
type ControllerServer struct {
|
2018-01-09 18:59:50 +00:00
|
|
|
*csicommon.DefaultControllerServer
|
2018-12-19 14:26:16 +00:00
|
|
|
MetadataStore util.CachePersister
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
rbdVolumes = map[string]*rbdVolume{}
|
|
|
|
rbdSnapshots = map[string]*rbdSnapshot{}
|
|
|
|
)
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// LoadExDataFromMetadataStore loads the rbd volume and snapshot
|
|
|
|
// info from metadata store
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) LoadExDataFromMetadataStore() error {
|
2018-12-19 14:26:16 +00:00
|
|
|
vol := &rbdVolume{}
|
2019-01-28 19:55:10 +00:00
|
|
|
// nolint
|
2018-12-19 14:26:16 +00:00
|
|
|
cs.MetadataStore.ForAll("csi-rbd-vol-", vol, func(identifier string) error {
|
|
|
|
rbdVolumes[identifier] = vol
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
snap := &rbdSnapshot{}
|
2019-01-28 19:55:10 +00:00
|
|
|
// nolint
|
2018-12-19 14:26:16 +00:00
|
|
|
cs.MetadataStore.ForAll("csi-rbd-(.*)-snap-", snap, func(identifier string) error {
|
|
|
|
rbdSnapshots[identifier] = snap
|
|
|
|
return nil
|
|
|
|
})
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Infof("Loaded %d volumes and %d snapshots from metadata store", len(rbdVolumes), len(rbdSnapshots))
|
2018-12-19 14:26:16 +00:00
|
|
|
return nil
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) validateVolumeReq(req *csi.CreateVolumeRequest) error {
|
2018-03-06 22:33:57 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
2019-02-08 07:50:21 +00:00
|
|
|
klog.V(3).Infof("invalid create volume req: %v", protosanitizer.StripSecrets(req))
|
2019-01-17 05:27:55 +00:00
|
|
|
return err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2018-03-06 22:33:57 +00:00
|
|
|
// Check sanity of request Name, Volume Capabilities
|
|
|
|
if len(req.Name) == 0 {
|
2019-01-17 05:27:55 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
|
2018-03-06 22:33:57 +00:00
|
|
|
}
|
|
|
|
if req.VolumeCapabilities == nil {
|
2019-01-17 05:27:55 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
|
2018-03-06 22:33:57 +00:00
|
|
|
}
|
2019-01-17 05:27:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-16 01:52:28 +00:00
|
|
|
|
2019-01-29 05:49:16 +00:00
|
|
|
func parseVolCreateRequest(req *csi.CreateVolumeRequest) (*rbdVolume, error) {
|
|
|
|
// TODO (sbezverk) Last check for not exceeding total storage capacity
|
|
|
|
|
2019-03-14 00:18:04 +00:00
|
|
|
isMultiNode := false
|
|
|
|
isBlock := false
|
|
|
|
for _, cap := range req.VolumeCapabilities {
|
2019-03-18 16:08:24 +00:00
|
|
|
// RO modes need to be handled indepedently (ie right now even if access mode is RO, they'll be RW upon attach)
|
|
|
|
if cap.GetAccessMode().GetMode() == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER {
|
2019-03-14 00:18:04 +00:00
|
|
|
isMultiNode = true
|
|
|
|
}
|
|
|
|
if cap.GetBlock() != nil {
|
|
|
|
isBlock = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to fail early if the user is trying to create a RWX on a non-block type device
|
|
|
|
if isMultiNode && !isBlock {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "multi node access modes are only supported on rbd `block` type volumes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's NOT SINGLE_NODE_WRITER and it's BLOCK we'll set the parameter to ignore the in-use checks
|
|
|
|
rbdVol, err := getRBDVolumeOptions(req.GetParameters(), (isMultiNode && isBlock))
|
2019-01-29 05:49:16 +00:00
|
|
|
if err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2019-01-29 05:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generating Volume Name and Volume ID, as according to CSI spec they MUST be different
|
|
|
|
volName := req.GetName()
|
|
|
|
uniqueID := uuid.NewUUID().String()
|
|
|
|
rbdVol.VolName = volName
|
|
|
|
volumeID := "csi-rbd-vol-" + uniqueID
|
|
|
|
rbdVol.VolID = volumeID
|
|
|
|
// Volume Size - Default is 1 GiB
|
|
|
|
volSizeBytes := int64(oneGB)
|
|
|
|
if req.GetCapacityRange() != nil {
|
|
|
|
volSizeBytes = req.GetCapacityRange().GetRequiredBytes()
|
|
|
|
}
|
2019-03-01 12:08:17 +00:00
|
|
|
|
|
|
|
rbdVol.VolSize = util.RoundUpToMiB(volSizeBytes)
|
2019-01-29 05:49:16 +00:00
|
|
|
|
|
|
|
return rbdVol, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:31:26 +00:00
|
|
|
func storeVolumeMetadata(vol *rbdVolume, cp util.CachePersister) error {
|
|
|
|
if err := cp.Create(vol.VolID, vol); err != nil {
|
|
|
|
klog.Errorf("failed to store metadata for volume %s: %v", vol.VolID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// CreateVolume creates the volume in backend and store the volume metadata
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
2018-01-16 01:52:28 +00:00
|
|
|
|
2019-01-17 05:27:55 +00:00
|
|
|
if err := cs.validateVolumeReq(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-17 12:52:45 +00:00
|
|
|
volumeNameMutex.LockKey(req.GetName())
|
2019-01-28 13:59:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := volumeNameMutex.UnlockKey(req.GetName()); err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to unlock mutex volume:%s %v", req.GetName(), err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-01-16 01:52:28 +00:00
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
// Need to check for already existing volume name, and if found
|
|
|
|
// check for the requested capacity and already allocated capacity
|
|
|
|
if exVol, err := getRBDVolumeByName(req.GetName()); err == nil {
|
|
|
|
// Since err is nil, it means the volume with the same name already exists
|
2019-01-28 19:55:10 +00:00
|
|
|
// need to check if the size of existing volume is the same as in new
|
2018-03-06 22:33:57 +00:00
|
|
|
// request
|
2019-01-17 06:24:10 +00:00
|
|
|
if exVol.VolSize >= req.GetCapacityRange().GetRequiredBytes() {
|
2019-01-28 19:55:10 +00:00
|
|
|
// existing volume is compatible with new request and should be reused.
|
2019-02-14 19:31:26 +00:00
|
|
|
|
|
|
|
if err = storeVolumeMetadata(exVol, cs.MetadataStore); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
// TODO (sbezverk) Do I need to make sure that RBD volume still exists?
|
|
|
|
return &csi.CreateVolumeResponse{
|
|
|
|
Volume: &csi.Volume{
|
2018-11-24 19:18:24 +00:00
|
|
|
VolumeId: exVol.VolID,
|
2019-01-17 06:24:10 +00:00
|
|
|
CapacityBytes: exVol.VolSize,
|
2018-11-24 19:18:24 +00:00
|
|
|
VolumeContext: req.GetParameters(),
|
2018-03-06 22:33:57 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
|
2018-03-06 22:33:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 05:49:16 +00:00
|
|
|
rbdVol, err := parseVolCreateRequest(req)
|
2018-01-16 01:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
// Check if there is already RBD image with requested name
|
2019-03-01 12:08:17 +00:00
|
|
|
err = cs.checkRBDStatus(rbdVol, req, int(rbdVol.VolSize))
|
2019-01-28 19:55:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-03-12 13:07:10 +00:00
|
|
|
// store volume size in bytes (snapshot and check existing volume needs volume
|
|
|
|
// size in bytes)
|
|
|
|
rbdVol.VolSize = rbdVol.VolSize * util.MiB
|
2018-12-19 14:26:16 +00:00
|
|
|
|
2019-01-29 05:49:16 +00:00
|
|
|
rbdVolumes[rbdVol.VolID] = rbdVol
|
2019-02-14 19:31:26 +00:00
|
|
|
|
|
|
|
if err = storeVolumeMetadata(rbdVol, cs.MetadataStore); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
return &csi.CreateVolumeResponse{
|
2018-02-15 13:51:23 +00:00
|
|
|
Volume: &csi.Volume{
|
2019-01-29 05:49:16 +00:00
|
|
|
VolumeId: rbdVol.VolID,
|
2019-03-12 13:07:10 +00:00
|
|
|
CapacityBytes: rbdVol.VolSize,
|
2018-11-24 19:18:24 +00:00
|
|
|
VolumeContext: req.GetParameters(),
|
2018-01-09 18:59:50 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-01 12:08:17 +00:00
|
|
|
func (cs *ControllerServer) checkRBDStatus(rbdVol *rbdVolume, req *csi.CreateVolumeRequest, volSizeMiB int) error {
|
2019-01-28 19:55:10 +00:00
|
|
|
var err error
|
|
|
|
// Check if there is already RBD image with requested name
|
2019-03-04 13:32:10 +00:00
|
|
|
//nolint
|
|
|
|
found, _, _ := rbdStatus(rbdVol, rbdVol.UserID, req.GetSecrets())
|
2019-01-28 19:55:10 +00:00
|
|
|
if !found {
|
|
|
|
// if VolumeContentSource is not nil, this request is for snapshot
|
|
|
|
if req.VolumeContentSource != nil {
|
|
|
|
if err = cs.checkSnapshot(req, rbdVol); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-01 12:08:17 +00:00
|
|
|
err = createRBDImage(rbdVol, volSizeMiB, rbdVol.AdminID, req.GetSecrets())
|
2019-01-28 19:55:10 +00:00
|
|
|
if err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to create volume: %v", err)
|
2019-03-07 12:56:47 +00:00
|
|
|
return status.Error(codes.Internal, err.Error())
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(4).Infof("create volume %s", rbdVol.VolName)
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) checkSnapshot(req *csi.CreateVolumeRequest, rbdVol *rbdVolume) error {
|
2019-01-17 05:27:55 +00:00
|
|
|
snapshot := req.VolumeContentSource.GetSnapshot()
|
|
|
|
if snapshot == nil {
|
|
|
|
return status.Error(codes.InvalidArgument, "Volume Snapshot cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotID := snapshot.GetSnapshotId()
|
|
|
|
if len(snapshotID) == 0 {
|
|
|
|
return status.Error(codes.InvalidArgument, "Volume Snapshot ID cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
rbdSnap := &rbdSnapshot{}
|
|
|
|
if err := cs.MetadataStore.Get(snapshotID, rbdSnap); err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return status.Error(codes.NotFound, err.Error())
|
2019-01-17 05:27:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
err := restoreSnapshot(rbdVol, rbdSnap, rbdVol.AdminID, req.GetSecrets())
|
2019-01-17 05:27:55 +00:00
|
|
|
if err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return status.Error(codes.Internal, err.Error())
|
2019-01-17 05:27:55 +00:00
|
|
|
}
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(4).Infof("create volume %s from snapshot %s", req.GetName(), rbdSnap.SnapName)
|
2019-01-17 05:27:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-17 06:49:35 +00:00
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// DeleteVolume deletes the volume in backend and removes the volume metadata
|
|
|
|
// from store
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
|
2018-03-06 22:33:57 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
2019-02-08 07:50:21 +00:00
|
|
|
klog.Warningf("invalid delete volume req: %v", protosanitizer.StripSecrets(req))
|
2018-01-09 18:59:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// For now the image get unconditionally deleted, but here retention policy can be checked
|
2018-01-16 01:52:28 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
2018-10-17 12:52:45 +00:00
|
|
|
volumeIDMutex.LockKey(volumeID)
|
2019-01-28 13:59:16 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := volumeIDMutex.UnlockKey(volumeID); err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to unlock mutex volume:%s %v", volumeID, err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
rbdVol := &rbdVolume{}
|
2018-12-19 14:26:16 +00:00
|
|
|
if err := cs.MetadataStore.Get(volumeID, rbdVol); err != nil {
|
2019-02-25 17:09:21 +00:00
|
|
|
if err, ok := err.(*util.CacheEntryNotFound); ok {
|
|
|
|
klog.V(3).Infof("metadata for volume %s not found, assuming the volume to be already deleted (%v)", volumeID, err)
|
2018-10-09 10:08:56 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
2019-02-25 17:09:21 +00:00
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-19 14:26:16 +00:00
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
volName := rbdVol.VolName
|
2018-01-09 18:59:50 +00:00
|
|
|
// Deleting rbd image
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(4).Infof("deleting volume %s", volName)
|
2019-01-17 05:46:32 +00:00
|
|
|
if err := deleteRBDImage(rbdVol, rbdVol.AdminID, req.GetSecrets()); err != nil {
|
2018-10-09 10:08:56 +00:00
|
|
|
// TODO: can we detect "already deleted" situations here and proceed?
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(3).Infof("failed to delete rbd image: %s/%s with error: %v", rbdVol.Pool, volName, err)
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2018-12-19 14:26:16 +00:00
|
|
|
|
|
|
|
if err := cs.MetadataStore.Delete(volumeID); err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
delete(rbdVolumes, volumeID)
|
2018-01-18 19:13:08 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 08:22:52 +00:00
|
|
|
// ListVolumes returns a list of volumes stored in memory
|
|
|
|
func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
|
2019-02-26 13:19:00 +00:00
|
|
|
var startToken int
|
2019-02-18 08:22:52 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_VOLUMES); err != nil {
|
|
|
|
klog.Warningf("invalid list volume req: %v", req)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//validate starting token if present
|
|
|
|
if len(req.GetStartingToken()) > 0 {
|
|
|
|
i, parseErr := strconv.ParseUint(req.StartingToken, 10, 32)
|
|
|
|
if parseErr != nil {
|
|
|
|
return nil, status.Errorf(codes.Aborted, "invalid starting token %s", parseErr.Error())
|
|
|
|
}
|
|
|
|
//check starting Token is greater than list of rbd volumes
|
|
|
|
if len(rbdVolumes) < int(i) {
|
|
|
|
return nil, status.Errorf(codes.Aborted, "invalid starting token %s", parseErr.Error())
|
|
|
|
}
|
2019-02-26 13:19:00 +00:00
|
|
|
startToken = int(i)
|
2019-02-18 08:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var entries []*csi.ListVolumesResponse_Entry
|
|
|
|
|
2019-02-26 13:19:00 +00:00
|
|
|
keys := make([]string, 0)
|
|
|
|
for k := range rbdVolumes {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for index, k := range keys {
|
|
|
|
if index < startToken {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-18 08:22:52 +00:00
|
|
|
entries = append(entries, &csi.ListVolumesResponse_Entry{
|
|
|
|
Volume: &csi.Volume{
|
2019-02-26 13:19:00 +00:00
|
|
|
VolumeId: rbdVolumes[k].VolID,
|
|
|
|
CapacityBytes: rbdVolumes[k].VolSize,
|
|
|
|
VolumeContext: extractStoredVolOpt(rbdVolumes[k]),
|
2019-02-18 08:22:52 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &csi.ListVolumesResponse{
|
|
|
|
Entries: entries,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ValidateVolumeCapabilities checks whether the volume capabilities requested
|
|
|
|
// are supported.
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
|
2019-03-13 18:19:14 +00:00
|
|
|
for _, cap := range req.VolumeCapabilities {
|
|
|
|
if cap.GetAccessMode().GetMode() != csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER {
|
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Message: ""}, nil
|
2018-01-18 19:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 19:18:24 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{
|
|
|
|
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
|
|
|
|
VolumeCapabilities: req.VolumeCapabilities,
|
|
|
|
},
|
|
|
|
}, nil
|
2018-01-18 19:13:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// CreateSnapshot creates the snapshot in backend and stores metadata
|
|
|
|
// in store
|
2019-02-14 21:25:32 +00:00
|
|
|
// nolint: gocyclo
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
|
2018-08-08 05:42:17 +00:00
|
|
|
|
2019-01-28 19:55:10 +00:00
|
|
|
if err := cs.validateSnapshotReq(req); err != nil {
|
|
|
|
return nil, err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2018-10-17 12:52:45 +00:00
|
|
|
snapshotNameMutex.LockKey(req.GetName())
|
2019-01-28 13:59:16 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := snapshotNameMutex.UnlockKey(req.GetName()); err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to unlock mutex snapshot:%s %v", req.GetName(), err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-10-17 12:52:45 +00:00
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
// Need to check for already existing snapshot name, and if found
|
|
|
|
// check for the requested source volume id and already allocated source volume id
|
|
|
|
if exSnap, err := getRBDSnapshotByName(req.GetName()); err == nil {
|
|
|
|
if req.SourceVolumeId == exSnap.SourceVolumeID {
|
2019-02-14 19:31:26 +00:00
|
|
|
if err = storeSnapshotMetadata(exSnap, cs.MetadataStore); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
return &csi.CreateSnapshotResponse{
|
|
|
|
Snapshot: &csi.Snapshot{
|
|
|
|
SizeBytes: exSnap.SizeBytes,
|
2018-11-24 19:18:24 +00:00
|
|
|
SnapshotId: exSnap.SnapID,
|
2018-08-08 05:42:17 +00:00
|
|
|
SourceVolumeId: exSnap.SourceVolumeID,
|
2018-12-05 02:44:04 +00:00
|
|
|
CreationTime: ×tamp.Timestamp{
|
2018-11-24 19:18:24 +00:00
|
|
|
Seconds: exSnap.CreatedAt,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
2018-12-05 02:44:04 +00:00
|
|
|
ReadyToUse: true,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "Snapshot with the same name: %s but with different source volume id already exist", req.GetName())
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rbdSnap, err := getRBDSnapshotOptions(req.GetParameters())
|
|
|
|
if err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generating Snapshot Name and Snapshot ID, as according to CSI spec they MUST be different
|
|
|
|
snapName := req.GetName()
|
|
|
|
uniqueID := uuid.NewUUID().String()
|
|
|
|
rbdVolume, err := getRBDVolumeByID(req.GetSourceVolumeId())
|
|
|
|
if err != nil {
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.NotFound, "Source Volume ID %s cannot found", req.GetSourceVolumeId())
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2018-08-09 13:07:13 +00:00
|
|
|
if !hasSnapshotFeature(rbdVolume.ImageFeatures) {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "volume(%s) has not snapshot feature(layering)", req.GetSourceVolumeId())
|
2018-08-09 13:07:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
rbdSnap.VolName = rbdVolume.VolName
|
|
|
|
rbdSnap.SnapName = snapName
|
2018-08-09 13:06:51 +00:00
|
|
|
snapshotID := "csi-rbd-" + rbdVolume.VolName + "-snap-" + uniqueID
|
2018-08-08 05:42:17 +00:00
|
|
|
rbdSnap.SnapID = snapshotID
|
|
|
|
rbdSnap.SourceVolumeID = req.GetSourceVolumeId()
|
|
|
|
rbdSnap.SizeBytes = rbdVolume.VolSize
|
|
|
|
|
2019-01-28 19:55:10 +00:00
|
|
|
err = cs.doSnapshot(rbdSnap, req.GetSecrets())
|
|
|
|
// if we already have the snapshot, return the snapshot
|
|
|
|
if err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rbdSnap.CreatedAt = ptypes.TimestampNow().GetSeconds()
|
|
|
|
|
2019-02-14 19:31:26 +00:00
|
|
|
rbdSnapshots[snapshotID] = rbdSnap
|
|
|
|
|
|
|
|
if err = storeSnapshotMetadata(rbdSnap, cs.MetadataStore); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.CreateSnapshotResponse{
|
|
|
|
Snapshot: &csi.Snapshot{
|
|
|
|
SizeBytes: rbdSnap.SizeBytes,
|
|
|
|
SnapshotId: snapshotID,
|
|
|
|
SourceVolumeId: req.GetSourceVolumeId(),
|
|
|
|
CreationTime: ×tamp.Timestamp{
|
|
|
|
Seconds: rbdSnap.CreatedAt,
|
|
|
|
},
|
|
|
|
ReadyToUse: true,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:31:26 +00:00
|
|
|
func storeSnapshotMetadata(rbdSnap *rbdSnapshot, cp util.CachePersister) error {
|
|
|
|
if err := cp.Create(rbdSnap.SnapID, rbdSnap); err != nil {
|
|
|
|
klog.Errorf("failed to store metadata for snapshot %s: %v", rbdSnap.SnapID, err)
|
|
|
|
return err
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
2019-02-14 19:31:26 +00:00
|
|
|
|
|
|
|
return nil
|
2019-01-28 19:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ControllerServer) validateSnapshotReq(req *csi.CreateSnapshotRequest) error {
|
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
|
2019-02-08 07:50:21 +00:00
|
|
|
klog.Warningf("invalid create snapshot req: %v", protosanitizer.StripSecrets(req))
|
2019-01-28 19:55:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check sanity of request Snapshot Name, Source Volume Id
|
|
|
|
if len(req.Name) == 0 {
|
|
|
|
return status.Error(codes.InvalidArgument, "Snapshot Name cannot be empty")
|
|
|
|
}
|
|
|
|
if len(req.SourceVolumeId) == 0 {
|
|
|
|
return status.Error(codes.InvalidArgument, "Source Volume ID cannot be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ControllerServer) doSnapshot(rbdSnap *rbdSnapshot, secret map[string]string) error {
|
|
|
|
err := createSnapshot(rbdSnap, rbdSnap.AdminID, secret)
|
2018-08-08 05:42:17 +00:00
|
|
|
// if we already have the snapshot, return the snapshot
|
|
|
|
if err != nil {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
if status.ExitStatus() == int(syscall.EEXIST) {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("Snapshot with the same name: %s, we return this.", rbdSnap.SnapName)
|
2018-08-08 05:42:17 +00:00
|
|
|
} else {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to create snapshot: %v", err)
|
2019-01-28 19:55:10 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to create snapshot: %v", err)
|
2019-01-28 19:55:10 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to create snapshot: %v", err)
|
2019-01-28 19:55:10 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(4).Infof("create snapshot %s", rbdSnap.SnapName)
|
2019-01-28 19:55:10 +00:00
|
|
|
err = protectSnapshot(rbdSnap, rbdSnap.AdminID, secret)
|
2018-08-08 05:42:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-28 19:55:10 +00:00
|
|
|
err = deleteSnapshot(rbdSnap, rbdSnap.AdminID, secret)
|
2018-08-08 05:42:17 +00:00
|
|
|
if err != nil {
|
2019-01-28 19:55:10 +00:00
|
|
|
return fmt.Errorf("snapshot is created but failed to protect and delete snapshot: %v", err)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2019-03-07 12:56:47 +00:00
|
|
|
return errors.New("snapshot is created but failed to protect snapshot")
|
2018-08-09 13:07:06 +00:00
|
|
|
}
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2019-01-28 19:55:10 +00:00
|
|
|
return nil
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// DeleteSnapshot deletes the snapshot in backend and removes the
|
|
|
|
//snapshot metadata from store
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
|
2018-08-08 05:42:17 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
|
2019-02-08 07:50:21 +00:00
|
|
|
klog.Warningf("invalid delete snapshot req: %v", protosanitizer.StripSecrets(req))
|
2018-08-08 05:42:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotID := req.GetSnapshotId()
|
2018-08-09 13:06:51 +00:00
|
|
|
if len(snapshotID) == 0 {
|
2018-08-08 05:42:17 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Snapshot ID cannot be empty")
|
|
|
|
}
|
2018-10-17 12:52:45 +00:00
|
|
|
snapshotIDMutex.LockKey(snapshotID)
|
2019-01-28 13:59:16 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := snapshotIDMutex.UnlockKey(snapshotID); err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("failed to unlock mutex snapshot:%s %v", snapshotID, err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-10-17 12:52:45 +00:00
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
rbdSnap := &rbdSnapshot{}
|
2018-12-19 14:26:16 +00:00
|
|
|
if err := cs.MetadataStore.Get(snapshotID, rbdSnap); err != nil {
|
2019-02-25 17:09:21 +00:00
|
|
|
if err, ok := err.(*util.CacheEntryNotFound); ok {
|
|
|
|
klog.V(3).Infof("metadata for snapshot %s not found, assuming the snapshot to be already deleted (%v)", snapshotID, err)
|
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unprotect snapshot
|
2019-01-17 05:46:32 +00:00
|
|
|
err := unprotectSnapshot(rbdSnap, rbdSnap.AdminID, req.GetSecrets())
|
2018-08-08 05:42:17 +00:00
|
|
|
if err != nil {
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "failed to unprotect snapshot: %s/%s with error: %v", rbdSnap.Pool, rbdSnap.SnapName, err)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting snapshot
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.V(4).Infof("deleting Snaphot %s", rbdSnap.SnapName)
|
2019-01-17 05:46:32 +00:00
|
|
|
if err := deleteSnapshot(rbdSnap, rbdSnap.AdminID, req.GetSecrets()); err != nil {
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "failed to delete snapshot: %s/%s with error: %v", rbdSnap.Pool, rbdSnap.SnapName, err)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 14:26:16 +00:00
|
|
|
if err := cs.MetadataStore.Delete(snapshotID); err != nil {
|
2019-03-07 12:56:47 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(rbdSnapshots, snapshotID)
|
|
|
|
|
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ListSnapshots lists the snapshots in the store
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
|
2018-08-08 05:42:17 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS); err != nil {
|
2019-02-04 13:05:07 +00:00
|
|
|
klog.Warningf("invalid list snapshot req: %v", req)
|
2018-08-08 05:42:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-17 07:34:59 +00:00
|
|
|
sourceVolumeID := req.GetSourceVolumeId()
|
2018-08-08 05:42:17 +00:00
|
|
|
|
|
|
|
// TODO (sngchlko) list with token
|
2018-10-17 12:52:45 +00:00
|
|
|
// TODO (#94) protect concurrent access to global data structures
|
2018-08-08 05:42:17 +00:00
|
|
|
|
|
|
|
// list only a specific snapshot which has snapshot ID
|
2018-08-09 13:06:51 +00:00
|
|
|
if snapshotID := req.GetSnapshotId(); len(snapshotID) != 0 {
|
2018-08-08 05:42:17 +00:00
|
|
|
if rbdSnap, ok := rbdSnapshots[snapshotID]; ok {
|
|
|
|
// if source volume ID also set, check source volume id on the cache.
|
2019-01-17 07:34:59 +00:00
|
|
|
if len(sourceVolumeID) != 0 && rbdSnap.SourceVolumeID != sourceVolumeID {
|
|
|
|
return nil, status.Errorf(codes.Unknown, "Requested Source Volume ID %s is different from %s", sourceVolumeID, rbdSnap.SourceVolumeID)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
return &csi.ListSnapshotsResponse{
|
|
|
|
Entries: []*csi.ListSnapshotsResponse_Entry{
|
|
|
|
{
|
|
|
|
Snapshot: &csi.Snapshot{
|
|
|
|
SizeBytes: rbdSnap.SizeBytes,
|
2018-11-24 19:18:24 +00:00
|
|
|
SnapshotId: rbdSnap.SnapID,
|
2018-08-08 05:42:17 +00:00
|
|
|
SourceVolumeId: rbdSnap.SourceVolumeID,
|
2018-12-05 02:44:04 +00:00
|
|
|
CreationTime: ×tamp.Timestamp{
|
2018-11-24 19:18:24 +00:00
|
|
|
Seconds: rbdSnap.CreatedAt,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
2018-12-05 02:44:04 +00:00
|
|
|
ReadyToUse: true,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2019-01-16 13:13:38 +00:00
|
|
|
return nil, status.Errorf(codes.NotFound, "Snapshot ID %s cannot found", snapshotID)
|
2019-01-16 13:03:38 +00:00
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entries := []*csi.ListSnapshotsResponse_Entry{}
|
|
|
|
for _, rbdSnap := range rbdSnapshots {
|
|
|
|
// if source volume ID also set, check source volume id on the cache.
|
2019-01-17 07:34:59 +00:00
|
|
|
if len(sourceVolumeID) != 0 && rbdSnap.SourceVolumeID != sourceVolumeID {
|
2018-08-08 05:42:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
entries = append(entries, &csi.ListSnapshotsResponse_Entry{
|
|
|
|
Snapshot: &csi.Snapshot{
|
|
|
|
SizeBytes: rbdSnap.SizeBytes,
|
2018-11-24 19:18:24 +00:00
|
|
|
SnapshotId: rbdSnap.SnapID,
|
2018-08-08 05:42:17 +00:00
|
|
|
SourceVolumeId: rbdSnap.SourceVolumeID,
|
2018-12-05 02:44:04 +00:00
|
|
|
CreationTime: ×tamp.Timestamp{
|
2018-11-24 19:18:24 +00:00
|
|
|
Seconds: rbdSnap.CreatedAt,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
2018-12-05 02:44:04 +00:00
|
|
|
ReadyToUse: true,
|
2018-08-08 05:42:17 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.ListSnapshotsResponse{
|
|
|
|
Entries: entries,
|
|
|
|
}, nil
|
|
|
|
}
|