@@ -14,47 +14,25 @@ import (
1414
1515 . "github.com/netapp/trident/logging"
1616 "github.com/netapp/trident/utils/devices/luks"
17- "github.com/netapp/trident/utils/errors"
1817 "github.com/netapp/trident/utils/exec"
1918 "github.com/netapp/trident/utils/filesystem"
2019 "github.com/netapp/trident/utils/models"
2120)
2221
2322const NVMeAttachTimeout = 20 * time .Second
2423
25- var fsClient = filesystem .New (mountClient )
26-
27- // getNVMeSubsystem returns the NVMe subsystem details.
28- func getNVMeSubsystem (ctx context.Context , subsysNqn string ) (* NVMeSubsystem , error ) {
29- Logc (ctx ).Debug (">>>> nvme.getNVMeSubsystem" )
30- defer Logc (ctx ).Debug ("<<<< nvme.getNVMeSubsystem" )
31-
32- subsys , err := GetNVMeSubsystemList (ctx )
33- if err != nil {
34- Logc (ctx ).WithField ("Error" , err ).Errorf ("Failed to get subsystem list: %v" , err )
35- return nil , err
36- }
37-
38- // Getting current subsystem details.
39- for _ , sub := range subsys .Subsystems {
40- if sub .NQN == subsysNqn {
41- return & sub , nil
42- }
43- }
44-
45- return nil , fmt .Errorf ("couldn't find subsystem %s" , subsysNqn )
46- }
24+ var fsClient = * filesystem .New (mountClient )
4725
4826// updatePaths updates the paths with the current state of the subsystem on the k8s node.
4927func (s * NVMeSubsystem ) updatePaths (ctx context.Context ) error {
5028 // Getting current state of subsystem on the k8s node.
51- sub , err := getNVMeSubsystem (ctx , s . NQN )
29+ paths , err := GetNVMeSubsystemPaths (ctx , fsClient , s . Name )
5230 if err != nil {
5331 Logc (ctx ).WithField ("Error" , err ).Errorf ("Failed to update subsystem paths: %v" , err )
5432 return fmt .Errorf ("failed to update subsystem paths: %v" , err )
5533 }
5634
57- s .Paths = sub . Paths
35+ s .Paths = paths
5836
5937 return nil
6038}
@@ -137,43 +115,38 @@ func (s *NVMeSubsystem) Disconnect(ctx context.Context) error {
137115
138116// GetNamespaceCount returns the number of namespaces mapped to the subsystem.
139117func (s * NVMeSubsystem ) GetNamespaceCount (ctx context.Context ) (int , error ) {
140- var combinedError error
141-
118+ credibility := false
142119 for _ , path := range s .Paths {
143- count , err := GetNamespaceCountForSubsDevice (ctx , "/dev/" + path .Name )
144- if err != nil {
145- Logc (ctx ).WithField ("Error" , err ).Warnf ("Failed to get namespace count: %v" , err )
146- combinedError = multierr .Append (combinedError , err )
147- continue
120+ if path .State == "live" {
121+ credibility = true
122+ break
148123 }
124+ }
149125
150- return count , nil
126+ if ! credibility {
127+ return 0 , fmt .Errorf ("nvme paths are down, couldn't get the number of namespaces" )
128+ }
129+
130+ count , err := GetNVMeDeviceCountAt (ctx , s .FS , s .Name )
131+ if err != nil {
132+ Logc (ctx ).Errorf ("Failed to get namespace count: %v" , err )
133+ return 0 , err
151134 }
152135
153- // Couldn't find any sessions, so no namespaces are attached to this subsystem.
154- // But if there was error getting the number of namespaces from all the paths, return error.
155- return 0 , combinedError
136+ return count , nil
156137}
157138
158- // getNVMeDevice returns the NVMe device corresponding to nsPath namespace.
159- func getNVMeDevice (ctx context.Context , nsUUID string ) (* NVMeDevice , error ) {
160- Logc (ctx ).Debug (">>>> nvme.getNVMeDevice" )
161- defer Logc (ctx ).Debug ("<<<< nvme.getNVMeDevice" )
139+ func (s * NVMeSubsystem ) GetNVMeDevice (ctx context.Context , nsUUID string ) (NVMeDeviceInterface , error ) {
140+ Logc (ctx ).Debug (">>>> nvme.GetNVMeDevice" )
141+ defer Logc (ctx ).Debug ("<<<< nvme.GetNVMeDevice" )
162142
163- dList , err := GetNVMeDeviceList (ctx )
143+ devInterface , err := GetNVMeDeviceAt (ctx , s . Name , nsUUID )
164144 if err != nil {
165- return nil , fmt .Errorf ("failed to get device: %v" , err )
166- }
167-
168- for _ , dev := range dList .Devices {
169- if dev .UUID == nsUUID {
170- Logc (ctx ).Debugf ("Device found: %v." , dev )
171- return & dev , nil
172- }
145+ Logc (ctx ).Errorf ("Failed to get NVMe device, %v" , err )
146+ return nil , err
173147 }
174148
175- Logc (ctx ).WithField ("nsUUID" , nsUUID ).Debug ("No device found for this Namespace." )
176- return nil , errors .NotFoundError ("no device found for the given namespace %v" , nsUUID )
149+ return devInterface , nil
177150}
178151
179152// GetPath returns the device path where we mount the filesystem in NodePublish.
@@ -203,7 +176,7 @@ func (d *NVMeDevice) FlushDevice(ctx context.Context, ignoreErrors, force bool)
203176
204177// IsNil returns true if Device and NamespacePath are not set.
205178func (d * NVMeDevice ) IsNil () bool {
206- if d == nil || ( d .Device == "" && d . NamespacePath == "" ) {
179+ if d == nil || d .Device == "" {
207180 return true
208181 }
209182 return false
@@ -214,20 +187,15 @@ func NewNVMeHandler() NVMeInterface {
214187 return & NVMeHandler {}
215188}
216189
217- // NewNVMeDevice returns new NVMe device
218- func (nh * NVMeHandler ) NewNVMeDevice (ctx context.Context , nsUUID string ) (NVMeDeviceInterface , error ) {
219- return getNVMeDevice (ctx , nsUUID )
220- }
221-
222190// NewNVMeSubsystem returns NVMe subsystem object. Even if a subsystem is not connected to the k8s node,
223191// this function returns a minimal NVMe subsystem object.
224192func (nh * NVMeHandler ) NewNVMeSubsystem (ctx context.Context , subsNqn string ) NVMeSubsystemInterface {
225- sub , err := getNVMeSubsystem (ctx , subsNqn )
193+ sub , err := GetNVMeSubsystem (ctx , fsClient , subsNqn )
226194 if err != nil {
227195 Logc (ctx ).WithField ("Error" , err ).Warnf ("Failed to get subsystem: %v; returning minimal subsystem" , err )
228196 return & NVMeSubsystem {NQN : subsNqn }
229197 }
230- return sub
198+ return & sub
231199}
232200
233201// GetHostNqn returns the NQN of the k8s node.
@@ -295,7 +263,7 @@ func AttachNVMeVolume(
295263 }
296264 }
297265
298- nvmeDev , err := nvmeHandler . NewNVMeDevice (ctx , publishInfo .NVMeNamespaceUUID )
266+ nvmeDev , err := nvmeSubsys . GetNVMeDevice (ctx , publishInfo .NVMeNamespaceUUID )
299267 if err != nil {
300268 return err
301269 }
@@ -589,7 +557,7 @@ func (nh *NVMeHandler) PopulateCurrentNVMeSessions(ctx context.Context, currSess
589557 }
590558
591559 // Get the list of the subsystems currently present on the k8s node.
592- subs , err := GetNVMeSubsystemList ( ctx )
560+ subs , err := listSubsystemsFromSysFs ( fsClient , ctx )
593561 if err != nil {
594562 Logc (ctx ).WithField ("Error" , err ).Errorf ("Failed to get subsystem list: %v" , err )
595563 return err
0 commit comments