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-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
|
|
|
"golang.org/x/net/context"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
type controllerCacheEntry struct {
|
|
|
|
VolOptions volumeOptions
|
|
|
|
VolumeID volumeID
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:25 +00:00
|
|
|
var (
|
Move locks to more granular locking than CPU count based
As detailed in issue #279, current lock scheme has hash
buckets that are count of CPUs. This causes a lot of contention
when parallel requests are made to the CSI plugin. To reduce
lock contention, this commit introduces granular locks per
identifier.
The commit also changes the timeout for gRPC requests to Create
and Delete volumes, as the current timeout is 10s (kubernetes
documentation says 15s but code defaults are 10s). A virtual
setup takes about 12-15s to complete a request at times, that leads
to unwanted retries of the same request, hence the increased
timeout to enable operation completion with minimal retries.
Tests to create PVCs before and after these changes look like so,
Before:
Default master code + sidecar provisioner --timeout option set
to 30 seconds
20 PVCs
Creation: 3 runs, 396/391/400 seconds
Deletion: 3 runs, 218/271/118 seconds
- Once was stalled for more than 8 minutes and cancelled the run
After:
Current commit + sidecar provisioner --timeout option set to 30 sec
20 PVCs
Creation: 3 runs, 42/59/65 seconds
Deletion: 3 runs, 32/32/31 seconds
Fixes: #279
Signed-off-by: ShyamsundarR <srangana@redhat.com>
2019-06-22 16:43:28 +00:00
|
|
|
volumeIDLocker = util.NewIDLocker()
|
|
|
|
volumeNameLocker = util.NewIDLocker()
|
2019-02-26 10:06:25 +00:00
|
|
|
)
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// createBackingVolume creates the backing subvolume and user/key for the given volOptions and vID,
|
|
|
|
// and on any error cleans up any created entities
|
|
|
|
func (cs *ControllerServer) createBackingVolume(volOptions *volumeOptions, vID *volumeIdentifier, secret map[string]string) error {
|
2019-06-01 21:26:42 +00:00
|
|
|
cr, err := util.GetAdminCredentials(secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = createVolume(volOptions, cr, volumeID(vID.FsSubvolName), volOptions.Size); err != nil {
|
|
|
|
klog.Errorf("failed to create volume %s: %v", volOptions.RequestName, err)
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if errDefer := purgeVolume(volumeID(vID.FsSubvolName), cr, volOptions); errDefer != nil {
|
|
|
|
klog.Warningf("failed purging volume: %s (%s)", volOptions.RequestName, errDefer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if _, err = createCephUser(volOptions, cr, volumeID(vID.FsSubvolName)); err != nil {
|
|
|
|
klog.Errorf("failed to create ceph user for volume %s: %v", volOptions.RequestName, err)
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("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()
|
|
|
|
volOptions, err := newVolumeOptions(requestName, req.GetCapacityRange().GetRequiredBytes(),
|
|
|
|
req.GetParameters(), secret)
|
2018-03-05 11:59:47 +00:00
|
|
|
if err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
klog.Errorf("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-05-28 19:03:18 +00:00
|
|
|
// Existence and conflict checks
|
Move locks to more granular locking than CPU count based
As detailed in issue #279, current lock scheme has hash
buckets that are count of CPUs. This causes a lot of contention
when parallel requests are made to the CSI plugin. To reduce
lock contention, this commit introduces granular locks per
identifier.
The commit also changes the timeout for gRPC requests to Create
and Delete volumes, as the current timeout is 10s (kubernetes
documentation says 15s but code defaults are 10s). A virtual
setup takes about 12-15s to complete a request at times, that leads
to unwanted retries of the same request, hence the increased
timeout to enable operation completion with minimal retries.
Tests to create PVCs before and after these changes look like so,
Before:
Default master code + sidecar provisioner --timeout option set
to 30 seconds
20 PVCs
Creation: 3 runs, 396/391/400 seconds
Deletion: 3 runs, 218/271/118 seconds
- Once was stalled for more than 8 minutes and cancelled the run
After:
Current commit + sidecar provisioner --timeout option set to 30 sec
20 PVCs
Creation: 3 runs, 42/59/65 seconds
Deletion: 3 runs, 32/32/31 seconds
Fixes: #279
Signed-off-by: ShyamsundarR <srangana@redhat.com>
2019-06-22 16:43:28 +00:00
|
|
|
idLk := volumeNameLocker.Lock(requestName)
|
|
|
|
defer volumeNameLocker.Unlock(idLk, requestName)
|
2019-02-26 10:06:25 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
vID, err := checkVolExists(volOptions, secret)
|
|
|
|
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
|
|
|
|
vID, err = reserveVol(volOptions, secret)
|
|
|
|
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-05-28 19:03:18 +00:00
|
|
|
errDefer := undoVolReservation(volOptions, *vID, secret)
|
|
|
|
if errDefer != nil {
|
|
|
|
klog.Warningf("failed undoing reservation of volume: %s (%s)",
|
|
|
|
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
|
|
|
|
err = cs.createBackingVolume(volOptions, vID, secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
klog.Infof("cephfs: successfully created backing volume named %s for request name %s",
|
|
|
|
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
|
|
|
|
func (cs *ControllerServer) deleteVolumeDeprecated(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 {
|
|
|
|
klog.Infof("cephfs: metadata for volume %s not found, assuming the volume to be already deleted (%v)", volID, err)
|
|
|
|
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-02-04 13:04:11 +00:00
|
|
|
klog.Warningf("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-02-13 12:57:16 +00:00
|
|
|
klog.Infof("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-01 21:26:42 +00:00
|
|
|
cr, err := util.GetAdminCredentials(secrets)
|
2018-06-13 14:23:13 +00:00
|
|
|
if err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("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
|
|
|
}
|
|
|
|
|
Move locks to more granular locking than CPU count based
As detailed in issue #279, current lock scheme has hash
buckets that are count of CPUs. This causes a lot of contention
when parallel requests are made to the CSI plugin. To reduce
lock contention, this commit introduces granular locks per
identifier.
The commit also changes the timeout for gRPC requests to Create
and Delete volumes, as the current timeout is 10s (kubernetes
documentation says 15s but code defaults are 10s). A virtual
setup takes about 12-15s to complete a request at times, that leads
to unwanted retries of the same request, hence the increased
timeout to enable operation completion with minimal retries.
Tests to create PVCs before and after these changes look like so,
Before:
Default master code + sidecar provisioner --timeout option set
to 30 seconds
20 PVCs
Creation: 3 runs, 396/391/400 seconds
Deletion: 3 runs, 218/271/118 seconds
- Once was stalled for more than 8 minutes and cancelled the run
After:
Current commit + sidecar provisioner --timeout option set to 30 sec
20 PVCs
Creation: 3 runs, 42/59/65 seconds
Deletion: 3 runs, 32/32/31 seconds
Fixes: #279
Signed-off-by: ShyamsundarR <srangana@redhat.com>
2019-06-22 16:43:28 +00:00
|
|
|
idLk := volumeIDLocker.Lock(string(volID))
|
|
|
|
defer volumeIDLocker.Unlock(idLk, string(volID))
|
2019-02-26 10:06:25 +00:00
|
|
|
|
2019-07-10 10:50:04 +00:00
|
|
|
if err = purgeVolumeDeprecated(volID, cr, &ce.VolOptions); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("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-02-13 12:57:16 +00:00
|
|
|
if err = deleteCephUser(&ce.VolOptions, cr, volID); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("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-02-04 13:04:11 +00:00
|
|
|
klog.Infof("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 {
|
|
|
|
klog.Errorf("DeleteVolumeRequest validation failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volID := volumeID(req.GetVolumeId())
|
|
|
|
secrets := req.GetSecrets()
|
|
|
|
|
|
|
|
// Find the volume using the provided VolumeID
|
|
|
|
volOptions, vID, err := newVolumeOptionsFromVolID(string(volID), nil, secrets)
|
|
|
|
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 {
|
|
|
|
return cs.deleteVolumeDeprecated(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting a volume requires admin credentials
|
2019-06-01 21:26:42 +00:00
|
|
|
cr, err := util.GetAdminCredentials(secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to retrieve admin credentials: %v", err)
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock out parallel delete and create requests against the same volume name as we
|
|
|
|
// cleanup the subvolume and associated omaps for the same
|
Move locks to more granular locking than CPU count based
As detailed in issue #279, current lock scheme has hash
buckets that are count of CPUs. This causes a lot of contention
when parallel requests are made to the CSI plugin. To reduce
lock contention, this commit introduces granular locks per
identifier.
The commit also changes the timeout for gRPC requests to Create
and Delete volumes, as the current timeout is 10s (kubernetes
documentation says 15s but code defaults are 10s). A virtual
setup takes about 12-15s to complete a request at times, that leads
to unwanted retries of the same request, hence the increased
timeout to enable operation completion with minimal retries.
Tests to create PVCs before and after these changes look like so,
Before:
Default master code + sidecar provisioner --timeout option set
to 30 seconds
20 PVCs
Creation: 3 runs, 396/391/400 seconds
Deletion: 3 runs, 218/271/118 seconds
- Once was stalled for more than 8 minutes and cancelled the run
After:
Current commit + sidecar provisioner --timeout option set to 30 sec
20 PVCs
Creation: 3 runs, 42/59/65 seconds
Deletion: 3 runs, 32/32/31 seconds
Fixes: #279
Signed-off-by: ShyamsundarR <srangana@redhat.com>
2019-06-22 16:43:28 +00:00
|
|
|
idLk := volumeNameLocker.Lock(volOptions.RequestName)
|
|
|
|
defer volumeNameLocker.Unlock(idLk, volOptions.RequestName)
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if err = purgeVolume(volumeID(vID.FsSubvolName), cr, volOptions); err != nil {
|
|
|
|
klog.Errorf("failed to delete volume %s: %v", volID, err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = deleteCephUser(volOptions, cr, volumeID(vID.FsSubvolName)); err != nil {
|
|
|
|
klog.Errorf("failed to delete ceph user for volume %s: %v", volID, err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := undoVolReservation(volOptions, *vID, secrets); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
klog.Infof("cephfs: successfully deleted volume %s", volID)
|
|
|
|
|
|
|
|
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
|
|
|
}
|