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 (
|
2018-01-16 01:52:28 +00:00
|
|
|
"fmt"
|
2018-01-09 18:59:50 +00:00
|
|
|
"os"
|
2018-01-16 01:52:28 +00:00
|
|
|
"strings"
|
2018-01-09 18:59:50 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2018-11-24 19:18:24 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-01-09 18:59:50 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-01-18 19:13:08 +00:00
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
|
|
|
|
"github.com/kubernetes-csi/drivers/pkg/csi-common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nodeServer struct {
|
|
|
|
*csicommon.DefaultNodeServer
|
2018-10-15 14:59:41 +00:00
|
|
|
mounter mount.Interface
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
|
|
|
|
targetPath := req.GetTargetPath()
|
|
|
|
|
2018-01-16 01:52:28 +00:00
|
|
|
if !strings.HasSuffix(targetPath, "/mount") {
|
|
|
|
return nil, fmt.Errorf("rnd: malformed the value of target path: %s", targetPath)
|
|
|
|
}
|
|
|
|
s := strings.Split(strings.TrimSuffix(targetPath, "/mount"), "/")
|
|
|
|
volName := s[len(s)-1]
|
|
|
|
|
2018-10-17 12:52:45 +00:00
|
|
|
targetPathMutex.LockKey(targetPath)
|
|
|
|
defer targetPathMutex.UnlockKey(targetPath)
|
|
|
|
|
2018-10-15 14:59:41 +00:00
|
|
|
notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetPath, 0750); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
notMnt = true
|
|
|
|
} else {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !notMnt {
|
|
|
|
return &csi.NodePublishVolumeResponse{}, nil
|
|
|
|
}
|
2018-11-24 19:18:24 +00:00
|
|
|
volOptions, err := getRBDVolumeOptions(req.GetVolumeContext())
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 01:52:28 +00:00
|
|
|
volOptions.VolName = volName
|
2018-01-09 18:59:50 +00:00
|
|
|
// Mapping RBD image
|
2018-11-24 19:18:24 +00:00
|
|
|
devicePath, err := attachRBDImage(volOptions, volOptions.UserId, req.GetSecrets())
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-21 14:07:14 +00:00
|
|
|
glog.V(4).Infof("rbd image: %s/%s was successfully mapped at %s\n", req.GetVolumeId(), volOptions.Pool, devicePath)
|
2018-01-09 18:59:50 +00:00
|
|
|
fsType := req.GetVolumeCapability().GetMount().GetFsType()
|
|
|
|
|
|
|
|
readOnly := req.GetReadonly()
|
2018-11-24 19:18:24 +00:00
|
|
|
attrib := req.GetVolumeContext()
|
2018-01-09 18:59:50 +00:00
|
|
|
mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
|
|
|
|
|
|
|
|
glog.V(4).Infof("target %v\nfstype %v\ndevice %v\nreadonly %v\nattributes %v\n mountflags %v\n",
|
|
|
|
targetPath, fsType, devicePath, readOnly, attrib, mountFlags)
|
|
|
|
|
|
|
|
options := []string{}
|
|
|
|
if readOnly {
|
|
|
|
options = append(options, "ro")
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:59:41 +00:00
|
|
|
diskMounter := &mount.SafeFormatAndMount{Interface: ns.mounter, Exec: mount.NewOsExec()}
|
2018-01-09 18:59:50 +00:00
|
|
|
if err := diskMounter.FormatAndMount(devicePath, targetPath, fsType, options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.NodePublishVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
|
|
|
|
targetPath := req.GetTargetPath()
|
2018-10-17 12:52:45 +00:00
|
|
|
targetPathMutex.LockKey(targetPath)
|
|
|
|
defer targetPathMutex.UnlockKey(targetPath)
|
2018-01-09 18:59:50 +00:00
|
|
|
|
2018-10-15 14:59:41 +00:00
|
|
|
notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
if notMnt {
|
|
|
|
return nil, status.Error(codes.NotFound, "Volume not mounted")
|
|
|
|
}
|
2018-02-20 16:10:59 +00:00
|
|
|
|
2018-10-15 14:59:41 +00:00
|
|
|
devicePath, cnt, err := mount.GetDeviceNameFromMount(ns.mounter, targetPath)
|
2018-02-20 16:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
// Unmounting the image
|
2018-10-15 14:59:41 +00:00
|
|
|
err = ns.mounter.Unmount(targetPath)
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2018-02-20 16:10:59 +00:00
|
|
|
|
|
|
|
cnt--
|
|
|
|
if cnt != 0 {
|
|
|
|
return &csi.NodeUnpublishVolumeResponse{}, nil
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2018-02-20 16:10:59 +00:00
|
|
|
|
|
|
|
// Unmapping rbd device
|
|
|
|
if err := detachRBDDevice(devicePath); err != nil {
|
|
|
|
glog.V(3).Infof("failed to unmap rbd device: %s with error: %v", devicePath, err)
|
2018-01-09 18:59:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.NodeUnpublishVolumeResponse{}, nil
|
|
|
|
}
|
2018-03-06 22:33:57 +00:00
|
|
|
|
|
|
|
func (ns *nodeServer) NodeStageVolume(
|
|
|
|
ctx context.Context,
|
|
|
|
req *csi.NodeStageVolumeRequest) (
|
|
|
|
*csi.NodeStageVolumeResponse, error) {
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Unimplemented, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *nodeServer) NodeUnstageVolume(
|
|
|
|
ctx context.Context,
|
|
|
|
req *csi.NodeUnstageVolumeRequest) (
|
|
|
|
*csi.NodeUnstageVolumeResponse, error) {
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Unimplemented, "")
|
|
|
|
}
|