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"
|
2020-06-25 06:41:35 +00:00
|
|
|
"errors"
|
2019-08-24 09:14:15 +00:00
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
csicommon "github.com/ceph/ceph-csi/internal/csi-common"
|
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2020-04-15 03:38:16 +00:00
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
"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"
|
2020-07-09 05:14:10 +00:00
|
|
|
klog "k8s.io/klog/v2"
|
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
|
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
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +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())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// 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 {
|
2020-03-23 02:15:35 +00:00
|
|
|
klog.Errorf(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), requestName)
|
2019-09-12 04:53:37 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, requestName)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(requestName)
|
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
volOptions, err := newVolumeOptions(ctx, requestName, req, 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-09-25 08:35:33 +00:00
|
|
|
if req.GetCapacityRange() != nil {
|
|
|
|
volOptions.Size = util.RoundOffBytes(req.GetCapacityRange().GetRequiredBytes())
|
|
|
|
}
|
|
|
|
// TODO need to add check for 0 volume size
|
|
|
|
|
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())
|
|
|
|
}
|
2019-09-25 08:35:33 +00:00
|
|
|
|
|
|
|
// TODO return error message if requested vol size greater than found volume return error
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if vID != nil {
|
2020-04-18 08:57:05 +00:00
|
|
|
volumeContext := req.GetParameters()
|
|
|
|
volumeContext["subvolumeName"] = vID.FsSubvolName
|
2020-01-24 16:26:56 +00:00
|
|
|
volume := &csi.Volume{
|
|
|
|
VolumeId: vID.VolumeID,
|
|
|
|
CapacityBytes: volOptions.Size,
|
2020-04-18 08:57:05 +00:00
|
|
|
VolumeContext: volumeContext,
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
if volOptions.Topology != nil {
|
|
|
|
volume.AccessibleTopology =
|
|
|
|
[]*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: volOptions.Topology,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &csi.CreateVolumeResponse{Volume: volume}, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
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
|
|
|
|
2020-07-09 14:48:24 +00:00
|
|
|
util.DebugLog(ctx, "cephfs: successfully created backing volume named %s for request name %s",
|
2019-05-28 19:03:18 +00:00
|
|
|
vID.FsSubvolName, requestName)
|
2020-04-18 08:57:05 +00:00
|
|
|
volumeContext := req.GetParameters()
|
|
|
|
volumeContext["subvolumeName"] = vID.FsSubvolName
|
2020-01-24 16:26:56 +00:00
|
|
|
volume := &csi.Volume{
|
|
|
|
VolumeId: vID.VolumeID,
|
|
|
|
CapacityBytes: volOptions.Size,
|
2020-04-18 08:57:05 +00:00
|
|
|
VolumeContext: volumeContext,
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
if volOptions.Topology != nil {
|
|
|
|
volume.AccessibleTopology =
|
|
|
|
[]*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: volOptions.Topology,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &csi.CreateVolumeResponse{Volume: volume}, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// DeleteVolume deletes the volume in backend and its reservation.
|
2019-05-28 19:03:18 +00:00
|
|
|
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 {
|
2020-03-23 02:15:35 +00:00
|
|
|
klog.Errorf(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), volID)
|
2019-09-12 04:53:37 +00:00
|
|
|
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 {
|
2020-01-31 08:49:11 +00:00
|
|
|
// if error is ErrPoolNotFound, the pool is already deleted we dont
|
|
|
|
// need to worry about deleting subvolume or omap data, return success
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, util.ErrPoolNotFound) {
|
2020-01-31 08:49:11 +00:00
|
|
|
klog.Warningf(util.Log(ctx, "failed to get backend volume for %s: %v"), string(volID), err)
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
// 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
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, util.ErrKeyNotFound) {
|
2019-05-28 19:03:18 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-23 02:48:46 +00:00
|
|
|
// All errors other than ErrVolumeNotFound should return an error back to the caller
|
2020-07-10 00:14:39 +00:00
|
|
|
if !errors.Is(err, ErrVolumeNotFound) {
|
2020-01-23 02:48:46 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If error is ErrImageNotFound then we failed to find the subvolume, but found the imageOMap
|
|
|
|
// to lead us to the image, hence the imageOMap needs to be garbage collected, by calling
|
|
|
|
// unreserve 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(volOptions.RequestName)
|
|
|
|
|
|
|
|
if err = undoVolReservation(ctx, volOptions, *vID, secrets); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-01-23 02:48:46 +00:00
|
|
|
// All errors other than ErrVolumeNotFound should return an error back to the caller
|
2020-07-10 00:14:39 +00:00
|
|
|
if !errors.Is(err, ErrVolumeNotFound) {
|
2020-01-23 02:48:46 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:48:24 +00:00
|
|
|
util.DebugLog(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
|
|
|
}
|
2019-10-07 06:41:33 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// ControllerExpandVolume expands CephFS Volumes on demand based on resizer request.
|
2019-10-07 06:41:33 +00:00
|
|
|
func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
|
|
|
|
if err := cs.validateExpandVolumeRequest(req); err != nil {
|
2019-12-16 09:24:21 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "ControllerExpandVolumeRequest validation failed: %v"), err)
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volID := req.GetVolumeId()
|
|
|
|
secret := req.GetSecrets()
|
|
|
|
|
2019-11-25 11:09:24 +00:00
|
|
|
// lock out parallel delete operations
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(volID); !acquired {
|
2020-03-23 02:15:35 +00:00
|
|
|
klog.Errorf(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), volID)
|
2019-11-25 11:09:24 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volID)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(volID)
|
|
|
|
|
2019-10-07 06:41:33 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
|
|
|
volOptions, volIdentifier, err := newVolumeOptionsFromVolID(ctx, volID, nil, secret)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-16 09:24:21 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "validation and extraction of volume options failed: %v"), err)
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-11-26 08:29:28 +00:00
|
|
|
RoundOffSize := util.RoundOffBytes(req.GetCapacityRange().GetRequiredBytes())
|
|
|
|
|
2020-05-06 13:47:46 +00:00
|
|
|
if err = resizeVolume(ctx, volOptions, cr, volumeID(volIdentifier.FsSubvolName), RoundOffSize); err != nil {
|
2019-12-16 09:24:21 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to expand volume %s: %v"), volumeID(volIdentifier.FsSubvolName), err)
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.ControllerExpandVolumeResponse{
|
2019-11-26 08:29:28 +00:00
|
|
|
CapacityBytes: RoundOffSize,
|
2019-10-07 06:41:33 +00:00
|
|
|
NodeExpansionRequired: false,
|
|
|
|
}, nil
|
|
|
|
}
|