Skip to content

Commit f6e67bb

Browse files
committed
Refactor the code to directly use BlockDevice
1 parent 1bb2147 commit f6e67bb

8 files changed

Lines changed: 111 additions & 158 deletions

File tree

pkg/common/provisioner_utils.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,14 @@ type CreateLocalPVArgs struct {
5050
StorageClass storagev1.StorageClass
5151
MountPointMap sets.String
5252
Client client.Client
53-
SymLinkPath string
54-
// DevicePath is the full path to the device (e.g. /dev/sda).
55-
// filepath.Base is applied internally where only the kernel name is needed.
56-
DevicePath string
53+
SymLinkPath string
5754
IDExists bool
5855
ExtraLabelsForPV map[string]string
5956

60-
// preferredsymlink stores the preferred symlink according to
61-
// LSO heuristics
62-
PreferredSymLink string
63-
// CurrentSymlink points to source to which
64-
// SymLinkPath points to.
57+
// CurrentSymlink points to source to which SymLinkPath points to.
6558
CurrentSymlink string
66-
// Kernel name of the device
67-
KName string
59+
// BlockDevice is the block device backing this PV.
60+
BlockDevice internal.BlockDevice
6861
}
6962

7063
// CreateLocalPV is used to create a local PV against a symlink
@@ -76,7 +69,7 @@ func CreateLocalPV(ctx context.Context, args CreateLocalPVArgs) error {
7669
mountPointMap := args.MountPointMap
7770
client := args.Client
7871
symLinkPath := args.SymLinkPath
79-
deviceName := filepath.Base(args.DevicePath)
72+
deviceName := args.BlockDevice.KName
8073
idExists := args.IDExists
8174
extraLabelsForPV := args.ExtraLabelsForPV
8275
nodeLabels := runtimeConfig.Node.GetLabels()
@@ -126,7 +119,7 @@ func CreateLocalPV(ctx context.Context, args CreateLocalPVArgs) error {
126119
return fmt.Errorf("name: %q, namespace: %q, or kind: %q is empty for obj: %+v", name, namespace, kind, obj)
127120
}
128121

129-
deviceHandler := internal.NewDeviceLinkHandler(args.CurrentSymlink, args.PreferredSymLink, client)
122+
deviceHandler := internal.NewDeviceLinkHandler(args.CurrentSymlink, client)
130123

131124
_, err = deviceHandler.Create(ctx, pvName, runtimeConfig.Namespace, obj)
132125
if err != nil {
@@ -263,7 +256,7 @@ func CreateLocalPV(ctx context.Context, args CreateLocalPVArgs) error {
263256
return fmt.Errorf("error creating pv %s: %w", pvName, err)
264257
}
265258

266-
_, err = deviceHandler.ApplyStatus(ctx, pvName, runtimeConfig.Namespace, args.KName, args.DevicePath, obj)
259+
_, err = deviceHandler.ApplyStatus(ctx, pvName, runtimeConfig.Namespace, args.BlockDevice, obj)
267260
if err != nil {
268261
return fmt.Errorf("error updating localvolumedevicelink object: %w", err)
269262
}

pkg/diskmaker/controllers/lv/create_pv_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestCreatePV(t *testing.T) {
255255
MountPointMap: tc.mountPoints,
256256
Client: r.Client,
257257
SymLinkPath: tc.symlinkpath,
258-
DevicePath: tc.deviceName,
258+
BlockDevice: internal.BlockDevice{KName: filepath.Base(tc.deviceName)},
259259
IDExists: true,
260260
ExtraLabelsForPV: map[string]string{},
261261
})
@@ -304,7 +304,7 @@ func TestCreatePV(t *testing.T) {
304304
MountPointMap: tc.mountPoints,
305305
Client: r.Client,
306306
SymLinkPath: tc.symlinkpath,
307-
DevicePath: tc.deviceName,
307+
BlockDevice: internal.BlockDevice{KName: filepath.Base(tc.deviceName)},
308308
IDExists: true,
309309
ExtraLabelsForPV: map[string]string{},
310310
})
@@ -412,8 +412,7 @@ func TestCreateLocalPV_DeviceLinkArgOrder(t *testing.T) {
412412
MountPointMap: sets.NewString(),
413413
Client: r.Client,
414414
SymLinkPath: symLinkPath,
415-
DevicePath: devPath,
416-
KName: kName,
415+
BlockDevice: internal.BlockDevice{KName: kName, PathByID: fakeByIDLink},
417416
IDExists: true,
418417
ExtraLabelsForPV: map[string]string{},
419418
})

