Skip to content

Commit

Permalink
Filesystem code encapsulation
Browse files Browse the repository at this point in the history
Refactor filesystem utils into its own package
  • Loading branch information
jharrod committed Nov 4, 2024
1 parent 30f4726 commit 5744fbe
Show file tree
Hide file tree
Showing 52 changed files with 1,476 additions and 961 deletions.
6 changes: 0 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,6 @@ const (
RawBlock VolumeMode = "Block"
Filesystem VolumeMode = "Filesystem"

// Filesystem types
FsXfs = "xfs"
FsExt3 = "ext3"
FsExt4 = "ext4"
FsRaw = "raw"

/* Volume type constants */
OntapNFS VolumeType = "ONTAP_NFS"
OntapISCSI VolumeType = "ONTAP_iSCSI"
Expand Down
11 changes: 7 additions & 4 deletions core/orchestrator_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/fcp"
"github.com/netapp/trident/utils/filesystem"
"github.com/netapp/trident/utils/iscsi"
"github.com/netapp/trident/utils/models"
"github.com/netapp/trident/utils/mount"
Expand Down Expand Up @@ -101,6 +102,7 @@ type TridentOrchestrator struct {
stopReconcileBackendLoop chan bool
uuid string
iscsi iscsi.ISCSI
fs filesystem.Filesystem
fcp fcp.FCP
mount mount.Mount
}
Expand All @@ -109,7 +111,7 @@ type TridentOrchestrator struct {
func NewTridentOrchestrator(client persistentstore.Client) (*TridentOrchestrator, error) {
// TODO (vivintw) the adaptors are being plugged in here as a temporary measure to prevent cyclic dependencies.
// NewClient() must plugin default implementation of the various package clients.
iscsiClient, err := iscsi.New(utils.NewOSClient(), utils.NewDevicesClient(), utils.NewFilesystemClient())
iscsiClient, err := iscsi.New(utils.NewOSClient(), utils.NewDevicesClient())
if err != nil {
return nil, err
}
Expand All @@ -119,7 +121,7 @@ func NewTridentOrchestrator(client persistentstore.Client) (*TridentOrchestrator
return nil, err
}

fcpClent, err := fcp.New(utils.NewOSClient(), utils.NewDevicesClient(), utils.NewFilesystemClient())
fcpClent, err := fcp.New(utils.NewOSClient(), utils.NewDevicesClient(), filesystem.New(mountClient))
if err != nil {
return nil, err
}
Expand All @@ -140,6 +142,7 @@ func NewTridentOrchestrator(client persistentstore.Client) (*TridentOrchestrator
iscsi: iscsiClient,
fcp: fcpClent,
mount: mountClient,
fs: filesystem.New(mountClient),
}, nil
}

Expand Down Expand Up @@ -2670,7 +2673,7 @@ func (o *TridentOrchestrator) validateImportVolume(ctx context.Context, volumeCo

// Make sure that for the Raw-block volume import we do not have ext3, ext4 or xfs filesystem specified
if volumeConfig.VolumeMode == config.RawBlock {
if volumeConfig.FileSystem != "" && volumeConfig.FileSystem != config.FsRaw {
if volumeConfig.FileSystem != "" && volumeConfig.FileSystem != filesystem.Raw {
return fmt.Errorf("cannot create raw-block volume %s with the filesystem %s",
originalName, volumeConfig.FileSystem)
}
Expand Down Expand Up @@ -3621,7 +3624,7 @@ func (o *TridentOrchestrator) AttachVolume(
}

// Check if volume is already mounted
dfOutput, dfOuputErr := utils.GetDFOutput(ctx)
dfOutput, dfOuputErr := o.fs.GetDFOutput(ctx)
if dfOuputErr != nil {
err = fmt.Errorf("error checking if %v is already mounted: %v", mountpoint, dfOuputErr)
return err
Expand Down
3 changes: 2 additions & 1 deletion frontend/csi/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
sa "github.com/netapp/trident/storage_attribute"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/filesystem"
"github.com/netapp/trident/utils/models"
)

Expand Down Expand Up @@ -161,7 +162,7 @@ func (p *Plugin) CreateVolume(
"raw block volumes are not supported for this container orchestrator")
}
volumeMode = tridentconfig.RawBlock
fsType = tridentconfig.FsRaw
fsType = filesystem.Raw
}

for _, csiAccessMode := range csiAccessModes {
Expand Down
7 changes: 6 additions & 1 deletion frontend/csi/node_helpers/kubernetes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ func NewHelper(orchestrator core.Orchestrator, kubeConfigPath string, enableForc
return nil, fmt.Errorf("could not initialize mount client; %v", err)
}

publishManager, err := csi.NewVolumePublishManager()
if err != nil {
return nil, fmt.Errorf("could not initialize VolumePublishManager; %v", err)
}

h := &helper{
orchestrator: orchestrator,
podsPath: kubeConfigPath + "/pods",
kubeConfigPath: kubeConfigPath,
publishedPaths: make(map[string]map[string]struct{}),
enableForceDetach: enableForceDetach,
mount: mountClient,
VolumePublishManager: csi.NewVolumePublishManager(config.VolumeTrackingInfoPath),
VolumePublishManager: publishManager,
}

return h, nil
Expand Down
12 changes: 8 additions & 4 deletions frontend/csi/node_helpers/plain/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package plain

import (
"context"
"fmt"

"github.com/netapp/trident/config"
"github.com/netapp/trident/core"
Expand All @@ -19,17 +20,20 @@ type helper struct {
}

// NewHelper instantiates this plugin.
func NewHelper(orchestrator core.Orchestrator) frontend.Plugin {
func NewHelper(orchestrator core.Orchestrator) (frontend.Plugin, error) {
ctx := GenerateRequestContext(nil, "", ContextSourceInternal, WorkflowPluginCreate, LogLayerCSIFrontend)

Logc(ctx).Info("Initializing plain CSI helper frontend.")

volPubManager := csi.NewVolumePublishManager("")
publishManager, err := csi.NewVolumePublishManager()
if err != nil {
return nil, fmt.Errorf("could not initialize VolumePublishManager; %v", err)
}

return &helper{
orchestrator: orchestrator,
VolumePublishManager: volPubManager,
}
VolumePublishManager: publishManager,
}, nil
}

// Activate starts this Trident frontend.
Expand Down
6 changes: 3 additions & 3 deletions frontend/csi/node_helpers/plain/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,21 @@ func TestRemovePublishedPath_Succeeds(t *testing.T) {
func TestNewHelper(t *testing.T) {
mockCtrl := gomock.NewController(t)
orchestrator := mockOrchestrator.NewMockOrchestrator(mockCtrl)
h := NewHelper(orchestrator)
h, _ := NewHelper(orchestrator)
assert.NotNilf(t, h, "expected helper to not be nil")
}

func TestActivate(t *testing.T) {
mockCtrl := gomock.NewController(t)
orchestrator := mockOrchestrator.NewMockOrchestrator(mockCtrl)
h := NewHelper(orchestrator)
h, _ := NewHelper(orchestrator)
err := h.Activate()
assert.NoError(t, err)
}

func TestVersion(t *testing.T) {
mockCtrl := gomock.NewController(t)
orchestrator := mockOrchestrator.NewMockOrchestrator(mockCtrl)
h := NewHelper(orchestrator)
h, _ := NewHelper(orchestrator)
assert.Equal(t, csi.Version, h.Version())
}
45 changes: 23 additions & 22 deletions frontend/csi/node_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/fcp"
"github.com/netapp/trident/utils/filesystem"
"github.com/netapp/trident/utils/iscsi"
"github.com/netapp/trident/utils/models"
)
Expand Down Expand Up @@ -355,14 +356,14 @@ func (p *Plugin) NodeGetVolumeStats(
}
publishInfo := &trackingInfo.VolumePublishInfo

isRawBlock = publishInfo.FilesystemType == tridentconfig.FsRaw
isRawBlock = publishInfo.FilesystemType == filesystem.Raw
}
if isRawBlock {
// Return no capacity info for raw block volumes, we cannot reliably determine the capacity.
return &csi.NodeGetVolumeStatsResponse{}, nil
} else {
// If filesystem, return usage reported by FS.
available, capacity, usage, inodes, inodesFree, inodesUsed, err := utils.GetFilesystemStats(
available, capacity, usage, inodes, inodesFree, inodesUsed, err := p.fs.GetFilesystemStats(
ctx, req.GetVolumePath())
if err != nil {
Logc(ctx).Errorf("unable to get filesystem stats at path: %s; %v", req.GetVolumePath(), err)
Expand Down Expand Up @@ -470,7 +471,7 @@ func (p *Plugin) nodeExpandVolume(
Logc(ctx).WithField("volumeId", volumeId).Info("Filesystem expansion check is not required.")
return nil
case tridentconfig.Block:
if fsType, err = utils.VerifyFilesystemSupport(publishInfo.FilesystemType); err != nil {
if fsType, err = filesystem.VerifyFilesystemSupport(publishInfo.FilesystemType); err != nil {
break
}
// We don't need to rescan mount devices for NVMe protocol backend. Automatic namespace rescanning happens
Expand Down Expand Up @@ -523,8 +524,8 @@ func (p *Plugin) nodeExpandVolume(
}

// Expand filesystem.
if fsType != tridentconfig.FsRaw {
filesystemSize, err := utils.ExpandFilesystemOnNode(ctx, publishInfo, devicePath, stagingTargetPath, fsType,
if fsType != filesystem.Raw {
filesystemSize, err := p.fs.ExpandFilesystemOnNode(ctx, publishInfo, devicePath, stagingTargetPath, fsType,
mountOptions)
if err != nil {
Logc(ctx).WithFields(LogFields{
Expand Down Expand Up @@ -983,7 +984,7 @@ func (p *Plugin) nodeUnstageSMBVolume(
return &csi.NodeUnstageVolumeResponse{}, err
}

mappingPath, err = utils.GetUnmountPath(ctx, trackingInfo)
mappingPath, err = p.fs.GetUnmountPath(ctx, trackingInfo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1389,7 +1390,7 @@ func (p *Plugin) nodePublishFCPVolume(
}
}

if publishInfo.FilesystemType == tridentconfig.FsRaw {
if publishInfo.FilesystemType == filesystem.Raw {

if len(publishInfo.MountOptions) > 0 {
publishInfo.MountOptions = utils.AppendToStringList(publishInfo.MountOptions, "bind", ",")
Expand Down Expand Up @@ -1628,7 +1629,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
return nil
}

deviceInfo, err := p.deviceClient.GetDeviceInfoForLUN(ctx, hostSessionMap, int(publishInfo.IscsiLunNumber),
deviceInfo, err := p.devices.GetDeviceInfoForLUN(ctx, hostSessionMap, int(publishInfo.IscsiLunNumber),
publishInfo.IscsiTargetIQN, false)
if err != nil {
Logc(ctx).WithError(err).Debug("Could not find devices.")
Expand All @@ -1650,7 +1651,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
"multipathDevice": deviceInfo.MultipathDevice,
}

luksMapperPath, err = p.deviceClient.GetLUKSDeviceForMultipathDevice(deviceInfo.MultipathDevice)
luksMapperPath, err = p.devices.GetLUKSDeviceForMultipathDevice(deviceInfo.MultipathDevice)
if err != nil {
if !errors.IsNotFoundError(err) {
Logc(ctx).WithFields(fields).WithError(err).Error("Failed to get LUKS device path from multipath device.")
Expand All @@ -1662,7 +1663,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
// Ensure the LUKS device is closed if the luksMapperPath is set.
if luksMapperPath != "" {
fields["luksDevice"] = luksMapperPath
err = p.deviceClient.EnsureLUKSDeviceClosedWithMaxWaitLimit(ctx, luksMapperPath)
err = p.devices.EnsureLUKSDeviceClosedWithMaxWaitLimit(ctx, luksMapperPath)
if err != nil {
if !errors.IsMaxWaitExceededError(err) {
Logc(ctx).WithFields(fields).WithError(err).Error("Failed to close LUKS device.")
Expand All @@ -1679,11 +1680,11 @@ func (p *Plugin) nodeUnstageISCSIVolume(
}

// Delete the device from the host.
unmappedMpathDevice, err := p.deviceClient.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach, force)
unmappedMpathDevice, err := p.devices.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach, force)
if err != nil {
if errors.IsISCSISameLunNumberError(err) {
// There is a need to pass all the publish infos this time
unmappedMpathDevice, err = p.deviceClient.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo,
unmappedMpathDevice, err = p.devices.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo,
p.readAllTrackingFiles(ctx),
p.unsafeDetach, force)
}
Expand Down Expand Up @@ -1756,7 +1757,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
// It needs to be removed prior to removing the 'unmappedMpathDevice' device below.
if luksMapperPath != "" {
// EnsureLUKSDeviceClosed will not return an error if the device is already closed or removed.
if err = p.deviceClient.EnsureLUKSDeviceClosed(ctx, luksMapperPath); err != nil {
if err = p.devices.EnsureLUKSDeviceClosed(ctx, luksMapperPath); err != nil {
Logc(ctx).WithFields(LogFields{
"devicePath": luksMapperPath,
}).WithError(err).Warning("Unable to remove LUKS mapper device.")
Expand All @@ -1766,7 +1767,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
}

// If there is multipath device, flush(remove) mappings
if err := p.deviceClient.RemoveMultipathDeviceMapping(ctx, unmappedMpathDevice); err != nil {
if err := p.devices.RemoveMultipathDeviceMapping(ctx, unmappedMpathDevice); err != nil {
return err
}

Expand Down Expand Up @@ -1832,7 +1833,7 @@ func (p *Plugin) nodePublishISCSIVolume(
var err error
if utils.IsLegacyLUKSDevicePath(devicePath) {
// Supports legacy volumes that store the LUKS device path
luksDevice, err = p.deviceClient.NewLUKSDeviceFromMappingPath(ctx, devicePath,
luksDevice, err = p.devices.NewLUKSDeviceFromMappingPath(ctx, devicePath,
req.VolumeContext["internalName"])
} else {
luksDevice, err = utils.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"])
Expand All @@ -1848,7 +1849,7 @@ func (p *Plugin) nodePublishISCSIVolume(
// Mount LUKS device instead of mpath.
devicePath = luksDevice.MappedDevicePath()
}
isRawBlock := publishInfo.FilesystemType == tridentconfig.FsRaw
isRawBlock := publishInfo.FilesystemType == filesystem.Raw
if isRawBlock {

if len(publishInfo.MountOptions) > 0 {
Expand Down Expand Up @@ -2179,7 +2180,7 @@ func (p *Plugin) selfHealingRectifySession(ctx context.Context, portal string, a
case models.LoginScan:
// Set FilesystemType to "raw" so that we only heal the session connectivity and not perform the mount and
// filesystem related operations.
publishInfo.FilesystemType = tridentconfig.FsRaw
publishInfo.FilesystemType = filesystem.Raw

volumeID, err := publishedISCSISessions.VolumeIDForPortalAndLUN(portal, lunID)
if err != nil {
Expand Down Expand Up @@ -2459,7 +2460,7 @@ func (p *Plugin) nodeStageNVMeVolume(
}

if isLUKS {
luksDevice, err := p.deviceClient.NewLUKSDeviceFromMappingPath(ctx, publishInfo.DevicePath,
luksDevice, err := p.devices.NewLUKSDeviceFromMappingPath(ctx, publishInfo.DevicePath,
req.VolumeContext["internalName"])
if err != nil {
return err
Expand Down Expand Up @@ -2609,7 +2610,7 @@ func (p *Plugin) nodePublishNVMeVolume(

if utils.ParseBool(publishInfo.LUKSEncryption) {
// Rotate the LUKS passphrase if needed, on failure, log and continue to publish
luksDevice, err := p.deviceClient.NewLUKSDeviceFromMappingPath(ctx, publishInfo.DevicePath,
luksDevice, err := p.devices.NewLUKSDeviceFromMappingPath(ctx, publishInfo.DevicePath,
req.VolumeContext["internalName"])
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -2620,7 +2621,7 @@ func (p *Plugin) nodePublishNVMeVolume(
}
}

isRawBlock := publishInfo.FilesystemType == tridentconfig.FsRaw
isRawBlock := publishInfo.FilesystemType == filesystem.Raw
if isRawBlock {

if len(publishInfo.MountOptions) > 0 {
Expand Down Expand Up @@ -2667,11 +2668,11 @@ func (p *Plugin) nodeStageSANVolume(
fsType = mountCapability.GetFsType()
}

if fsType == tridentconfig.FsRaw && mountCapability != nil {
if fsType == filesystem.Raw && mountCapability != nil {
return nil, status.Error(codes.InvalidArgument, "mount capability requested with raw blocks")
}

if fsType != tridentconfig.FsRaw && blockCapability != nil {
if fsType != filesystem.Raw && blockCapability != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("block capability requested with %s", fsType))
}

Expand Down
Loading

0 comments on commit 5744fbe

Please sign in to comment.