ceph-csi/vendor/github.com/kubernetes-csi/drivers/pkg/cinder/nodeserver.go
2018-07-31 14:53:26 +02:00

168 lines
4.2 KiB
Go

/*
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 cinder
import (
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/kubernetes-csi/drivers/pkg/cinder/mount"
csicommon "github.com/kubernetes-csi/drivers/pkg/csi-common"
)
type nodeServer struct {
*csicommon.DefaultNodeServer
}
func (ns *nodeServer) NodeGetId(ctx context.Context, req *csi.NodeGetIdRequest) (*csi.NodeGetIdResponse, error) {
nodeID, err := getNodeID()
if err != nil {
return nil, err
}
if len(nodeID) > 0 {
return &csi.NodeGetIdResponse{
NodeId: nodeID,
}, nil
}
// Using default function
return ns.DefaultNodeServer.NodeGetId(ctx, req)
}
func (ns *nodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
nodeID, err := getNodeID()
if err != nil {
return nil, err
}
if len(nodeID) > 0 {
return &csi.NodeGetInfoResponse{
NodeId: nodeID,
}, nil
}
// Using default function
return ns.DefaultNodeServer.NodeGetInfo(ctx, req)
}
func getNodeID() (string, error) {
// Get Mount Provider
m, err := mount.GetMountProvider()
if err != nil {
glog.V(3).Infof("Failed to GetMountProvider: %v", err)
return "", err
}
nodeID, err := m.GetInstanceID()
if err != nil {
glog.V(3).Infof("Failed to GetInstanceID: %v", err)
return "", err
}
return nodeID, nil
}
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
targetPath := req.GetTargetPath()
fsType := req.GetVolumeCapability().GetMount().GetFsType()
devicePath := req.GetPublishInfo()["DevicePath"]
// Get Mount Provider
m, err := mount.GetMountProvider()
if err != nil {
glog.V(3).Infof("Failed to GetMountProvider: %v", err)
return nil, err
}
// Device Scan
err = m.ScanForAttach(devicePath)
if err != nil {
glog.V(3).Infof("Failed to ScanForAttach: %v", err)
return nil, err
}
// Verify whether mounted
notMnt, err := m.IsLikelyNotMountPointAttach(targetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// Volume Mount
if notMnt {
// Get Options
var options []string
if req.GetReadonly() {
options = append(options, "ro")
} else {
options = append(options, "rw")
}
mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
options = append(options, mountFlags...)
// Mount
err = m.FormatAndMount(devicePath, targetPath, fsType, options)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
targetPath := req.GetTargetPath()
// Get Mount Provider
m, err := mount.GetMountProvider()
if err != nil {
glog.V(3).Infof("Failed to GetMountProvider: %v", err)
return nil, err
}
notMnt, err := m.IsLikelyNotMountPointDetach(targetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if notMnt {
return nil, status.Error(codes.NotFound, "Volume not mounted")
}
err = m.UnmountPath(req.GetTargetPath())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
return &csi.NodeStageVolumeResponse{}, nil
}