refactor: refactor nvmfDiskInfo and mounter/unmounter creation

- Refactor function arguments to allow reuse across multiple components
- Remove unused variables and parameters for cleaner implementation

Signed-off-by: cheolho.kang <cheolho.kang@samsung.com>
This commit is contained in:
cheolho.kang 2025-03-20 15:14:15 +09:00
parent e6be700ac2
commit 8a4641f6e1
2 changed files with 30 additions and 25 deletions

View File

@ -74,11 +74,15 @@ func (n *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish
} }
// 2. attachdisk // 2. attachdisk
nvmfInfo, err := getNVMfDiskInfo(req) // Create mounter for the volume to be published
parameter := req.GetVolumeContext()
volumeID := req.GetVolumeId()
targetPath := req.GetTargetPath()
nvmfInfo, err := getNVMfDiskInfo(volumeID, parameter)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "NodePublishVolume: get NVMf disk info from req err: %v", err) return nil, status.Errorf(codes.Internal, "NodePublishVolume: get NVMf disk info from req err: %v", err)
} }
diskMounter := getNVMfDiskMounter(nvmfInfo, req) diskMounter := getNVMfDiskMounter(nvmfInfo, targetPath, req.GetVolumeCapability())
// attachDisk realize connect NVMf disk and mount to docker path // attachDisk realize connect NVMf disk and mount to docker path
_, err = AttachDisk(req, *diskMounter) _, err = AttachDisk(req, *diskMounter)
@ -100,7 +104,8 @@ func (n *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpub
return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Staging TargetPath must be provided") return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Staging TargetPath must be provided")
} }
targetPath := req.GetTargetPath() targetPath := req.GetTargetPath()
err := DetachDisk(req.VolumeId, getNVMfDiskUnMounter(req), targetPath) unmounter := getNVMfDiskUnMounter()
err := DetachDisk(req.VolumeId, unmounter, targetPath)
if err != nil { if err != nil {
klog.Errorf("NodeUnpublishVolume: VolumeID: %s detachDisk err: %v", req.VolumeId, err) klog.Errorf("NodeUnpublishVolume: VolumeID: %s detachDisk err: %v", req.VolumeId, err)
return nil, err return nil, err

View File

@ -44,7 +44,7 @@ type nvmfDiskInfo struct {
type nvmfDiskMounter struct { type nvmfDiskMounter struct {
*nvmfDiskInfo *nvmfDiskInfo
readOnly bool isBlock bool
fsType string fsType string
mountOptions []string mountOptions []string
mounter *mount.SafeFormatAndMount mounter *mount.SafeFormatAndMount
@ -58,22 +58,24 @@ type nvmfDiskUnMounter struct {
exec exec.Interface exec exec.Interface
} }
func getNVMfDiskInfo(req *csi.NodePublishVolumeRequest) (*nvmfDiskInfo, error) { // getNVMfDiskInfo extracts NVMf disk information from the provided parameters
volName := req.GetVolumeId() func getNVMfDiskInfo(volID string, params map[string]string) (*nvmfDiskInfo, error) {
if params == nil {
return nil, fmt.Errorf("discovery parameters are nil")
}
volOpts := req.GetVolumeContext() targetTrAddr := params[paramAddr]
targetTrAddr := volOpts[paramAddr] targetTrPort := params[paramPort]
targetTrPort := volOpts[paramPort] targetTrType := params[paramType]
targetTrType := volOpts[paramType] deviceUUID := params["deviceUUID"]
deviceUUID := volOpts["deviceUUID"] nqn := volID
nqn := volOpts["nqn"]
if targetTrAddr == "" || nqn == "" || targetTrPort == "" || targetTrType == "" || deviceUUID == "" { if targetTrAddr == "" || nqn == "" || targetTrPort == "" || targetTrType == "" || deviceUUID == "" {
return nil, fmt.Errorf("some nvme target info is missing, volID: %s ", volName) return nil, fmt.Errorf("some nvme target info is missing, volID: %s ", volID)
} }
return &nvmfDiskInfo{ return &nvmfDiskInfo{
VolName: volName, VolName: volID,
Addr: targetTrAddr, Addr: targetTrAddr,
Port: targetTrPort, Port: targetTrPort,
Nqn: nqn, Nqn: nqn,
@ -82,24 +84,22 @@ func getNVMfDiskInfo(req *csi.NodePublishVolumeRequest) (*nvmfDiskInfo, error) {
}, nil }, nil
} }
func getNVMfDiskMounter(nvmfInfo *nvmfDiskInfo, req *csi.NodePublishVolumeRequest) *nvmfDiskMounter { // getNVMfDiskMounter creates and configures a new disk mounter
readOnly := req.GetReadonly() func getNVMfDiskMounter(nvmfInfo *nvmfDiskInfo, targetPath string, cap *csi.VolumeCapability) *nvmfDiskMounter {
fsType := req.GetVolumeCapability().GetMount().GetFsType()
mountOptions := req.GetVolumeCapability().GetMount().GetMountFlags()
return &nvmfDiskMounter{ return &nvmfDiskMounter{
nvmfDiskInfo: nvmfInfo, nvmfDiskInfo: nvmfInfo,
readOnly: readOnly, isBlock: cap.GetBlock() != nil,
fsType: fsType, fsType: cap.GetMount().GetFsType(),
mountOptions: mountOptions, mountOptions: cap.GetMount().GetMountFlags(),
mounter: &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: exec.New()}, mounter: &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: exec.New()},
exec: exec.New(), exec: exec.New(),
targetPath: req.GetTargetPath(), targetPath: targetPath,
connector: getNvmfConnector(nvmfInfo, req.GetTargetPath()), connector: getNvmfConnector(nvmfInfo, targetPath),
} }
} }
func getNVMfDiskUnMounter(req *csi.NodeUnpublishVolumeRequest) *nvmfDiskUnMounter { // getNVMfDiskUnMounter creates a new disk unmounter
func getNVMfDiskUnMounter() *nvmfDiskUnMounter {
return &nvmfDiskUnMounter{ return &nvmfDiskUnMounter{
mounter: mount.New(""), mounter: mount.New(""),
exec: exec.New(), exec: exec.New(),