2018-03-05 11:59:47 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-03-05 11:59:47 +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 cephfs
|
|
|
|
|
|
|
|
import (
|
2019-08-24 09:14:15 +00:00
|
|
|
"context"
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
csicommon "github.com/ceph/ceph-csi/pkg/csi-common"
|
|
|
|
"github.com/ceph/ceph-csi/pkg/util"
|
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-03-05 11:59:47 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-02-04 13:04:11 +00:00
|
|
|
"k8s.io/klog"
|
2018-03-05 11:59:47 +00:00
|
|
|
)
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ControllerServer struct of CEPH CSI driver with supported methods of CSI
|
|
|
|
// controller server spec.
|
2019-01-17 07:51:06 +00:00
|
|
|
type ControllerServer struct {
|
2018-03-05 11:59:47 +00:00
|
|
|
*csicommon.DefaultControllerServer
|
2018-12-19 14:26:16 +00:00
|
|
|
MetadataStore util.CachePersister
|
2019-09-12 04:53:37 +00:00
|
|
|
// A map storing all volumes with ongoing operations so that additional operations
|
|
|
|
// for that same volume (as defined by VolumeID/volume name) return an Aborted error
|
|
|
|
VolumeLocks *util.VolumeLocks
|
2018-12-19 14:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type controllerCacheEntry struct {
|
|
|
|
VolOptions volumeOptions
|
|
|
|
VolumeID volumeID
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 07:35:53 +00:00
|
|
|
// createBackingVolume creates the backing subvolume and on any error cleans up any created entities
|
2019-08-22 17:19:06 +00:00
|
|
|
func (cs *ControllerServer) createBackingVolume(ctx context.Context, volOptions *volumeOptions, vID *volumeIdentifier, secret map[string]string) error {
|
2019-06-25 19:29:17 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
if err = createVolume(ctx, volOptions, cr, volumeID(vID.FsSubvolName), volOptions.Size); err != nil {
|
|
|
|
klog.Errorf(util.Log(ctx, "failed to create volume %s: %v"), volOptions.RequestName, err)
|
2019-05-28 19:03:18 +00:00
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
if errDefer := purgeVolume(ctx, volumeID(vID.FsSubvolName), cr, volOptions); errDefer != nil {
|
|
|
|
klog.Warningf(util.Log(ctx, "failed purging volume: %s (%s)"), volOptions.RequestName, errDefer)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVolume creates a reservation and the volume in backend, if it is not already present
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
2018-03-20 15:15:19 +00:00
|
|
|
if err := cs.validateCreateVolumeRequest(req); err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "CreateVolumeRequest validation failed: %v"), err)
|
2018-03-05 11:59:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 12:57:16 +00:00
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
// Configuration
|
2019-01-21 14:21:03 +00:00
|
|
|
secret := req.GetSecrets()
|
2019-05-28 19:03:18 +00:00
|
|
|
requestName := req.GetName()
|
2019-09-12 04:53:37 +00:00
|
|
|
|
|
|
|
// Existence and conflict checks
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(requestName); !acquired {
|
|
|
|
klog.Infof(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), requestName)
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, requestName)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(requestName)
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
volOptions, err := newVolumeOptions(ctx, requestName, req.GetCapacityRange().GetRequiredBytes(),
|
2019-05-28 19:03:18 +00:00
|
|
|
req.GetParameters(), secret)
|
2018-03-05 11:59:47 +00:00
|
|
|
if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "validation and extraction of volume options failed: %v"), err)
|
2018-03-20 15:15:19 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
vID, err := checkVolExists(ctx, volOptions, secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
if vID != nil {
|
|
|
|
return &csi.CreateVolumeResponse{
|
|
|
|
Volume: &csi.Volume{
|
|
|
|
VolumeId: vID.VolumeID,
|
|
|
|
CapacityBytes: volOptions.Size,
|
|
|
|
VolumeContext: req.GetParameters(),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Reservation
|
2019-08-22 17:19:06 +00:00
|
|
|
vID, err = reserveVol(ctx, volOptions, secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer func() {
|
2018-04-13 12:54:40 +00:00
|
|
|
if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
errDefer := undoVolReservation(ctx, volOptions, *vID, secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if errDefer != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Warningf(util.Log(ctx, "failed undoing reservation of volume: %s (%s)"),
|
2019-05-28 19:03:18 +00:00
|
|
|
requestName, errDefer)
|
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
}()
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Create a volume
|
2019-08-22 17:19:06 +00:00
|
|
|
err = cs.createBackingVolume(ctx, volOptions, vID, secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Infof(util.Log(ctx, "cephfs: successfully created backing volume named %s for request name %s"),
|
2019-05-28 19:03:18 +00:00
|
|
|
vID.FsSubvolName, requestName)
|
2018-07-28 08:24:07 +00:00
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
return &csi.CreateVolumeResponse{
|
2018-03-13 09:25:50 +00:00
|
|
|
Volume: &csi.Volume{
|
2019-05-28 19:03:18 +00:00
|
|
|
VolumeId: vID.VolumeID,
|
2018-12-01 09:39:09 +00:00
|
|
|
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
|
2018-11-24 18:48:36 +00:00
|
|
|
VolumeContext: req.GetParameters(),
|
2018-03-05 11:59:47 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// deleteVolumeDeprecated is used to delete volumes created using version 1.0.0 of the plugin,
|
|
|
|
// that have state information stored in files or kubernetes config maps
|
2019-08-22 17:19:06 +00:00
|
|
|
func (cs *ControllerServer) deleteVolumeDeprecated(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
|
2018-04-13 12:54:40 +00:00
|
|
|
var (
|
2019-02-13 12:57:16 +00:00
|
|
|
volID = volumeID(req.GetVolumeId())
|
|
|
|
secrets = req.GetSecrets()
|
2018-04-13 12:54:40 +00:00
|
|
|
)
|
|
|
|
|
2018-12-19 14:26:16 +00:00
|
|
|
ce := &controllerCacheEntry{}
|
2019-02-25 17:07:28 +00:00
|
|
|
if err := cs.MetadataStore.Get(string(volID), ce); err != nil {
|
|
|
|
if err, ok := err.(*util.CacheEntryNotFound); ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Infof(util.Log(ctx, "cephfs: metadata for volume %s not found, assuming the volume to be already deleted (%v)"), volID, err)
|
2019-02-25 17:07:28 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 14:26:16 +00:00
|
|
|
if !ce.VolOptions.ProvisionVolume {
|
2018-06-13 14:23:13 +00:00
|
|
|
// DeleteVolume() is forbidden for statically provisioned volumes!
|
2018-07-28 08:24:07 +00:00
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Warningf(util.Log(ctx, "volume %s is provisioned statically, aborting delete"), volID)
|
2018-06-13 14:23:13 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
2019-02-13 12:57:16 +00:00
|
|
|
|
2019-01-18 15:27:48 +00:00
|
|
|
// mons may have changed since create volume,
|
|
|
|
// retrieve the latest mons and override old mons
|
2019-06-01 21:26:42 +00:00
|
|
|
if mon, secretsErr := util.GetMonValFromSecret(secrets); secretsErr == nil && len(mon) > 0 {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Infof(util.Log(ctx, "overriding monitors [%q] with [%q] for volume %s"), ce.VolOptions.Monitors, mon, volID)
|
2019-01-18 15:27:48 +00:00
|
|
|
ce.VolOptions.Monitors = mon
|
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
// Deleting a volume requires admin credentials
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secrets)
|
2018-06-13 14:23:13 +00:00
|
|
|
if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to retrieve admin credentials: %v"), err)
|
2018-06-13 14:23:13 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(string(volID)); !acquired {
|
|
|
|
klog.Infof(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), volID)
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, string(volID))
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(string(volID))
|
2019-02-26 10:06:25 +00:00
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
if err = purgeVolumeDeprecated(ctx, volID, cr, &ce.VolOptions); err != nil {
|
|
|
|
klog.Errorf(util.Log(ctx, "failed to delete volume %s: %v"), volID, err)
|
2018-04-13 12:54:40 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
if err = deleteCephUserDeprecated(ctx, &ce.VolOptions, cr, volID); err != nil {
|
|
|
|
klog.Errorf(util.Log(ctx, "failed to delete ceph user for volume %s: %v"), volID, err)
|
2018-08-28 08:21:11 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-01-17 06:49:35 +00:00
|
|
|
if err = cs.MetadataStore.Delete(string(volID)); err != nil {
|
2018-12-19 14:26:16 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Infof(util.Log(ctx, "cephfs: successfully deleted volume %s"), volID)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// DeleteVolume deletes the volume in backend and its reservation
|
|
|
|
func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
|
|
|
|
if err := cs.validateDeleteVolumeRequest(); err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "DeleteVolumeRequest validation failed: %v"), err)
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volID := volumeID(req.GetVolumeId())
|
|
|
|
secrets := req.GetSecrets()
|
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
// lock out parallel delete operations
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(string(volID)); !acquired {
|
|
|
|
klog.Infof(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), volID)
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, string(volID))
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(string(volID))
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Find the volume using the provided VolumeID
|
2019-08-22 17:19:06 +00:00
|
|
|
volOptions, vID, err := newVolumeOptionsFromVolID(ctx, string(volID), nil, secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
// if error is ErrKeyNotFound, then a previous attempt at deletion was complete
|
|
|
|
// or partially complete (subvolume and imageOMap are garbage collected already), hence
|
|
|
|
// return success as deletion is complete
|
|
|
|
if _, ok := err.(util.ErrKeyNotFound); ok {
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrInvalidVolID may mean this is an 1.0.0 version volume
|
|
|
|
if _, ok := err.(ErrInvalidVolID); ok && cs.MetadataStore != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
return cs.deleteVolumeDeprecated(ctx, req)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
// lock out parallel delete and create requests against the same volume name as we
|
|
|
|
// cleanup the subvolume and associated omaps for the same
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(volOptions.RequestName); !acquired {
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volOptions.RequestName)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(string(volID))
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Deleting a volume requires admin credentials
|
2019-06-25 19:29:17 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to retrieve admin credentials: %v"), err)
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
if err = purgeVolume(ctx, volumeID(vID.FsSubvolName), cr, volOptions); err != nil {
|
|
|
|
klog.Errorf(util.Log(ctx, "failed to delete volume %s: %v"), volID, err)
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
if err := undoVolReservation(ctx, volOptions, *vID, secrets); err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Infof(util.Log(ctx, "cephfs: successfully deleted volume %s"), volID)
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
return &csi.DeleteVolumeResponse{}, 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(
|
2018-04-13 12:54:40 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
|
2018-07-10 16:48:55 +00:00
|
|
|
// Cephfs doesn't support Block volume
|
|
|
|
for _, cap := range req.VolumeCapabilities {
|
|
|
|
if cap.GetBlock() != nil {
|
2018-11-24 18:48:36 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Message: ""}, nil
|
2018-07-10 16:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 18:48:36 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{
|
|
|
|
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
|
|
|
|
VolumeCapabilities: req.VolumeCapabilities,
|
|
|
|
},
|
|
|
|
}, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|