Skip to content

Commit c2d2372

Browse files
authored
Devices utils refactor
Refactor device utils into its own pacakge
1 parent 746c138 commit c2d2372

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+5397
-5386
lines changed

core/orchestrator_core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type TridentOrchestrator struct {
111111
func NewTridentOrchestrator(client persistentstore.Client) (*TridentOrchestrator, error) {
112112
// TODO (vivintw) the adaptors are being plugged in here as a temporary measure to prevent cyclic dependencies.
113113
// NewClient() must plugin default implementation of the various package clients.
114-
iscsiClient, err := iscsi.New(utils.NewOSClient(), utils.NewDevicesClient())
114+
iscsiClient, err := iscsi.New(utils.NewOSClient())
115115
if err != nil {
116116
return nil, err
117117
}
@@ -121,7 +121,7 @@ func NewTridentOrchestrator(client persistentstore.Client) (*TridentOrchestrator
121121
return nil, err
122122
}
123123

124-
fcpClent, err := fcp.New(utils.NewOSClient(), utils.NewDevicesClient(), filesystem.New(mountClient))
124+
fcpClent, err := fcp.New(utils.NewOSClient(), filesystem.New(mountClient))
125125
if err != nil {
126126
return nil, err
127127
}

frontend/csi/controller_helpers/kubernetes/import.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
. "github.com/netapp/trident/logging"
2121
"github.com/netapp/trident/storage"
2222
"github.com/netapp/trident/utils"
23+
"github.com/netapp/trident/utils/devices/luks"
2324
"github.com/netapp/trident/utils/errors"
2425
)
2526

@@ -115,7 +116,7 @@ func (h *helper) ImportVolume(
115116
// LUKS annotation should be accepted as either "LUKSEncryption" or "luksEncryption" to match storage pools.
116117
if luksAnnotation := getCaseFoldedAnnotation(claim.GetObjectMeta().GetAnnotations(), AnnLUKSEncryption); luksAnnotation != "" {
117118
if utils.ParseBool(luksAnnotation) {
118-
dataSize -= utils.LUKSMetadataSize
119+
dataSize -= luks.LUKSMetadataSize
119120
}
120121
}
121122

frontend/csi/node_server.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math/big"
1010
"os"
1111
"path"
12+
"path/filepath"
1213
"runtime/debug"
1314
"strconv"
1415
"strings"
@@ -25,6 +26,8 @@ import (
2526
. "github.com/netapp/trident/logging"
2627
sa "github.com/netapp/trident/storage_attribute"
2728
"github.com/netapp/trident/utils"
29+
"github.com/netapp/trident/utils/devices"
30+
"github.com/netapp/trident/utils/devices/luks"
2831
"github.com/netapp/trident/utils/errors"
2932
"github.com/netapp/trident/utils/fcp"
3033
"github.com/netapp/trident/utils/filesystem"
@@ -499,8 +502,8 @@ func (p *Plugin) nodeExpandVolume(
499502

500503
devicePath := publishInfo.DevicePath
501504
if utils.ParseBool(publishInfo.LUKSEncryption) {
502-
if !utils.IsLegacyLUKSDevicePath(devicePath) {
503-
devicePath, err = utils.GetLUKSDeviceForMultipathDevice(devicePath)
505+
if !luks.IsLegacyLUKSDevicePath(devicePath) {
506+
devicePath, err = p.devices.GetLUKSDeviceForMultipathDevice(devicePath)
504507
if err != nil {
505508
Logc(ctx).WithFields(LogFields{
506509
"volumeId": volumeId,
@@ -520,7 +523,8 @@ func (p *Plugin) nodeExpandVolume(
520523
return status.Error(codes.InvalidArgument, "cannot expand LUKS encrypted volume; empty passphrase provided")
521524
}
522525

523-
if err := utils.ResizeLUKSDevice(ctx, devicePath, passphrase); err != nil {
526+
luksDevice := luks.NewLUKSDevice("", filepath.Base(devicePath), p.command)
527+
if err := luksDevice.Resize(ctx, passphrase); err != nil {
524528
if errors.IsIncorrectLUKSPassphraseError(err) {
525529
return status.Error(codes.InvalidArgument, err.Error())
526530
}
@@ -1174,9 +1178,9 @@ func (p *Plugin) nodeStageFCPVolume(
11741178
}
11751179

11761180
if isLUKS {
1177-
var luksDevice models.LUKSDeviceInterface
1178-
luksDevice, err = utils.NewLUKSDeviceFromMappingPath(
1179-
ctx, publishInfo.DevicePath,
1181+
var luksDevice luks.Device
1182+
luksDevice, err = luks.NewLUKSDeviceFromMappingPath(
1183+
ctx, p.command, publishInfo.DevicePath,
11801184
req.VolumeContext["internalName"],
11811185
)
11821186
if err != nil {
@@ -1231,7 +1235,8 @@ func (p *Plugin) ensureAttachFCPVolume(
12311235
func (p *Plugin) nodeUnstageFCPVolume(
12321236
ctx context.Context, req *csi.NodeUnstageVolumeRequest, publishInfo *models.VolumePublishInfo, force bool,
12331237
) error {
1234-
deviceInfo, err := utils.GetDeviceInfoForFCPLUN(ctx, nil, int(publishInfo.FCPLunNumber), publishInfo.FCTargetWWNN, false)
1238+
deviceInfo, err := utils.GetDeviceInfoForFCPLUN(ctx, nil, int(publishInfo.FCPLunNumber), publishInfo.FCTargetWWNN,
1239+
false)
12351240
if err != nil {
12361241
return fmt.Errorf("could not get device info: %v", err)
12371242
}
@@ -1246,14 +1251,14 @@ func (p *Plugin) nodeUnstageFCPVolume(
12461251
fields := LogFields{"luksDevicePath": publishInfo.DevicePath, "lunID": publishInfo.FCPLunNumber}
12471252

12481253
// Before closing the LUKS device, get the underlying mapper device from cryptsetup.
1249-
mapperPath, err := utils.GetUnderlyingDevicePathForLUKSDevice(ctx, publishInfo.DevicePath)
1254+
mapperPath, err := luks.GetUnderlyingDevicePathForLUKSDevice(ctx, p.command, publishInfo.DevicePath)
12501255
if err != nil {
12511256
// No need to return an error
12521257
Logc(ctx).WithFields(fields).WithError(err).Error("Could not determine underlying device for LUKS.")
12531258
}
12541259
fields["mapperPath"] = mapperPath
12551260

1256-
err = utils.EnsureLUKSDeviceClosedWithMaxWaitLimit(ctx, publishInfo.DevicePath)
1261+
err = p.devices.EnsureLUKSDeviceClosedWithMaxWaitLimit(ctx, publishInfo.DevicePath)
12571262
if err != nil {
12581263
if errors.IsMaxWaitExceededError(err) {
12591264
Logc(ctx).WithFields(LogFields{
@@ -1278,11 +1283,13 @@ func (p *Plugin) nodeUnstageFCPVolume(
12781283
}
12791284

12801285
// Delete the device from the host.
1281-
unmappedMpathDevice, err := utils.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach, force)
1286+
unmappedMpathDevice, err := p.iscsi.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach,
1287+
force)
12821288
if err != nil {
12831289
if errors.IsFCPSameLunNumberError(err) {
12841290
// There is a need to pass all the publish infos this time
1285-
unmappedMpathDevice, err = utils.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, p.readAllTrackingFiles(ctx),
1291+
unmappedMpathDevice, err = p.iscsi.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo,
1292+
p.readAllTrackingFiles(ctx),
12861293
p.unsafeDetach, force)
12871294
}
12881295

@@ -1312,17 +1319,17 @@ func (p *Plugin) nodeUnstageFCPVolume(
13121319
// It needs to be removed prior to removing the 'unmappedMpathDevice' device below.
13131320
if luksMapperPath != "" {
13141321
// EnsureLUKSDeviceClosed will not return an error if the device is already closed or removed.
1315-
if err = utils.EnsureLUKSDeviceClosed(ctx, luksMapperPath); err != nil {
1322+
if err = p.devices.EnsureLUKSDeviceClosed(ctx, luksMapperPath); err != nil {
13161323
Logc(ctx).WithFields(LogFields{
13171324
"devicePath": luksMapperPath,
13181325
}).WithError(err).Warning("Unable to remove LUKS mapper device.")
13191326
}
13201327
// Clear the time duration for the LUKS device.
1321-
delete(utils.LuksCloseDurations, luksMapperPath)
1328+
delete(devices.LuksCloseDurations, luksMapperPath)
13221329
}
13231330

13241331
// If there is multipath device, flush(remove) mappings
1325-
if err := utils.RemoveMultipathDeviceMapping(ctx, unmappedMpathDevice); err != nil {
1332+
if err := p.devices.RemoveMultipathDeviceMapping(ctx, unmappedMpathDevice); err != nil {
13261333
return err
13271334
}
13281335

@@ -1384,7 +1391,7 @@ func (p *Plugin) nodePublishFCPVolume(
13841391

13851392
if utils.ParseBool(publishInfo.LUKSEncryption) {
13861393
// Rotate the LUKS passphrase if needed, on failure, log and continue to publish
1387-
luksDevice, err := utils.NewLUKSDeviceFromMappingPath(ctx, publishInfo.DevicePath,
1394+
luksDevice, err := luks.NewLUKSDeviceFromMappingPath(ctx, p.command, publishInfo.DevicePath,
13881395
req.VolumeContext["internalName"])
13891396
if err != nil {
13901397
return nil, status.Error(codes.Internal, err.Error())
@@ -1540,11 +1547,8 @@ func (p *Plugin) nodeStageISCSIVolume(
15401547
return err
15411548
}
15421549

1543-
var luksDevice models.LUKSDeviceInterface
1544-
luksDevice, err = p.devices.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"])
1545-
if err != nil {
1546-
return err
1547-
}
1550+
var luksDevice luks.Device
1551+
luksDevice = luks.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"], p.command)
15481552

15491553
// Ensure we update the passphrase in case it has never been set before
15501554
err = ensureLUKSVolumePassphrase(ctx, p.restClient, luksDevice, volumeId, req.GetSecrets(), true)
@@ -1634,7 +1638,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
16341638
return nil
16351639
}
16361640

1637-
deviceInfo, err := p.devices.GetDeviceInfoForLUN(ctx, hostSessionMap, int(publishInfo.IscsiLunNumber),
1641+
deviceInfo, err := p.iscsi.GetDeviceInfoForLUN(ctx, hostSessionMap, int(publishInfo.IscsiLunNumber),
16381642
publishInfo.IscsiTargetIQN, false)
16391643
if err != nil {
16401644
Logc(ctx).WithError(err).Debug("Could not find devices.")
@@ -1679,17 +1683,18 @@ func (p *Plugin) nodeUnstageISCSIVolume(
16791683
}
16801684

16811685
// Set device path to dm device to correctly verify legacy volumes.
1682-
if utils.IsLegacyLUKSDevicePath(publishInfo.DevicePath) {
1686+
if luks.IsLegacyLUKSDevicePath(publishInfo.DevicePath) {
16831687
publishInfo.DevicePath = deviceInfo.MultipathDevice
16841688
}
16851689
}
16861690

16871691
// Delete the device from the host.
1688-
unmappedMpathDevice, err := p.devices.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach, force)
1692+
unmappedMpathDevice, err := p.iscsi.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo, nil, p.unsafeDetach,
1693+
force)
16891694
if err != nil {
16901695
if errors.IsISCSISameLunNumberError(err) {
16911696
// There is a need to pass all the publish infos this time
1692-
unmappedMpathDevice, err = p.devices.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo,
1697+
unmappedMpathDevice, err = p.iscsi.PrepareDeviceForRemoval(ctx, deviceInfo, publishInfo,
16931698
p.readAllTrackingFiles(ctx),
16941699
p.unsafeDetach, force)
16951700
}
@@ -1768,7 +1773,7 @@ func (p *Plugin) nodeUnstageISCSIVolume(
17681773
}).WithError(err).Warning("Unable to remove LUKS mapper device.")
17691774
}
17701775
// Clear the time duration for the LUKS device.
1771-
utils.LuksCloseDurations.RemoveDurationTracking(luksMapperPath)
1776+
devices.LuksCloseDurations.RemoveDurationTracking(luksMapperPath)
17721777
}
17731778

17741779
// If there is multipath device, flush(remove) mappings
@@ -1834,14 +1839,14 @@ func (p *Plugin) nodePublishISCSIVolume(
18341839
devicePath := publishInfo.DevicePath
18351840
if utils.ParseBool(publishInfo.LUKSEncryption) {
18361841
// Rotate the LUKS passphrase if needed, on failure, log and continue to publish
1837-
var luksDevice models.LUKSDeviceInterface
1842+
var luksDevice luks.Device
18381843
var err error
1839-
if utils.IsLegacyLUKSDevicePath(devicePath) {
1844+
if luks.IsLegacyLUKSDevicePath(devicePath) {
18401845
// Supports legacy volumes that store the LUKS device path
1841-
luksDevice, err = p.devices.NewLUKSDeviceFromMappingPath(ctx, devicePath,
1846+
luksDevice, err = luks.NewLUKSDeviceFromMappingPath(ctx, p.command, devicePath,
18421847
req.VolumeContext["internalName"])
18431848
} else {
1844-
luksDevice, err = p.devices.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"])
1849+
luksDevice = luks.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"], p.command)
18451850
}
18461851

18471852
if err != nil {
@@ -2469,10 +2474,7 @@ func (p *Plugin) nodeStageNVMeVolume(
24692474
}
24702475

24712476
if isLUKS {
2472-
luksDevice, err := p.devices.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"])
2473-
if err != nil {
2474-
return err
2475-
}
2477+
luksDevice := luks.NewLUKSDevice(publishInfo.DevicePath, req.VolumeContext["internalName"], p.command)
24762478

24772479
// Ensure we update the passphrase in case it has never been set before
24782480
err = ensureLUKSVolumePassphrase(ctx, p.restClient, luksDevice, volumeId, req.GetSecrets(), true)
@@ -2620,7 +2622,7 @@ func (p *Plugin) nodeUnstageNVMeVolume(
26202622
}).WithError(err).Warning("Unable to remove LUKS mapper device.")
26212623
}
26222624
// Clear the time duration for the LUKS device.
2623-
utils.LuksCloseDurations.RemoveDurationTracking(luksMapperPath)
2625+
devices.LuksCloseDurations.RemoveDurationTracking(luksMapperPath)
26242626
}
26252627

26262628
// Delete the device info we saved to the volume tracking info path so unstage can succeed.
@@ -2654,10 +2656,7 @@ func (p *Plugin) nodePublishNVMeVolume(
26542656
devicePath := publishInfo.DevicePath
26552657
if utils.ParseBool(publishInfo.LUKSEncryption) {
26562658
// Rotate the LUKS passphrase if needed, on failure, log and continue to publish
2657-
luksDevice, err := p.devices.NewLUKSDevice(devicePath, req.VolumeContext["internalName"])
2658-
if err != nil {
2659-
return nil, status.Error(codes.Internal, err.Error())
2660-
}
2659+
luksDevice := luks.NewLUKSDevice(devicePath, req.VolumeContext["internalName"], p.command)
26612660

26622661
err = ensureLUKSVolumePassphrase(ctx, p.restClient, luksDevice, req.GetVolumeId(), req.GetSecrets(), false)
26632662
if err != nil {

0 commit comments

Comments
 (0)