Skip to content

Commit

Permalink
Merge pull request #259 from iamniting/device
Browse files Browse the repository at this point in the history
fix: allow all kind of paths
  • Loading branch information
openshift-merge-robot authored Sep 13, 2022
2 parents 7e461a4 + 0ef6805 commit 7ecdee5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 81 deletions.
5 changes: 2 additions & 3 deletions pkg/internal/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ type BlockDevice struct {
Serial string `json:"serial,omitempty"`
PartLabel string `json:"partLabel,omitempty"`

// DiskByPath is not part of lsblk output
// fetch and set it only if specified in the CR
DiskByPath string
// DevicePath is the path given by user
DevicePath string
}

// ListBlockDevices using the lsblk command
Expand Down
88 changes: 10 additions & 78 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ func (r *VGReconciler) addDevicesToVG(vgName string, devices []internal.BlockDev

args := []string{vgName}
for _, device := range devices {
if device.DiskByPath != "" {
args = append(args, device.DiskByPath)
if device.DevicePath != "" {
args = append(args, device.DevicePath)
} else {
args = append(args, device.KName)
}
Expand Down Expand Up @@ -465,28 +465,13 @@ func (r *VGReconciler) filterMatchingDevices(blockDevices []internal.BlockDevice
}

blockDevice, ok := hasExactDisk(blockDevices, diskName)

if filepath.Dir(path) == internal.DiskByPathPrefix {
// handle disk by path here such as /dev/disk/by-path/pci-0000:87:00.0-nvme-1
if ok {
blockDevice.DiskByPath = path
filteredBlockDevices = append(filteredBlockDevices, blockDevice)
} else {
err = fmt.Errorf("can not find device path %s, device name %s in the available block devices", path, diskName)
return []internal.BlockDevice{}, err
}
} else if filepath.Dir(path) == internal.DiskByNamePrefix {
// handle disk by names here such as /dev/nvme0n1
if ok {
filteredBlockDevices = append(filteredBlockDevices, blockDevice)
} else {
err := fmt.Errorf("can not find device name %s in the available block devices", path)
return []internal.BlockDevice{}, err
}
} else {
err = fmt.Errorf("unsupported disk path format %s. only '/dev/disk/by-path' and '/dev/' links are currently supported", path)
if !ok {
err := fmt.Errorf("can not find device name %s in the available block devices", path)
return []internal.BlockDevice{}, err
}

blockDevice.DevicePath = path
filteredBlockDevices = append(filteredBlockDevices, blockDevice)
}

return filteredBlockDevices, nil
Expand Down Expand Up @@ -598,8 +583,7 @@ func (r *VGReconciler) getMatchingDevicesForVG(volumeGroup *lvmv1alpha1.LVMVolum
return matchingDevices, delayedDevices, nil
}

func (r *VGReconciler) generateVolumeGroupNodeStatus(deviceNameAndPaths map[string]string,
lvmVolumeGroups map[string]*lvmv1alpha1.LVMVolumeGroup, vgStatus *lvmv1alpha1.VGStatus) (*lvmv1alpha1.LVMVolumeGroupNodeStatus, error) {
func (r *VGReconciler) generateVolumeGroupNodeStatus(vgStatus *lvmv1alpha1.VGStatus) (*lvmv1alpha1.LVMVolumeGroupNodeStatus, error) {

vgs, err := ListVolumeGroups(r.executor)
if err != nil {
Expand All @@ -612,33 +596,9 @@ func (r *VGReconciler) generateVolumeGroupNodeStatus(deviceNameAndPaths map[stri
var vgExists bool

for _, vg := range vgs {
// Add pvs as per volumeGroup CR if path is given add path else add name
diskPattern := internal.DiskByNamePrefix

lvmVolumeGroup, ok := lvmVolumeGroups[vg.Name]
if !ok {
continue
}

deviceSelector := lvmVolumeGroup.Spec.DeviceSelector
if deviceSelector != nil && len(deviceSelector.Paths) > 0 {
if filepath.Dir(deviceSelector.Paths[0]) == internal.DiskByPathPrefix {
diskPattern = internal.DiskByPathPrefix
}
}

devices := []string{}
if diskPattern == internal.DiskByPathPrefix {
for _, pv := range vg.PVs {
devices = append(devices, deviceNameAndPaths[pv])
}
} else {
devices = vg.PVs
}

newStatus := &lvmv1alpha1.VGStatus{
Name: vg.Name,
Devices: devices,
Devices: vg.PVs,
Status: lvmv1alpha1.VGStatusReady,
}

Expand Down Expand Up @@ -671,17 +631,7 @@ func (r *VGReconciler) generateVolumeGroupNodeStatus(deviceNameAndPaths map[stri

func (r *VGReconciler) updateStatus(ctx context.Context, vgStatus *lvmv1alpha1.VGStatus) error {

deviceNameAndPaths, err := internal.ListDiskByPath(r.executor)
if err != nil {
return err
}

lvmVolumeGroups, err := r.getAllLvmVolumeGroups(ctx)
if err != nil {
return err
}

vgNodeStatus, err := r.generateVolumeGroupNodeStatus(deviceNameAndPaths, lvmVolumeGroups, vgStatus)
vgNodeStatus, err := r.generateVolumeGroupNodeStatus(vgStatus)

nodeStatus := &lvmv1alpha1.LVMVolumeGroupNodeStatus{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -711,24 +661,6 @@ func (r *VGReconciler) updateStatus(ctx context.Context, vgStatus *lvmv1alpha1.V
return err
}

func (r *VGReconciler) getAllLvmVolumeGroups(ctx context.Context) (map[string]*lvmv1alpha1.LVMVolumeGroup, error) {

lvmVolumeGroupsMap := make(map[string]*lvmv1alpha1.LVMVolumeGroup)

lvmVolumeGroups := &lvmv1alpha1.LVMVolumeGroupList{}
err := r.Client.List(ctx, lvmVolumeGroups, &client.ListOptions{Namespace: r.Namespace})
if err != nil {
r.Log.Error(err, "failed to list LVMVolumeGroups")
return nil, err
}

for i := range lvmVolumeGroups.Items {
lvmVolumeGroupsMap[lvmVolumeGroups.Items[i].Name] = &lvmVolumeGroups.Items[i]
}

return lvmVolumeGroupsMap, nil
}

// filterAvailableDevices returns:
// validDevices: the list of blockdevices considered available
// delayedDevices: the list of blockdevices considered available, but first observed less than 'minDeviceAge' time ago
Expand Down

0 comments on commit 7ecdee5

Please sign in to comment.