ceph-csi/pkg/rbd/controllerserver.go

165 lines
6.0 KiB
Go
Raw Normal View History

2018-01-09 18:59:50 +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 rbd
import (
"fmt"
"path"
2018-03-06 22:33:57 +00:00
"github.com/container-storage-interface/spec/lib/go/csi/v0"
2018-01-09 18:59:50 +00:00
"github.com/golang/glog"
2018-03-06 22:33:57 +00:00
"github.com/kubernetes-csi/drivers/pkg/csi-common"
2018-01-09 18:59:50 +00:00
"github.com/pborman/uuid"
"golang.org/x/net/context"
2018-03-06 22:33:57 +00:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2018-01-09 18:59:50 +00:00
)
const (
oneGB = 1073741824
)
type controllerServer struct {
*csicommon.DefaultControllerServer
}
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
2018-03-06 22:33:57 +00:00
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
2018-01-09 18:59:50 +00:00
glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err
}
2018-03-06 22:33:57 +00:00
// Check sanity of request Name, Volume Capabilities
if len(req.Name) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
}
if req.VolumeCapabilities == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
}
2018-03-06 22:33:57 +00:00
// Need to check for already existing volume name, and if found
// check for the requested capacity and already allocated capacity
if exVol, err := getRBDVolumeByName(req.GetName()); err == nil {
// Since err is nil, it means the volume with the same name already exists
// need to check if the size of exisiting volume is the same as in new
// request
if exVol.VolSize >= int64(req.GetCapacityRange().GetRequiredBytes()) {
// exisiting volume is compatible with new request and should be reused.
// TODO (sbezverk) Do I need to make sure that RBD volume still exists?
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
Id: exVol.VolID,
CapacityBytes: int64(exVol.VolSize),
Attributes: req.GetParameters(),
},
}, nil
}
return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("Volume with the same name: %s but with different size already exist", req.GetName()))
}
// TODO (sbezverk) Last check for not exceeding total storage capacity
rbdVol, err := getRBDVolumeOptions(req.GetParameters())
if err != nil {
return nil, err
}
// Generating Volume Name and Volume ID, as accoeding to CSI spec they MUST be different
2018-01-09 18:59:50 +00:00
volName := req.GetName()
uniqueID := uuid.NewUUID().String()
2018-01-09 18:59:50 +00:00
if len(volName) == 0 {
2018-03-06 22:33:57 +00:00
volName = rbdVol.Pool + "-dynamic-pvc-" + uniqueID
2018-01-09 18:59:50 +00:00
}
2018-03-06 22:33:57 +00:00
rbdVol.VolName = volName
volumeID := "csi-rbd-" + uniqueID
2018-03-06 22:33:57 +00:00
rbdVol.VolID = volumeID
2018-01-09 18:59:50 +00:00
// Volume Size - Default is 1 GiB
volSizeBytes := int64(oneGB)
if req.GetCapacityRange() != nil {
volSizeBytes = int64(req.GetCapacityRange().GetRequiredBytes())
}
2018-03-06 22:33:57 +00:00
rbdVol.VolSize = volSizeBytes
2018-01-09 18:59:50 +00:00
volSizeGB := int(volSizeBytes / 1024 / 1024 / 1024)
// Check if there is already RBD image with requested name
2018-03-06 22:33:57 +00:00
found, _, _ := rbdStatus(rbdVol, req.GetControllerCreateSecrets())
2018-01-09 18:59:50 +00:00
if !found {
2018-03-06 22:33:57 +00:00
if err := createRBDImage(rbdVol, volSizeGB, req.GetControllerCreateSecrets()); err != nil {
2018-01-09 18:59:50 +00:00
if err != nil {
glog.Warningf("failed to create volume: %v", err)
return nil, err
}
}
glog.V(4).Infof("create volume %s", volName)
}
2018-03-06 22:33:57 +00:00
// Storing volInfo into a persistent file.
if err := persistVolInfo(volumeID, path.Join(PluginFolder, "controller"), rbdVol); err != nil {
2018-01-09 18:59:50 +00:00
glog.Warningf("rbd: failed to store volInfo with error: %v", err)
}
2018-03-06 22:33:57 +00:00
rbdVolumes[volumeID] = *rbdVol
2018-01-09 18:59:50 +00:00
return &csi.CreateVolumeResponse{
2018-02-15 13:51:23 +00:00
Volume: &csi.Volume{
Id: volumeID,
2018-02-15 13:51:23 +00:00
CapacityBytes: int64(volSizeBytes),
2018-01-09 18:59:50 +00:00
Attributes: req.GetParameters(),
},
}, nil
}
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
2018-03-06 22:33:57 +00:00
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
2018-01-09 18:59:50 +00:00
glog.Warningf("invalid delete volume req: %v", req)
return nil, err
}
// For now the image get unconditionally deleted, but here retention policy can be checked
volumeID := req.GetVolumeId()
2018-03-06 22:33:57 +00:00
rbdVol := &rbdVolume{}
if err := loadVolInfo(volumeID, path.Join(PluginFolder, "controller"), rbdVol); err != nil {
2018-01-09 18:59:50 +00:00
return nil, err
}
2018-03-06 22:33:57 +00:00
volName := rbdVol.VolName
2018-01-09 18:59:50 +00:00
// Deleting rbd image
glog.V(4).Infof("deleting volume %s", volName)
2018-03-06 22:33:57 +00:00
if err := deleteRBDImage(rbdVol, req.GetControllerDeleteSecrets()); err != nil {
glog.V(3).Infof("failed to delete rbd image: %s/%s with error: %v", rbdVol.Pool, volName, err)
2018-01-09 18:59:50 +00:00
return nil, err
}
// Removing persistent storage file for the unmapped volume
if err := deleteVolInfo(volumeID, path.Join(PluginFolder, "controller")); err != nil {
2018-01-09 18:59:50 +00:00
return nil, err
}
2018-03-06 22:33:57 +00:00
delete(rbdVolumes, volumeID)
return &csi.DeleteVolumeResponse{}, nil
}
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
for _, cap := range req.VolumeCapabilities {
if cap.GetAccessMode().GetMode() != csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER {
return &csi.ValidateVolumeCapabilitiesResponse{Supported: false, Message: ""}, nil
}
}
return &csi.ValidateVolumeCapabilitiesResponse{Supported: true, Message: ""}, nil
}
func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
2018-01-09 18:59:50 +00:00
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return &csi.ControllerPublishVolumeResponse{}, nil
}