@@ -166,39 +166,25 @@ func filterAutomaticDevices(devices []string) []string {
166166func newAutomaticCDISpecModifier (logger logger.Interface , cfg * config.Config , devices []string ) (oci.SpecModifier , error ) {
167167 logger .Debugf ("Generating in-memory CDI specs for devices %v" , devices )
168168
169- perModeIdentifiers := make (map [string ][]string )
170- perModeDeviceClass := map [string ]string {"auto" : automaticDeviceClass }
171- uniqueModes := []string {"auto" }
172- seen := make (map [string ]bool )
173- for _ , device := range devices {
174- mode , id := getModeIdentifier (device )
175- logger .Debugf ("Mapped %v to %v: %v" , device , mode , id )
176- if ! seen [mode ] {
177- uniqueModes = append (uniqueModes , mode )
178- seen [mode ] = true
179- }
180- if id != "" {
181- perModeIdentifiers [mode ] = append (perModeIdentifiers [mode ], id )
182- }
183- }
169+ cdiModeIdentifiers := cdiModeIdentfiersFromDevices (devices ... )
184170
185- logger .Debugf ("Per-mode identifiers: %v" , perModeIdentifiers )
171+ logger .Debugf ("Per-mode identifiers: %v" , cdiModeIdentifiers )
186172 var modifiers oci.SpecModifiers
187- for _ , mode := range uniqueModes {
173+ for _ , mode := range cdiModeIdentifiers . modes {
188174 cdilib , err := nvcdi .New (
189175 nvcdi .WithLogger (logger ),
190176 nvcdi .WithNVIDIACDIHookPath (cfg .NVIDIACTKConfig .Path ),
191177 nvcdi .WithDriverRoot (cfg .NVIDIAContainerCLIConfig .Root ),
192178 nvcdi .WithVendor (automaticDeviceVendor ),
193- nvcdi .WithClass (perModeDeviceClass [mode ]),
179+ nvcdi .WithClass (cdiModeIdentifiers . deviceClassByMode [mode ]),
194180 nvcdi .WithMode (mode ),
195181 nvcdi .WithFeatureFlags (cfg .NVIDIAContainerRuntimeConfig .Modes .JitCDI .NVCDIFeatureFlags ... ),
196182 )
197183 if err != nil {
198184 return nil , fmt .Errorf ("failed to construct CDI library for mode %q: %w" , mode , err )
199185 }
200186
201- spec , err := cdilib .GetSpec (perModeIdentifiers [mode ]... )
187+ spec , err := cdilib .GetSpec (cdiModeIdentifiers . idsByMode [mode ]... )
202188 if err != nil {
203189 return nil , fmt .Errorf ("failed to generate CDI spec for mode %q: %w" , mode , err )
204190 }
@@ -217,6 +203,35 @@ func newAutomaticCDISpecModifier(logger logger.Interface, cfg *config.Config, de
217203 return modifiers , nil
218204}
219205
206+ type cdiModeIdentifiers struct {
207+ modes []string
208+ idsByMode map [string ][]string
209+ deviceClassByMode map [string ]string
210+ }
211+
212+ func cdiModeIdentfiersFromDevices (devices ... string ) * cdiModeIdentifiers {
213+ perModeIdentifiers := make (map [string ][]string )
214+ perModeDeviceClass := map [string ]string {"auto" : automaticDeviceClass }
215+ var uniqueModes []string
216+ seen := make (map [string ]bool )
217+ for _ , device := range devices {
218+ mode , id := getModeIdentifier (device )
219+ if ! seen [mode ] {
220+ uniqueModes = append (uniqueModes , mode )
221+ seen [mode ] = true
222+ }
223+ if id != "" {
224+ perModeIdentifiers [mode ] = append (perModeIdentifiers [mode ], id )
225+ }
226+ }
227+
228+ return & cdiModeIdentifiers {
229+ modes : uniqueModes ,
230+ idsByMode : perModeIdentifiers ,
231+ deviceClassByMode : perModeDeviceClass ,
232+ }
233+ }
234+
220235func getModeIdentifier (device string ) (string , string ) {
221236 if ! strings .HasPrefix (device , "mode=" ) {
222237 return "auto" , strings .TrimPrefix (device , automaticDevicePrefix )
0 commit comments