Skip to content

Commit

Permalink
Merge pull request #714 from openshift-cherrypick-robot/cherry-pick-7…
Browse files Browse the repository at this point in the history
…12-to-release-4.17

[release-4.17] OCPBUGS-41725: fix: ensure uniq available devices
  • Loading branch information
openshift-merge-bot[bot] authored Sep 11, 2024
2 parents 90e32bb + 9bafe59 commit 32fdaeb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
18 changes: 15 additions & 3 deletions internal/controllers/vgmanager/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,21 @@ func (f FilteredBlockDevices) FilterErrors(dev string) []error {
func filterDevices(ctx context.Context, devices []lsblk.BlockDevice, resolver *symlinkResolver.Resolver, filters filter.Filters) FilteredBlockDevices {
logger := log.FromContext(ctx)

var availableDevices []lsblk.BlockDevice
availableByKName := make(map[string]lsblk.BlockDevice)
excludedByKName := make(map[string]FilteredBlockDevice)

for _, device := range devices {
// check for partitions recursively
if device.HasChildren() {
filteredChildDevices := filterDevices(ctx, device.Children, resolver, filters)
availableDevices = append(availableDevices, filteredChildDevices.Available...)
for _, available := range filteredChildDevices.Available {
if _, exists := availableByKName[available.KName]; exists {
logger.WithValues("device.KName", available.KName).
V(3).Info("skipping duplicate available device, this can happen for multipath / dmsetup devices")
} else {
availableByKName[available.KName] = available
}
}
for _, excludedChildDevice := range filteredChildDevices.Excluded {
if excluded, ok := excludedByKName[excludedChildDevice.KName]; ok {
excluded.FilterErrors = append(excluded.FilterErrors, excludedChildDevice.FilterErrors...)
Expand All @@ -167,7 +174,7 @@ func filterDevices(ctx context.Context, devices []lsblk.BlockDevice, resolver *s
}
}
if len(filterErrs) == 0 {
availableDevices = append(availableDevices, device)
availableByKName[device.KName] = device
continue
}

Expand All @@ -184,6 +191,11 @@ func filterDevices(ctx context.Context, devices []lsblk.BlockDevice, resolver *s
excluded = append(excluded, device)
}

availableDevices := make([]lsblk.BlockDevice, 0, len(availableByKName))
for _, device := range availableByKName {
availableDevices = append(availableDevices, device)
}

return FilteredBlockDevices{
Available: availableDevices,
Excluded: excluded,
Expand Down
53 changes: 53 additions & 0 deletions internal/controllers/vgmanager/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Test_getNewDevicesToBeAdded(t *testing.T) {
devicePaths = make(map[string]v1alpha1.DevicePath)
devicePaths["nvme1n1p1"] = v1alpha1.DevicePath(fmt.Sprintf("%s/%s", tmpDir, "nvme1n1p1"))
devicePaths["nvme1n1p2"] = v1alpha1.DevicePath(fmt.Sprintf("%s/%s", tmpDir, "nvme1n1p2"))
devicePaths["md1"] = v1alpha1.DevicePath(fmt.Sprintf("%s/%s", tmpDir, "md1"))
for _, path := range devicePaths {
err := os.Mkdir(path.Unresolved(), 0755)
if err != nil {
Expand Down Expand Up @@ -588,6 +589,58 @@ func Test_getNewDevicesToBeAdded(t *testing.T) {
},
numOfAvailableDevices: 0,
},
{
description: "vg RAID with multiple paths under different devices",
volumeGroup: v1alpha1.LVMVolumeGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "vg1",
},
Spec: v1alpha1.LVMVolumeGroupSpec{
DeviceSelector: &v1alpha1.DeviceSelector{
Paths: []v1alpha1.DevicePath{
devicePaths["md1"],
},
},
},
},
existingBlockDevices: []lsblk.BlockDevice{
{
Name: "nvme1n1p1",
Type: "disk",
Size: "50G",
ReadOnly: false,
State: "live",
KName: calculateDevicePath(t, "nvme1n1p1"),
Serial: "vol071204a42d92d52a2",
FSType: "linux_raid_member",
Children: []lsblk.BlockDevice{{
Name: "/dev/md1",
Type: "raid1",
Size: "50G",
ReadOnly: false,
KName: calculateDevicePath(t, "md1"),
}},
},
{
Name: "nvme1n1p2",
Type: "disk",
Size: "50G",
ReadOnly: false,
State: "live",
KName: calculateDevicePath(t, "nvme1n1p2"),
Serial: "vol07993aa64fe3c5316",
FSType: "linux_raid_member",
Children: []lsblk.BlockDevice{{
Name: "/dev/md1",
Type: "raid1",
Size: "50G",
ReadOnly: false,
KName: calculateDevicePath(t, "md1"),
}},
},
},
numOfAvailableDevices: 1,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 32fdaeb

Please sign in to comment.