diff --git a/internal/edits/resource.go b/internal/edits/resource.go new file mode 100644 index 000000000..bf4d5f719 --- /dev/null +++ b/internal/edits/resource.go @@ -0,0 +1,30 @@ +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package edits + +import ( + "tags.cncf.io/container-device-interface/pkg/cdi" + "tags.cncf.io/container-device-interface/specs-go" +) + +// NewResource creates a CDI resource (Device) with the specified name. +func NewResource(name string, edits *cdi.ContainerEdits) specs.Device { + return specs.Device{ + Name: name, + ContainerEdits: *edits.ContainerEdits, + } +} diff --git a/pkg/nvcdi/full-gpu-nvml.go b/pkg/nvcdi/full-gpu-nvml.go index 881d102d2..18f4ced60 100644 --- a/pkg/nvcdi/full-gpu-nvml.go +++ b/pkg/nvcdi/full-gpu-nvml.go @@ -30,7 +30,7 @@ import ( // GetGPUDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'. func (l *nvmllib) GetGPUDeviceSpecs(i int, d device.Device) ([]specs.Device, error) { - edits, err := l.GetGPUDeviceEdits(d) + e, err := l.GetGPUDeviceEdits(d) if err != nil { return nil, fmt.Errorf("failed to get edits for device: %v", err) } @@ -41,10 +41,7 @@ func (l *nvmllib) GetGPUDeviceSpecs(i int, d device.Device) ([]specs.Device, err return nil, fmt.Errorf("failed to get device name: %v", err) } for _, name := range names { - spec := specs.Device{ - Name: name, - ContainerEdits: *edits.ContainerEdits, - } + spec := edits.NewResource(name, e) deviceSpecs = append(deviceSpecs, spec) } diff --git a/pkg/nvcdi/gds.go b/pkg/nvcdi/gds.go index 915cb94a2..f5d8b44f2 100644 --- a/pkg/nvcdi/gds.go +++ b/pkg/nvcdi/gds.go @@ -38,15 +38,12 @@ func (l *gdslib) GetAllDeviceSpecs() ([]specs.Device, error) { if err != nil { return nil, fmt.Errorf("failed to create GPUDirect Storage discoverer: %v", err) } - edits, err := edits.FromDiscoverer(discoverer) + e, err := edits.FromDiscoverer(discoverer) if err != nil { return nil, fmt.Errorf("failed to create container edits for GPUDirect Storage: %v", err) } - deviceSpec := specs.Device{ - Name: "all", - ContainerEdits: *edits.ContainerEdits, - } + deviceSpec := edits.NewResource("all", e) return []specs.Device{deviceSpec}, nil } diff --git a/pkg/nvcdi/lib-csv.go b/pkg/nvcdi/lib-csv.go index 649b801ae..fdb2be7d3 100644 --- a/pkg/nvcdi/lib-csv.go +++ b/pkg/nvcdi/lib-csv.go @@ -64,10 +64,7 @@ func (l *csvlib) GetAllDeviceSpecs() ([]specs.Device, error) { } var deviceSpecs []specs.Device for _, name := range names { - deviceSpec := specs.Device{ - Name: name, - ContainerEdits: *e.ContainerEdits, - } + deviceSpec := edits.NewResource(name, e) deviceSpecs = append(deviceSpecs, deviceSpec) } diff --git a/pkg/nvcdi/lib-wsl.go b/pkg/nvcdi/lib-wsl.go index 1c96c5383..e5876c543 100644 --- a/pkg/nvcdi/lib-wsl.go +++ b/pkg/nvcdi/lib-wsl.go @@ -39,15 +39,12 @@ func (l *wsllib) GetSpec() (spec.Interface, error) { // GetAllDeviceSpecs returns the device specs for all available devices. func (l *wsllib) GetAllDeviceSpecs() ([]specs.Device, error) { device := newDXGDeviceDiscoverer(l.logger, l.devRoot) - deviceEdits, err := edits.FromDiscoverer(device) + e, err := edits.FromDiscoverer(device) if err != nil { return nil, fmt.Errorf("failed to create container edits for DXG device: %v", err) } - deviceSpec := specs.Device{ - Name: "all", - ContainerEdits: *deviceEdits.ContainerEdits, - } + deviceSpec := edits.NewResource("all", e) return []specs.Device{deviceSpec}, nil } diff --git a/pkg/nvcdi/management.go b/pkg/nvcdi/management.go index 4648e5bb0..37f8262ca 100644 --- a/pkg/nvcdi/management.go +++ b/pkg/nvcdi/management.go @@ -43,19 +43,16 @@ func (m *managementlib) GetAllDeviceSpecs() ([]specs.Device, error) { return nil, fmt.Errorf("failed to create device discoverer: %v", err) } - edits, err := edits.FromDiscoverer(devices) + e, err := edits.FromDiscoverer(devices) if err != nil { return nil, fmt.Errorf("failed to create edits from discoverer: %v", err) } - if len(edits.DeviceNodes) == 0 { + if len(e.DeviceNodes) == 0 { return nil, fmt.Errorf("no NVIDIA device nodes found") } - device := specs.Device{ - Name: "all", - ContainerEdits: *edits.ContainerEdits, - } + device := edits.NewResource("all", e) return []specs.Device{device}, nil } diff --git a/pkg/nvcdi/mig-device-nvml.go b/pkg/nvcdi/mig-device-nvml.go index 91fe879c2..dc1b74adf 100644 --- a/pkg/nvcdi/mig-device-nvml.go +++ b/pkg/nvcdi/mig-device-nvml.go @@ -29,7 +29,7 @@ import ( // GetMIGDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'. func (l *nvmllib) GetMIGDeviceSpecs(i int, d device.Device, j int, mig device.MigDevice) ([]specs.Device, error) { - edits, err := l.GetMIGDeviceEdits(d, mig) + e, err := l.GetMIGDeviceEdits(d, mig) if err != nil { return nil, fmt.Errorf("failed to get edits for device: %v", err) } @@ -40,10 +40,7 @@ func (l *nvmllib) GetMIGDeviceSpecs(i int, d device.Device, j int, mig device.Mi } var deviceSpecs []specs.Device for _, name := range names { - spec := specs.Device{ - Name: name, - ContainerEdits: *edits.ContainerEdits, - } + spec := edits.NewResource(name, e) deviceSpecs = append(deviceSpecs, spec) } return deviceSpecs, nil diff --git a/pkg/nvcdi/transform/merged-device.go b/pkg/nvcdi/transform/merged-device.go index 523876e35..b7f992ee2 100644 --- a/pkg/nvcdi/transform/merged-device.go +++ b/pkg/nvcdi/transform/merged-device.go @@ -109,7 +109,6 @@ func mergeDeviceSpecs(deviceSpecs []specs.Device, mergedDeviceName string) (*spe } mergedEdits := edits.NewContainerEdits() - for _, d := range deviceSpecs { d := d edit := cdi.ContainerEdits{ @@ -118,9 +117,6 @@ func mergeDeviceSpecs(deviceSpecs []specs.Device, mergedDeviceName string) (*spe mergedEdits.Append(&edit) } - merged := specs.Device{ - Name: mergedDeviceName, - ContainerEdits: *mergedEdits.ContainerEdits, - } + merged := edits.NewResource(mergedDeviceName, mergedEdits) return &merged, nil }