Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Zimin <alexandr.zimin@flant.com>
  • Loading branch information
AleksZimin committed Mar 31, 2024
1 parent dcfa0fd commit 6cf8239
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
19 changes: 12 additions & 7 deletions images/sds-local-volume-csi/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
d.log.Error(err, "error GetStorageClassLVGs")
return nil, status.Errorf(codes.Internal, err.Error())
}
// TODO: Consider refactoring the naming strategy for llvName and lvName.
// Currently, we use the same name for llvName (the name of the LVMLogicalVolume resource in Kubernetes)
// and lvName (the name of the LV in LVM on the node) because the PV name is unique within the cluster,
// preventing name collisions. This approach simplifies matching between nodes and Kubernetes by maintaining
// the same name in both contexts. Future consideration should be given to optimizing this logic to enhance
// code readability and maintainability.
// TODO: Consider refactoring the naming strategy for llvName and lvName.
// Currently, we use the same name for llvName (the name of the LVMLogicalVolume resource in Kubernetes)
// and lvName (the name of the LV in LVM on the node) because the PV name is unique within the cluster,
// preventing name collisions. This approach simplifies matching between nodes and Kubernetes by maintaining
// the same name in both contexts. Future consideration should be given to optimizing this logic to enhance
// code readability and maintainability.
llvName := request.Name
lvName := request.Name
d.log.Info(fmt.Sprintf("llv name: %s ", llvName))
Expand Down Expand Up @@ -149,7 +149,12 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
}

volumeCtx[internal.SubPath] = request.Name
volumeCtx[internal.VGNameKey] = selectedLVG.Spec.ActualVGNameOnTheNode
volumeCtx[internal.VGNameKey] = llvSpec.LvmVolumeGroupName
if llvSpec.Type == internal.LLMTypeThin {
volumeCtx[internal.ThinPoolNameKey] = llvSpec.Thin.PoolName
} else {
volumeCtx[internal.ThinPoolNameKey] = ""
}

