Skip to content

Commit

Permalink
[no-relnote] Add NewResource function
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Aug 5, 2024
1 parent fa46c01 commit 87f54c5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 35 deletions.
30 changes: 30 additions & 0 deletions internal/edits/resource.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
7 changes: 2 additions & 5 deletions pkg/nvcdi/full-gpu-nvml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/nvcdi/gds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/nvcdi/lib-csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/nvcdi/lib-wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/nvcdi/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/nvcdi/mig-device-nvml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions pkg/nvcdi/transform/merged-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func mergeDeviceSpecs(deviceSpecs []specs.Device, mergedDeviceName string) (*spe
}

mergedEdits := edits.NewContainerEdits()

for _, d := range deviceSpecs {
d := d
edit := cdi.ContainerEdits{
Expand All @@ -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
}

0 comments on commit 87f54c5

Please sign in to comment.