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 (
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2018-03-20 15:15:19 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi/v0"
|
2018-03-05 11:59:47 +00:00
|
|
|
"github.com/kubernetes-csi/drivers/pkg/csi-common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type controllerServer struct {
|
|
|
|
*csicommon.DefaultControllerServer
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
oneGB = 1073741824
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
glog.Errorf("CreateVolumeRequest validation failed: %v", err)
|
2018-03-05 11:59:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
// Configuration
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
volOptions, err := newVolumeOptions(req.GetParameters())
|
|
|
|
if err != nil {
|
2018-04-13 12:54:40 +00:00
|
|
|
glog.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
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
volId := newVolumeID()
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
conf := cephConfigData{Monitors: volOptions.Monitors, VolumeID: volId}
|
2018-06-12 15:07:20 +00:00
|
|
|
if err = conf.writeToFile(); err != nil {
|
2018-07-28 08:24:07 +00:00
|
|
|
glog.Errorf("failed to write ceph config file to %s: %v", getCephConfPath(volId), err)
|
2018-06-12 15:07:20 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
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
|
2018-04-13 12:54:40 +00:00
|
|
|
|
|
|
|
cr, err := getAdminCredentials(req.GetControllerCreateSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
if err = storeCephCredentials(volId, cr); err != nil {
|
2018-04-13 12:54:40 +00:00
|
|
|
glog.Errorf("failed to store admin credentials for '%s': %v", cr.id, err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
if err = createVolume(volOptions, cr, volId, req.GetCapacityRange().GetRequiredBytes()); err != nil {
|
|
|
|
glog.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())
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:21:11 +00:00
|
|
|
if _, err = createCephUser(volOptions, cr, volId); err != nil {
|
|
|
|
glog.Errorf("failed to create ceph user for volume %s: %v", req.GetName(), err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
glog.Infof("cephfs: successfuly created volume %s", volId)
|
2018-04-13 12:54:40 +00:00
|
|
|
} else {
|
2018-07-28 08:24:07 +00:00
|
|
|
glog.Infof("cephfs: volume %s is provisioned statically", volId)
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
sz := req.GetCapacityRange().GetRequiredBytes()
|
|
|
|
if sz == 0 {
|
|
|
|
sz = oneGB
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
if err = ctrCache.insert(&controllerCacheEntry{VolOptions: *volOptions, VolumeID: volId}); err != nil {
|
|
|
|
glog.Errorf("failed to store a cache entry for volume %s: %v", volId, err)
|
|
|
|
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{
|
2018-07-28 08:24:07 +00:00
|
|
|
Id: string(volId),
|
2018-06-13 14:23:13 +00:00
|
|
|
CapacityBytes: sz,
|
2018-03-20 15:15:19 +00:00
|
|
|
Attributes: req.GetParameters(),
|
2018-03-05 11:59:47 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
|
2018-03-20 15:15:19 +00:00
|
|
|
if err := cs.validateDeleteVolumeRequest(req); err != nil {
|
|
|
|
glog.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 (
|
2018-07-28 08:24:07 +00:00
|
|
|
volId = volumeID(req.GetVolumeId())
|
|
|
|
err error
|
2018-04-13 12:54:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Load volume info from cache
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
ent, err := ctrCache.pop(volId)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
if !ent.VolOptions.ProvisionVolume {
|
|
|
|
// DeleteVolume() is forbidden for statically provisioned volumes!
|
2018-07-28 08:24:07 +00:00
|
|
|
|
|
|
|
glog.Warningf("volume %s is provisioned statically, aborting delete", volId)
|
2018-06-13 14:23:13 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// Reinsert cache entry for retry
|
|
|
|
if insErr := ctrCache.insert(ent); insErr != nil {
|
|
|
|
glog.Errorf("failed to reinsert volume cache entry in rollback procedure for volume %s: %v", volId, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Deleting a volume requires admin credentials
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
cr, err := getAdminCredentials(req.GetControllerDeleteSecrets())
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to retrieve admin credentials: %v", err)
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
if err = purgeVolume(volId, cr, &ent.VolOptions); err != nil {
|
|
|
|
glog.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())
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:21:11 +00:00
|
|
|
if err = deleteCephUser(cr, volId); err != nil {
|
|
|
|
glog.Errorf("failed to delete ceph user for volume %s: %v", volId, err)
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
glog.Infof("cephfs: successfuly deleted volume %s", volId)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
func (cs *controllerServer) ValidateVolumeCapabilities(
|
|
|
|
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 {
|
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Supported: false, Message: ""}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 16:05:19 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Supported: true}, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|