pkg/diskmaker/controllers/lv/reconcile.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,10 @@ func (r *LocalVolumeReconciler) Reconcile(ctx context.Context, request ctrl.Requ
463463
MountPointMap: mountPointMap,
464464
Client: r.Client,
465465
SymLinkPath: target,
466-
DevicePath: deviceNameLocation.DiskNamePath,
467466
IDExists: idExists,
468467
ExtraLabelsForPV: lvOwnerLabels,
469-
PreferredSymLink: deviceNameLocation.DiskID,
470468
CurrentSymlink: source,
471-
KName: deviceNameLocation.BlockDevice.KName,
469+
BlockDevice: deviceNameLocation.BlockDevice,
472470
}
473471

474472
err = common.CreateLocalPV(ctx, createLocalPVArgs)
@@ -526,22 +524,11 @@ func (r *LocalVolumeReconciler) processRejectedDevicesForDeviceLinks(ctx context
526524

527525
blockDevice := diskLocation.BlockDevice
528526

529-
preferredSymLink, err := blockDevice.GetPathByID("")
530-
if err != nil {
531-
klog.ErrorS(err, "", "failed to get preferred device link", "disk", blockDevice.Name)
532-
continue
533-
}
534-
535-
devicePath, err := blockDevice.GetDevPath()
536-
if err != nil {
537-
klog.ErrorS(err, "failed to get /dev path for block device", "disk", blockDevice.Name)
538-
continue
539-
}
540-
541527
pvName := common.GeneratePVName(existingSymlink, r.runtimeConfig.Node.Name, storageClassName)
542-
deviceHandler := internal.NewDeviceLinkHandler(source, preferredSymLink, r.Client)
543-
_, err = deviceHandler.ApplyStatus(ctx, pvName, r.runtimeConfig.Namespace, blockDevice.KName, devicePath, r.localVolume)
528+
deviceHandler := internal.NewDeviceLinkHandler(source, r.Client)
529+
_, err = deviceHandler.ApplyStatus(ctx, pvName, r.runtimeConfig.Namespace, blockDevice, r.localVolume)
544530
if err != nil {
531+
r.eventSync.Report(r.localVolume, newDiskEvent(diskmaker.ErrorCreatingLVDL, "failed to create localvolumedevicelink", blockDevice.KName, corev1.EventTypeWarning))
545532
klog.ErrorS(err, "error updating LocalVolumeDeviceLink", "device", blockDevice.Name)
546533
}
547534
}

pkg/diskmaker/controllers/lvset/create_pv_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestCreatePV(t *testing.T) {
234234
MountPointMap: tc.mountPoints,
235235
Client: r.Client,
236236
SymLinkPath: tc.symlinkpath,
237-
DevicePath: tc.deviceName,
237+
BlockDevice: internal.BlockDevice{KName: filepath.Base(tc.deviceName)},
238238
IDExists: true,
239239
ExtraLabelsForPV: map[string]string{},
240240
})
@@ -283,7 +283,7 @@ func TestCreatePV(t *testing.T) {
283283
MountPointMap: tc.mountPoints,
284284
Client: r.Client,
285285
SymLinkPath: tc.symlinkpath,
286-
DevicePath: tc.deviceName,
286+
BlockDevice: internal.BlockDevice{KName: filepath.Base(tc.deviceName)},
287287
IDExists: true,
288288
ExtraLabelsForPV: map[string]string{},
289289
})
@@ -361,8 +361,7 @@ func TestCreatePV_SetsLVDLOwnerRefToLocalVolumeSet(t *testing.T) {
361361
MountPointMap: sets.NewString(),
362362
Client: r.Client,
363363
SymLinkPath: symLinkPath,
364-
DevicePath: "/dev/device-ownerref",
365-
KName: "device-ownerref",
364+
BlockDevice: internal.BlockDevice{KName: "device-ownerref"},
366365
IDExists: true,
367366
ExtraLabelsForPV: map[string]string{},
368367
})

