2018-03-05 11:59:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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 cephfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2018-11-24 18:48:36 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-03-05 11:59:47 +00:00
|
|
|
"github.com/kubernetes-csi/drivers/pkg/csi-common"
|
2018-12-19 14:26:16 +00:00
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/pkg/util"
|
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-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-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-02-13 12:57:16 +00:00
|
|
|
|
2019-01-21 14:21:03 +00:00
|
|
|
secret := req.GetSecrets()
|
|
|
|
volOptions, err := newVolumeOptions(req.GetParameters(), secret)
|
2018-03-05 11:59:47 +00:00
|
|
|
if err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("validation 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-01-17 05:46:32 +00:00
|
|
|
volID := makeVolumeID(req.GetName())
|
2018-06-12 15:07:20 +00:00
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
// Create a volume in case the user didn't provide one
|
|
|
|
|
|
|
|
if volOptions.ProvisionVolume {
|
2018-07-28 08:24:07 +00:00
|
|
|
// Admin credentials are required
|
2019-01-18 15:27:48 +00:00
|
|
|
cr, err := getAdminCredentials(secret)
|
2018-04-13 12:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
if err = createVolume(volOptions, cr, volID, req.GetCapacityRange().GetRequiredBytes()); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("failed to create volume %s: %v", req.GetName(), err)
|
2018-04-13 12:54:40 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
if _, err = createCephUser(volOptions, cr, volID); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("failed to create ceph user for volume %s: %v", req.GetName(), err)
|
2018-08-28 08:21:11 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Infof("cephfs: successfully created volume %s", volID)
|
2018-04-13 12:54:40 +00:00
|
|
|
} else {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Infof("cephfs: volume %s is provisioned statically", volID)
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
ce := &controllerCacheEntry{VolOptions: *volOptions, VolumeID: volID}
|
|
|
|
if err := cs.MetadataStore.Create(string(volID), ce); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("failed to store a cache entry for volume %s: %v", volID, err)
|
2018-07-28 08:24:07 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
return &csi.CreateVolumeResponse{
|
2018-03-13 09:25:50 +00:00
|
|
|
Volume: &csi.Volume{
|
2019-01-17 05:46:32 +00:00
|
|
|
VolumeId: string(volID),
|
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-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) {
|
2019-01-17 07:59:29 +00:00
|
|
|
if err := cs.validateDeleteVolumeRequest(); err != nil {
|
2019-02-04 13:04:11 +00:00
|
|
|
klog.Errorf("DeleteVolumeRequest validation failed: %v", err)
|
2018-03-05 11:59:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
var (
|
2019-02-13 12:57:16 +00:00
|
|
|
volID = volumeID(req.GetVolumeId())
|
|
|
|
secrets = req.GetSecrets()
|
|
|
|
err error
|
2018-04-13 12:54:40 +00:00
|
|
|
)
|
|
|
|
|
2018-12-19 14:26:16 +00:00
|
|
|
ce := &controllerCacheEntry{}
|
2019-01-17 06:49:35 +00:00
|
|
|
if err = cs.MetadataStore.Get(string(volID), ce); err != 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-02-13 12:57:16 +00:00
|
|
|
if mon, secretsErr := getMonValFromSecret(secrets); secretsErr == nil && len(mon) > 0 {
|
|
|
|
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-02-13 12:57:16 +00:00
|
|
|
cr, err := 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
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
if err = purgeVolume(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-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
|
|
|
}
|