Skip to content

Commit 65b575f

Browse files
authored
Merge pull request #933 from elezar/move-wrapper
[no-relnote] Move nvcdi wrapper to separate file
2 parents 5d9b27f + 6e413d8 commit 65b575f

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

pkg/nvcdi/lib.go

-44
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,14 @@ import (
2222
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
2323
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
2424
"github.com/NVIDIA/go-nvml/pkg/nvml"
25-
"tags.cncf.io/container-device-interface/pkg/cdi"
2625

27-
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
2826
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2927
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
3028
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
3129
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
32-
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
3330
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
3431
)
3532

36-
type wrapper struct {
37-
Interface
38-
39-
vendor string
40-
class string
41-
42-
mergedDeviceOptions []transform.MergedDeviceOption
43-
}
44-
4533
type nvcdilib struct {
4634
logger logger.Interface
4735
nvmllib nvml.Interface
@@ -180,38 +168,6 @@ func New(opts ...Option) (Interface, error) {
180168
return &w, nil
181169
}
182170

183-
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
184-
func (l *wrapper) GetSpec() (spec.Interface, error) {
185-
deviceSpecs, err := l.GetAllDeviceSpecs()
186-
if err != nil {
187-
return nil, err
188-
}
189-
190-
edits, err := l.GetCommonEdits()
191-
if err != nil {
192-
return nil, err
193-
}
194-
195-
return spec.New(
196-
spec.WithDeviceSpecs(deviceSpecs),
197-
spec.WithEdits(*edits.ContainerEdits),
198-
spec.WithVendor(l.vendor),
199-
spec.WithClass(l.class),
200-
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
201-
)
202-
}
203-
204-
// GetCommonEdits returns the wrapped edits and adds additional edits on top.
205-
func (m *wrapper) GetCommonEdits() (*cdi.ContainerEdits, error) {
206-
edits, err := m.Interface.GetCommonEdits()
207-
if err != nil {
208-
return nil, err
209-
}
210-
edits.Env = append(edits.Env, image.EnvVarNvidiaVisibleDevices+"=void")
211-
212-
return edits, nil
213-
}
214-
215171
// getCudaVersion returns the CUDA version of the current system.
216172
func (l *nvcdilib) getCudaVersion() (string, error) {
217173
version, err := l.getCudaVersionNvsandboxutils()

pkg/nvcdi/wrapper.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
# Copyright 2025 NVIDIA CORPORATION
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package nvcdi
18+
19+
import (
20+
"tags.cncf.io/container-device-interface/pkg/cdi"
21+
"tags.cncf.io/container-device-interface/specs-go"
22+
23+
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
24+
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
25+
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
26+
)
27+
28+
type wrapper struct {
29+
Interface
30+
31+
vendor string
32+
class string
33+
34+
mergedDeviceOptions []transform.MergedDeviceOption
35+
}
36+
37+
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
38+
func (l *wrapper) GetSpec() (spec.Interface, error) {
39+
deviceSpecs, err := l.GetAllDeviceSpecs()
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
edits, err := l.GetCommonEdits()
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
return spec.New(
50+
spec.WithDeviceSpecs(deviceSpecs),
51+
spec.WithEdits(*edits.ContainerEdits),
52+
spec.WithVendor(l.vendor),
53+
spec.WithClass(l.class),
54+
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
55+
)
56+
}
57+
58+
// GetAllDeviceSpecs returns the device specs for all available devices.
59+
func (l *wrapper) GetAllDeviceSpecs() ([]specs.Device, error) {
60+
return l.Interface.GetAllDeviceSpecs()
61+
}
62+
63+
// GetCommonEdits returns the wrapped edits and adds additional edits on top.
64+
func (m *wrapper) GetCommonEdits() (*cdi.ContainerEdits, error) {
65+
edits, err := m.Interface.GetCommonEdits()
66+
if err != nil {
67+
return nil, err
68+
}
69+
edits.Env = append(edits.Env, image.EnvVarNvidiaVisibleDevices+"=void")
70+
71+
return edits, nil
72+
}

0 commit comments

Comments
 (0)