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 (
|
|
|
|
"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 (
|
|
|
|
deviceID = "deviceID"
|
|
|
|
provisionRoot = "/tmp/"
|
|
|
|
)
|
|
|
|
|
|
|
|
type controllerServer struct {
|
|
|
|
*csicommon.DefaultControllerServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
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-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 create volume req: %v", req)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
volumeId := uuid.NewUUID().String()
|
|
|
|
path := provisionRoot + volumeId
|
|
|
|
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)
|
|
|
|
return &csi.CreateVolumeResponse{
|
2018-02-15 13:50:31 +00:00
|
|
|
Volume: &csi.Volume{
|
2018-03-06 22:33:18 +00:00
|
|
|
Id: volumeId,
|
|
|
|
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
|
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
|
|
|
|
}
|
|
|
|
volumeId := req.VolumeId
|
|
|
|
glog.V(4).Infof("deleting volume %s", volumeId)
|
|
|
|
path := provisionRoot + volumeId
|
|
|
|
os.RemoveAll(path)
|
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|