mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
Fix gometalinter issues
Signed-off-by: Madhu Rajanna <mrajanna@redhat.com>
This commit is contained in:
@ -87,6 +87,33 @@ func (cs *ControllerServer) validateVolumeReq(req *csi.CreateVolumeRequest) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseVolCreateRequest(req *csi.CreateVolumeRequest) (*rbdVolume, error) {
|
||||
// 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 according to CSI spec they MUST be different
|
||||
volName := req.GetName()
|
||||
uniqueID := uuid.NewUUID().String()
|
||||
if len(volName) == 0 {
|
||||
volName = rbdVol.Pool + "-dynamic-pvc-" + uniqueID
|
||||
}
|
||||
rbdVol.VolName = volName
|
||||
volumeID := "csi-rbd-vol-" + uniqueID
|
||||
rbdVol.VolID = volumeID
|
||||
// Volume Size - Default is 1 GiB
|
||||
volSizeBytes := int64(oneGB)
|
||||
if req.GetCapacityRange() != nil {
|
||||
volSizeBytes = req.GetCapacityRange().GetRequiredBytes()
|
||||
}
|
||||
rbdVol.VolSize = volSizeBytes
|
||||
|
||||
return rbdVol, nil
|
||||
}
|
||||
|
||||
// CreateVolume creates the volume in backend and store the volume metadata
|
||||
func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
||||
|
||||
@ -120,36 +147,19 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
return nil, status.Errorf(codes.AlreadyExists, "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())
|
||||
rbdVol, err := parseVolCreateRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Generating Volume Name and Volume ID, as according to CSI spec they MUST be different
|
||||
volName := req.GetName()
|
||||
uniqueID := uuid.NewUUID().String()
|
||||
if len(volName) == 0 {
|
||||
volName = rbdVol.Pool + "-dynamic-pvc-" + uniqueID
|
||||
}
|
||||
rbdVol.VolName = volName
|
||||
volumeID := "csi-rbd-vol-" + uniqueID
|
||||
rbdVol.VolID = volumeID
|
||||
// Volume Size - Default is 1 GiB
|
||||
volSizeBytes := int64(oneGB)
|
||||
if req.GetCapacityRange() != nil {
|
||||
volSizeBytes = req.GetCapacityRange().GetRequiredBytes()
|
||||
}
|
||||
rbdVol.VolSize = volSizeBytes
|
||||
volSizeGB := int(volSizeBytes / 1024 / 1024 / 1024)
|
||||
volSizeGB := int(rbdVol.VolSize / 1024 / 1024 / 1024)
|
||||
|
||||
// Check if there is already RBD image with requested name
|
||||
err = cs.checkrbdStatus(rbdVol, req, volSizeGB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if createErr := cs.MetadataStore.Create(volumeID, rbdVol); createErr != nil {
|
||||
if createErr := cs.MetadataStore.Create(rbdVol.VolID, rbdVol); createErr != nil {
|
||||
glog.Warningf("failed to store volume metadata with error: %v", err)
|
||||
if err = deleteRBDImage(rbdVol, rbdVol.AdminID, req.GetSecrets()); err != nil {
|
||||
glog.V(3).Infof("failed to delete rbd image: %s/%s with error: %v", rbdVol.Pool, rbdVol.VolName, err)
|
||||
@ -158,11 +168,11 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
return nil, createErr
|
||||
}
|
||||
|
||||
rbdVolumes[volumeID] = rbdVol
|
||||
rbdVolumes[rbdVol.VolID] = rbdVol
|
||||
return &csi.CreateVolumeResponse{
|
||||
Volume: &csi.Volume{
|
||||
VolumeId: volumeID,
|
||||
CapacityBytes: volSizeBytes,
|
||||
VolumeId: rbdVol.VolID,
|
||||
CapacityBytes: rbdVol.VolSize,
|
||||
VolumeContext: req.GetParameters(),
|
||||
},
|
||||
}, nil
|
||||
|
@ -215,11 +215,20 @@ func (ns *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
if err = ns.unmount(targetPath, devicePath, cnt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &csi.NodeUnpublishVolumeResponse{}, nil
|
||||
}
|
||||
|
||||
func (ns *NodeServer) unmount(targetPath, devicePath string, cnt int) error {
|
||||
var err error
|
||||
// Bind mounted device needs to be resolved by using resolveBindMountedBlockDevice
|
||||
if devicePath == "devtmpfs" {
|
||||
devicePath, err = resolveBindMountedBlockDevice(targetPath)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
glog.V(4).Infof("NodeUnpublishVolume: devicePath: %s, (original)cnt: %d\n", devicePath, cnt)
|
||||
// cnt for GetDeviceNameFromMount is broken for bind mouted device,
|
||||
@ -235,30 +244,27 @@ func (ns *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
|
||||
err = ns.mounter.Unmount(targetPath)
|
||||
if err != nil {
|
||||
glog.V(3).Infof("failed to unmount targetPath: %s with error: %v", targetPath, err)
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
cnt--
|
||||
if cnt != 0 {
|
||||
// TODO should this be fixed not to success, so that driver can retry unmounting?
|
||||
return &csi.NodeUnpublishVolumeResponse{}, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unmapping rbd device
|
||||
if err = detachRBDDevice(devicePath); err != nil {
|
||||
glog.V(3).Infof("failed to unmap rbd device: %s with error: %v", devicePath, err)
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove targetPath
|
||||
if err = os.RemoveAll(targetPath); err != nil {
|
||||
glog.V(3).Infof("failed to remove targetPath: %s with error: %v", targetPath, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &csi.NodeUnpublishVolumeResponse{}, nil
|
||||
return err
|
||||
}
|
||||
|
||||
func resolveBindMountedBlockDevice(mountPath string) (string, error) {
|
||||
// #nosec
|
||||
cmd := exec.Command("findmnt", "-n", "-o", "SOURCE", "--first-only", "--target", mountPath)
|
||||
@ -275,12 +281,13 @@ func parseFindMntResolveSource(out string) (string, error) {
|
||||
// cut trailing newline
|
||||
out = strings.TrimSuffix(out, "\n")
|
||||
// Check if out is a mounted device
|
||||
reMnt := regexp.MustCompile(`^(/[^/]+(?:/[^/]*)*)$`)
|
||||
reMnt := regexp.MustCompile("^(/[^/]+(?:/[^/]*)*)$")
|
||||
if match := reMnt.FindStringSubmatch(out); match != nil {
|
||||
return match[1], nil
|
||||
}
|
||||
// Check if out is a block device
|
||||
reBlk := regexp.MustCompile(`^devtmpfs\\[(/[^/]+(?:/[^/]*)*)\\]$`)
|
||||
// nolint
|
||||
reBlk := regexp.MustCompile("^devtmpfs\\[(/[^/]+(?:/[^/]*)*)\\]$")
|
||||
if match := reBlk.FindStringSubmatch(out); match != nil {
|
||||
return fmt.Sprintf("/dev%s", match[1]), nil
|
||||
}
|
||||
|
@ -137,42 +137,8 @@ func getNbdDevFromImageAndPool(pool string, image string) (string, bool) {
|
||||
|
||||
for i := 0; i < maxNbds; i++ {
|
||||
nbdPath := basePath + strconv.Itoa(i)
|
||||
_, err := os.Lstat(nbdPath)
|
||||
devicePath, err := getnbdDevicePath(nbdPath, imgPath, i)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("error reading nbd info directory %s: %v", nbdPath, err)
|
||||
continue
|
||||
}
|
||||
// #nosec
|
||||
pidBytes, err := ioutil.ReadFile(path.Join(nbdPath, "pid"))
|
||||
if err != nil {
|
||||
glog.V(5).Infof("did not find valid pid file in dir %s: %v", nbdPath, err)
|
||||
continue
|
||||
}
|
||||
cmdlineFileName := path.Join(hostRootFS, "/proc", strings.TrimSpace(string(pidBytes)), "cmdline")
|
||||
// #nosec
|
||||
rawCmdline, err := ioutil.ReadFile(cmdlineFileName)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("failed to read cmdline file %s: %v", cmdlineFileName, err)
|
||||
continue
|
||||
}
|
||||
cmdlineArgs := strings.FieldsFunc(string(rawCmdline), func(r rune) bool {
|
||||
return r == '\u0000'
|
||||
})
|
||||
// Check if this process is mapping a rbd device.
|
||||
// Only accepted pattern of cmdline is from execRbdMap:
|
||||
// rbd-nbd map pool/image ...
|
||||
if len(cmdlineArgs) < 3 || cmdlineArgs[0] != rbdTonbd || cmdlineArgs[1] != "map" {
|
||||
glog.V(4).Infof("nbd device %s is not used by rbd", nbdPath)
|
||||
continue
|
||||
}
|
||||
if cmdlineArgs[2] != imgPath {
|
||||
glog.V(4).Infof("rbd-nbd device %s did not match expected image path: %s with path found: %s",
|
||||
nbdPath, imgPath, cmdlineArgs[2])
|
||||
continue
|
||||
}
|
||||
devicePath := path.Join("/dev", "nbd"+strconv.Itoa(i))
|
||||
if _, err := os.Lstat(devicePath); err != nil {
|
||||
glog.Warningf("Stat device %s for imgpath %s failed %v", devicePath, imgPath, err)
|
||||
continue
|
||||
}
|
||||
return devicePath, true
|
||||
@ -180,6 +146,50 @@ func getNbdDevFromImageAndPool(pool string, image string) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
func getnbdDevicePath(nbdPath, imgPath string, count int) (string, error) {
|
||||
|
||||
_, err := os.Lstat(nbdPath)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("error reading nbd info directory %s: %v", nbdPath, err)
|
||||
return "", err
|
||||
}
|
||||
// #nosec
|
||||
pidBytes, err := ioutil.ReadFile(path.Join(nbdPath, "pid"))
|
||||
if err != nil {
|
||||
glog.V(5).Infof("did not find valid pid file in dir %s: %v", nbdPath, err)
|
||||
return "", err
|
||||
}
|
||||
cmdlineFileName := path.Join(hostRootFS, "/proc", strings.TrimSpace(string(pidBytes)), "cmdline")
|
||||
// #nosec
|
||||
rawCmdline, err := ioutil.ReadFile(cmdlineFileName)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("failed to read cmdline file %s: %v", cmdlineFileName, err)
|
||||
return "", err
|
||||
}
|
||||
cmdlineArgs := strings.FieldsFunc(string(rawCmdline), func(r rune) bool {
|
||||
return r == '\u0000'
|
||||
})
|
||||
// Check if this process is mapping a rbd device.
|
||||
// Only accepted pattern of cmdline is from execRbdMap:
|
||||
// rbd-nbd map pool/image ...
|
||||
if len(cmdlineArgs) < 3 || cmdlineArgs[0] != rbdTonbd || cmdlineArgs[1] != "map" {
|
||||
glog.V(4).Infof("nbd device %s is not used by rbd", nbdPath)
|
||||
return "", err
|
||||
|
||||
}
|
||||
if cmdlineArgs[2] != imgPath {
|
||||
glog.V(4).Infof("rbd-nbd device %s did not match expected image path: %s with path found: %s",
|
||||
nbdPath, imgPath, cmdlineArgs[2])
|
||||
return "", err
|
||||
}
|
||||
devicePath := path.Join("/dev", "nbd"+strconv.Itoa(count))
|
||||
if _, err := os.Lstat(devicePath); err != nil {
|
||||
glog.Warningf("Stat device %s for imgpath %s failed %v", devicePath, imgPath, err)
|
||||
return "", err
|
||||
}
|
||||
return devicePath, nil
|
||||
}
|
||||
|
||||
// Stat a path, if it doesn't exist, retry maxRetries times.
|
||||
func waitForPath(pool, image string, maxRetries int, useNbdDriver bool) (string, bool) {
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
@ -216,21 +226,18 @@ func checkRbdNbdTools() bool {
|
||||
|
||||
func attachRBDImage(volOptions *rbdVolume, userID string, credentials map[string]string) (string, error) {
|
||||
var err error
|
||||
var output []byte
|
||||
|
||||
image := volOptions.VolName
|
||||
imagePath := fmt.Sprintf("%s/%s", volOptions.Pool, image)
|
||||
|
||||
useNBD := false
|
||||
cmdName := rbd
|
||||
moduleName := rbd
|
||||
if volOptions.Mounter == rbdTonbd && hasNBD {
|
||||
useNBD = true
|
||||
cmdName = rbdTonbd
|
||||
moduleName = nbd
|
||||
}
|
||||
|
||||
devicePath, found := waitForPath(volOptions.Pool, image, 1, useNBD)
|
||||
_, found := waitForPath(volOptions.Pool, image, 1, useNBD)
|
||||
if !found {
|
||||
attachdetachMutex.LockKey(imagePath)
|
||||
|
||||
@ -243,6 +250,7 @@ func attachRBDImage(volOptions *rbdVolume, userID string, credentials map[string
|
||||
_, err = execCommand("modprobe", []string{moduleName})
|
||||
if err != nil {
|
||||
glog.Warningf("rbd: failed to load rbd kernel module:%v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
backoff := wait.Backoff{
|
||||
@ -250,33 +258,50 @@ func attachRBDImage(volOptions *rbdVolume, userID string, credentials map[string
|
||||
Factor: rbdImageWatcherFactor,
|
||||
Steps: rbdImageWatcherSteps,
|
||||
}
|
||||
err := waitForrbdImage(backoff, volOptions, userID, credentials)
|
||||
err = waitForrbdImage(backoff, volOptions, userID, credentials)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mon, err := getMon(volOptions, credentials)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
glog.V(5).Infof("rbd: map mon %s", mon)
|
||||
key, err := getRBDKey(userID, credentials)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
output, err = execCommand(cmdName, []string{
|
||||
"map", imagePath, "--id", userID, "-m", mon, "--key=" + key})
|
||||
if err != nil {
|
||||
glog.Warningf("rbd: map error %v, rbd output: %s", err, string(output))
|
||||
return "", fmt.Errorf("rbd: map failed %v, rbd output: %s", err, string(output))
|
||||
}
|
||||
devicePath, found = waitForPath(volOptions.Pool, image, 10, useNBD)
|
||||
if !found {
|
||||
return "", fmt.Errorf("Could not map image %s, Timeout after 10s", imagePath)
|
||||
}
|
||||
}
|
||||
devicePath, err := createPath(volOptions, userID, credentials)
|
||||
|
||||
return devicePath, err
|
||||
}
|
||||
|
||||
func createPath(volOpt *rbdVolume, userID string, creds map[string]string) (string, error) {
|
||||
image := volOpt.VolName
|
||||
imagePath := fmt.Sprintf("%s/%s", volOpt.Pool, image)
|
||||
|
||||
mon, err := getMon(volOpt, creds)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
glog.V(5).Infof("rbd: map mon %s", mon)
|
||||
key, err := getRBDKey(userID, creds)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
useNBD := false
|
||||
cmdName := rbd
|
||||
if volOpt.Mounter == rbdTonbd && hasNBD {
|
||||
useNBD = true
|
||||
cmdName = rbdTonbd
|
||||
}
|
||||
|
||||
output, err := execCommand(cmdName, []string{
|
||||
"map", imagePath, "--id", userID, "-m", mon, "--key=" + key})
|
||||
if err != nil {
|
||||
glog.Warningf("rbd: map error %v, rbd output: %s", err, string(output))
|
||||
return "", fmt.Errorf("rbd: map failed %v, rbd output: %s", err, string(output))
|
||||
}
|
||||
devicePath, found := waitForPath(volOpt.Pool, image, 10, useNBD)
|
||||
if !found {
|
||||
return "", fmt.Errorf("Could not map image %s, Timeout after 10s", imagePath)
|
||||
}
|
||||
return devicePath, nil
|
||||
}
|
||||
|
||||
|
@ -259,6 +259,12 @@ func getRBDVolumeOptions(volOptions map[string]string) (*rbdVolume, error) {
|
||||
}
|
||||
|
||||
}
|
||||
getCredsFromVol(rbdVol, volOptions)
|
||||
return rbdVol, nil
|
||||
}
|
||||
|
||||
func getCredsFromVol(rbdVol *rbdVolume, volOptions map[string]string) {
|
||||
var ok bool
|
||||
rbdVol.AdminID, ok = volOptions["adminid"]
|
||||
if !ok {
|
||||
rbdVol.AdminID = rbdDefaultAdminID
|
||||
@ -271,9 +277,7 @@ func getRBDVolumeOptions(volOptions map[string]string) (*rbdVolume, error) {
|
||||
if !ok {
|
||||
rbdVol.Mounter = rbdDefaultMounter
|
||||
}
|
||||
return rbdVol, nil
|
||||
}
|
||||
|
||||
func getRBDSnapshotOptions(snapOptions map[string]string) (*rbdSnapshot, error) {
|
||||
var ok bool
|
||||
rbdSnap := &rbdSnapshot{}
|
||||
|
Reference in New Issue
Block a user