pkg/diskmaker/controllers/lvset/reconcile.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,11 @@ func (r *LocalVolumeSetReconciler) processRejectedDevicesForDeviceLinks(ctx cont
283283
"blockDevice", blockDevice.Name)
284284
continue
285285
}
286-
preferredSymLink, err := blockDevice.GetPathByID("")
287-
if err != nil {
288-
klog.ErrorS(err, "", "failed to get preferred device link", "disk", blockDevice.Name)
289-
continue
290-
}
291-
292-
devicePath, err := blockDevice.GetDevPath()
293-
if err != nil {
294-
klog.ErrorS(err, "failed to get /dev path for block device", "disk", blockDevice.Name)
295-
continue
296-
}
297-
298286
pvName := common.GeneratePVName(existingSymlink, r.runtimeConfig.Node.Name, storageClassName)
299-
deviceHandler := internal.NewDeviceLinkHandler(symlinkSourcePath, preferredSymLink, r.Client)
300-
_, err = deviceHandler.ApplyStatus(ctx, pvName, r.runtimeConfig.Namespace, blockDevice.KName, devicePath, lvset)
287+
deviceHandler := internal.NewDeviceLinkHandler(symlinkSourcePath, r.Client)
288+
_, err = deviceHandler.ApplyStatus(ctx, pvName, r.runtimeConfig.Namespace, blockDevice, lvset)
301289
if err != nil {
290+
r.eventReporter.Report(lvset, newDiskEvent(diskmaker.ErrorCreatingLVDL, "failed to create localvolumedevicelink", blockDevice.KName, corev1.EventTypeWarning))
302291
klog.ErrorS(err, "error updating LocalVolumeDeviceLink", "device", blockDevice.Name)
303292
}
304293
}
@@ -468,28 +457,17 @@ func (r *LocalVolumeSetReconciler) provisionPV(
468457
}
469458
}
470459

471-
// find what would be preferred symlink for the device
472-
// TODO: This may not be a fatal error
473-
preferredSymLink, err := dev.GetPathByID("")
474-
if err != nil {
475-
klog.ErrorS(err, "", "failed to get preferred device link", "disk", devLabelPath)
476-
return err
477-
}
478-
479460
createLocalPVArgs := common.CreateLocalPVArgs{
480461
LocalVolumeLikeObject: obj,
481462
RuntimeConfig: r.runtimeConfig,
482463
StorageClass: storageClass,
483464
MountPointMap: mountPointMap,
484465
Client: r.Client,
485466
SymLinkPath: symlinkPath,
486-
DevicePath: devLabelPath,
487467
IDExists: idExists,
488468
ExtraLabelsForPV: map[string]string{},
489-
490-
CurrentSymlink: symlinkSourcePath,
491-
KName: dev.KName,
492-
PreferredSymLink: preferredSymLink,
469+
CurrentSymlink: symlinkSourcePath,
470+
BlockDevice: dev,
493471
}
494472

495473
defer unlockFunc()

pkg/diskmaker/event_reporter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
FoundMatchingDisk = "FoundMatchingDisk"
2323
DeviceSymlinkExists = "DeviceSymlinkExists"
2424

25+
ErrorCreatingLVDL = "ErrorCreatingLocalVolumeDeviceLink"
26+
2527
// LocalVolumeDiscovery events
2628
ErrorCreatingDiscoveryResultObject = "ErrorCreatingDiscoveryResultObject"
2729
ErrorUpdatingDiscoveryResultObject = "ErrorUpdatingDiscoveryResultObject"