return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
Expand Down
8 changes: 6 additions & 2 deletions images/sds-local-volume-csi/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (d *Driver) NodePublishVolume(ctx context.Context, request *csi.NodePublish
d.log.Trace("------------- NodePublishVolume --------------")

dev := fmt.Sprintf("/dev/%s/%s", request.GetVolumeContext()[internal.VGNameKey], request.VolumeId)
lvmType := request.GetVolumeContext()[internal.LvmTypeKey]
lvmThinPoolName := request.GetVolumeContext()[internal.ThinPoolNameKey]
d.log.Info(fmt.Sprintf("dev = %s", dev))

var mountOptions []string
Expand All @@ -63,10 +65,12 @@ func (d *Driver) NodePublishVolume(ctx context.Context, request *csi.NodePublish
}

d.log.Info(fmt.Sprintf("mountOptions = %s", mountOptions))
d.log.Info(fmt.Sprintf("lvmType = %s", lvmType))
d.log.Info(fmt.Sprintf("lvmThinPoolName = %s", lvmThinPoolName))
d.log.Info(fmt.Sprintf("fsType = %s", fsType))
d.log.Info(fmt.Sprintf("IsBlock = %t", IsBlock))

err := d.storeManager.Mount(dev, request.GetTargetPath(), IsBlock, fsType, false, mountOptions)
err := d.storeManager.Mount(dev, request.GetTargetPath(), IsBlock, fsType, false, mountOptions, lvmType, lvmThinPoolName)
if err != nil {
d.log.Error(err, "d.mounter.Mount :")
return nil, err
Expand Down Expand Up @@ -146,7 +150,7 @@ func (d *Driver) NodeGetCapabilities(ctx context.Context, request *csi.NodeGetCa

func (d *Driver) NodeGetInfo(ctx context.Context, request *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
d.log.Info("method NodeGetInfo")
d.log.Info("hostID = ", d.hostID)
d.log.Info(fmt.Sprintf("hostID = %s", d.hostID))

return &csi.NodeGetInfoResponse{
NodeId: d.hostID,
Expand Down
1 change: 1 addition & 0 deletions images/sds-local-volume-csi/internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
TopologyKey = "topology.sds-local-volume-csi/node"
SubPath = "subPath"
VGNameKey = "vgname"
ThinPoolNameKey = "thinPoolName"
LLMTypeThin = "Thin"
LLMTypeThick = "Thick"
LLVStatusCreated = "Created"
Expand Down
52 changes: 30 additions & 22 deletions images/sds-local-volume-csi/pkg/utils/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ package utils
import (
"fmt"
"os"
"sds-local-volume-csi/internal"
"sds-local-volume-csi/pkg/logger"
"strings"

mu "k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
)

type NodeStoreManager interface {
Mount(source, target string, isBlock bool, fsType string, readonly bool, mntOpts []string) error
Mount(source, target string, isBlock bool, fsType string, readonly bool, mntOpts []string, lvmType, lvmThinPoolName string) error
Unmount(target string) error
IsNotMountPoint(target string) (bool, error)
IsMountPointCorrect(mountRefs []string, target string) bool
ResizeFS(target string) error
}

Expand All @@ -48,20 +49,20 @@ func NewStore(logger *logger.Logger) *Store {
}
}

func (s *Store) Mount(source, target string, isBlock bool, fsType string, readonly bool, mntOpts []string) error {
func (s *Store) Mount(devSourcePath, target string, isBlock bool, fsType string, readonly bool, mntOpts []string, lvmType, lvmThinPoolName string) error {
s.Log.Info(" ----== Node Mount ==---- ")

s.Log.Trace("≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈ Mount options ≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈")
s.Log.Trace(fmt.Sprintf("[mount] params source=%s target=%s fs=%s blockMode=%t mountOptions=%v", source, target, fsType, isBlock, mntOpts))
s.Log.Trace(fmt.Sprintf("[mount] params source=%s target=%s fs=%s blockMode=%t mountOptions=%v", devSourcePath, target, fsType, isBlock, mntOpts))
s.Log.Trace("≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈ Mount options ≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈")

info, err := os.Stat(source)
info, err := os.Stat(devSourcePath)
if err != nil {
return fmt.Errorf("failed to stat source device: %w", err)
}

if (info.Mode() & os.ModeDevice) != os.ModeDevice {
return fmt.Errorf("[NewMount] path %s is not a device", source)
return fmt.Errorf("[NewMount] path %s is not a device", devSourcePath)
}

s.Log.Trace("≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈ MODE SOURCE ≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈")
Expand Down Expand Up @@ -91,24 +92,29 @@ func (s *Store) Mount(source, target string, isBlock bool, fsType string, readon
s.Log.Trace("≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈ isMountPoint ≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈")

if isMountPoint {
s.Log.Trace(fmt.Sprintf("Try to find mount refs for source %s", source))
mountRefs, err := s.NodeStorage.GetMountRefs(source)
mapperSourcePath := toMapperPath(devSourcePath)
s.Log.Trace(fmt.Sprintf("Target %s is a mount point. Checking if it is already mounted to source %s or %s", target, devSourcePath, mapperSourcePath))

mountedDevicePath, _, err := mu.GetDeviceNameFromMount(s.NodeStorage.Interface, target)
if err != nil {
return fmt.Errorf("failed to get mount refs for source %s: %w", source, err)
return fmt.Errorf("failed to find the device mounted at %s: %w", target, err)
}
s.Log.Trace(fmt.Sprintf("Found mount refs: %v", mountRefs))
mountPointCorrect := s.IsMountPointCorrect(mountRefs, target)
if !mountPointCorrect {
return fmt.Errorf("target %s is a mount point and is not mounted to source %s", target, source)
s.Log.Trace(fmt.Sprintf("Found device mounted at %s: %s", target, mountedDevicePath))

if mountedDevicePath != devSourcePath && mountedDevicePath != mapperSourcePath {
return fmt.Errorf("target %s is a mount point and is not mounted to source %s or %s", target, devSourcePath, mapperSourcePath)
}

s.Log.Trace(fmt.Sprintf("Target %s is a mount point and already mounted to source %s. Skipping FormatAndMount without any checks", target, source))
s.Log.Trace(fmt.Sprintf("Target %s is a mount point and already mounted to source %s. Skipping FormatAndMount without any checks", target, devSourcePath))
return nil
}

s.Log.Trace("-----------------== start FormatAndMount ==---------------")

err = s.NodeStorage.FormatAndMount(source, target, fsType, mntOpts)
if lvmType == internal.LLMTypeThin {
s.Log.Trace(fmt.Sprintf("LVM type is Thin. Ckecking free space in thin pool %s", lvmThinPoolName))
}
err = s.NodeStorage.FormatAndMount(devSourcePath, target, fsType, mntOpts)
if err != nil {
return fmt.Errorf("failed to FormatAndMount : %w", err)
}
Expand All @@ -129,7 +135,7 @@ func (s *Store) Mount(source, target string, isBlock bool, fsType string, readon
}
s.Log.Trace("-----------------== stop Create File ==---------------")
s.Log.Trace("-----------------== start Mount ==---------------")
err = s.NodeStorage.Mount(source, target, fsType, mntOpts)
err = s.NodeStorage.Mount(devSourcePath, target, fsType, mntOpts)
if err != nil {
s.Log.Error(err, "block mount error :")
return err
Expand Down Expand Up @@ -184,11 +190,13 @@ func (s *Store) ResizeFS(mountTarget string) error {
return nil
}

func (s *Store) IsMountPointCorrect(mountRefs []string, target string) bool {
for _, ref := range mountRefs {
if ref == target {
return true
}
func toMapperPath(devPath string) string {
if !strings.HasPrefix(devPath, "/dev/") {
return ""
}
return false

shortPath := strings.TrimPrefix(devPath, "/dev/")
mapperPath := strings.Replace(shortPath, "-", "--", -1)
mapperPath = strings.Replace(mapperPath, "/", "-", -1)
return "/dev/mapper/" + mapperPath
}

0 comments on commit 6cf8239

Please sign in to comment.