@@ -2,6 +2,7 @@ package internal
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "os/exec"
78 "reflect"
@@ -22,21 +23,14 @@ import (
2223)
2324
2425type 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