pkg/internal/device_link_handler.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os/exec"
78
"reflect"
@@ -22,21 +23,14 @@ import (
2223
)
2324

2425
type DeviceLinkHandler struct {
25-
currentSymlink string
26-
preferredSymlink string
27-
28-
// devicePoints to current device such as /dev/sda or something similar
29-
// so basically this is direct path in /dev filesystem to which
30-
// currentSymlink or preferredSymlink resolves to
31-
devicePath string
32-
client client.Client
26+
currentSymlink string
27+
client client.Client
3328
}
3429

35-
func NewDeviceLinkHandler(currentSymlink, preferredSymlink string, client client.Client) *DeviceLinkHandler {
30+
func NewDeviceLinkHandler(currentSymlink string, client client.Client) *DeviceLinkHandler {
3631
return &DeviceLinkHandler{
37-
currentSymlink: currentSymlink,
38-
preferredSymlink: preferredSymlink,
39-
client: client,
32+
currentSymlink: currentSymlink,
33+
client: client,
4034
}
4135
}
4236

@@ -145,8 +139,24 @@ func isNilOwnerObject(ownerObj runtime.Object) bool {
145139
return value.Kind() == reflect.Ptr && value.IsNil()
146140
}
147141

148-
func (dl *DeviceLinkHandler) ApplyStatus(ctx context.Context, pvName, namespace, kName, devicePath string, ownerObj runtime.Object) (*v1.LocalVolumeDeviceLink, error) {
149-
klog.V(2).Infof("updating lvdl with currentSymlink: %s, preferredSymlink: %s, devicePath: %s, kname: %s", dl.currentSymlink, dl.preferredSymlink, devicePath, kName)
142+
func (dl *DeviceLinkHandler) ApplyStatus(ctx context.Context, pvName, namespace string, blockDevice BlockDevice, ownerObj runtime.Object) (*v1.LocalVolumeDeviceLink, error) {
143+
preferredSymlink, err := blockDevice.GetUncachedPathID()
144+
if err != nil {
145+
// IDPathNotFoundError means no by-id symlink exists for this device;
146+
// treat it as "no preferred symlink" rather than a hard error.
147+
var idNotFound IDPathNotFoundError
148+
if !errors.As(err, &idNotFound) {
149+
return nil, fmt.Errorf("failed to get preferred device link for %s: %w", blockDevice.Name, err)
150+
}
151+
preferredSymlink = ""
152+
}
153+
154+
devicePath, err := blockDevice.GetDevPath()
155+
if err != nil {
156+
return nil, fmt.Errorf("failed to get /dev path for %s: %w", blockDevice.Name, err)
157+
}
158+
159+
klog.V(2).Infof("updating lvdl with currentSymlink: %s, preferredSymlink: %s, devicePath: %s, kname: %s", dl.currentSymlink, preferredSymlink, devicePath, blockDevice.KName)
150160

151161
// Update is best-effort and independent from Create: if either the PV or
152162
// the LVDL does not exist yet, return without doing anything.
@@ -169,7 +179,7 @@ func (dl *DeviceLinkHandler) ApplyStatus(ctx context.Context, pvName, namespace,
169179
return nil, nil
170180
}
171181

172-
validLinks, err := dl.getValidByIDSymlinks(kName)
182+
validLinks, err := dl.getValidByIDSymlinks(blockDevice.KName)
173183
if err != nil {
174184
return nil, err
175185
}
@@ -183,7 +193,7 @@ func (dl *DeviceLinkHandler) ApplyStatus(ctx context.Context, pvName, namespace,
183193
updatedCopy := existing.DeepCopy()
184194

185195
updatedCopy.Status.CurrentLinkTarget = dl.currentSymlink
186-
updatedCopy.Status.PreferredLinkTarget = dl.preferredSymlink
196+
updatedCopy.Status.PreferredLinkTarget = preferredSymlink
187197
updatedCopy.Status.ValidLinkTargets = validLinks
188198
updatedCopy.Status.FilesystemUUID = filesystemUUID
189199

0 commit comments

Comments
 (0)