ceph-csi/vendor/github.com/kubernetes-csi/drivers/pkg/hostpath/controllerserver.go

142 lines
4.9 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2017 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 hostpath
import (
2018-07-18 14:47:22 +00:00
"fmt"
2018-01-09 18:57:14 +00:00
"os"
"github.com/golang/glog"
"github.com/pborman/uuid"
"golang.org/x/net/context"
2018-02-15 13:50:31 +00:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2018-01-09 18:57:14 +00:00
2018-03-06 22:33:18 +00:00
"github.com/container-storage-interface/spec/lib/go/csi/v0"
2018-01-09 18:57:14 +00:00
"github.com/kubernetes-csi/drivers/pkg/csi-common"
)
const (
2018-07-18 14:47:22 +00:00
deviceID = "deviceID"
provisionRoot = "/tmp/"
maxStorageCapacity = tib
2018-01-09 18:57:14 +00:00
)
type controllerServer struct {
*csicommon.DefaultControllerServer
}
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
2018-07-18 14:47:22 +00:00
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err
}
2018-02-15 13:50:31 +00:00
// Check arguments
if len(req.GetName()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
}
if req.GetVolumeCapabilities() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities missing in request")
}
2018-07-18 14:47:22 +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 := getVolumeByName(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()))
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
// Check for maximum available capacity
capacity := int64(req.GetCapacityRange().GetRequiredBytes())
if capacity >= maxStorageCapacity {
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, maxStorageCapacity)
}
volumeID := uuid.NewUUID().String()
path := provisionRoot + volumeID
2018-01-09 18:57:14 +00:00
err := os.MkdirAll(path, 0777)
if err != nil {
glog.V(3).Infof("failed to create volume: %v", err)
return nil, err
}
glog.V(4).Infof("create volume %s", path)
2018-07-18 14:47:22 +00:00
hostPathVol := hostPathVolume{}
hostPathVol.VolName = req.GetName()
hostPathVol.VolID = volumeID
hostPathVol.VolSize = capacity
hostPathVol.VolPath = path
hostPathVolumes[volumeID] = hostPathVol
2018-01-09 18:57:14 +00:00
return &csi.CreateVolumeResponse{
2018-02-15 13:50:31 +00:00
Volume: &csi.Volume{
2018-07-18 14:47:22 +00:00
Id: volumeID,
2018-03-06 22:33:18 +00:00
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
2018-07-18 14:47:22 +00:00
Attributes: req.GetParameters(),
2018-01-09 18:57:14 +00:00
},
}, nil
}
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
2018-02-15 13:50:31 +00:00
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
2018-03-06 22:33:18 +00:00
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
2018-01-09 18:57:14 +00:00
glog.V(3).Infof("invalid delete volume req: %v", req)
return nil, err
}
2018-07-18 14:47:22 +00:00
volumeID := req.VolumeId
glog.V(4).Infof("deleting volume %s", volumeID)
path := provisionRoot + volumeID
2018-01-09 18:57:14 +00:00
os.RemoveAll(path)
2018-07-18 14:47:22 +00:00
delete(hostPathVolumes, volumeID)
2018-01-09 18:57:14 +00:00
return &csi.DeleteVolumeResponse{}, nil
}
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
2018-02-15 13:50:31 +00:00
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if req.GetVolumeCapabilities() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities missing in request")
}
2018-07-18 14:47:22 +00:00
if _, ok := hostPathVolumes[req.GetVolumeId()]; !ok {
return nil, status.Error(codes.NotFound, "Volume does not exist")
}
2018-02-15 13:50:31 +00:00
2018-01-09 18:57:14 +00:00
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
}