Skip to content

Commit 1172f27

Browse files
committed
[no-relnote] Move nvcdi wrapper to separate file
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 3ebe8ea commit 1172f27

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

pkg/nvcdi/lib.go

Lines changed: 0 additions & 44 deletions
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
@@ -174,38 +162,6 @@ func New(opts ...Option) (Interface, error) {
174162
return &w, nil
175163
}
176164

177-
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
178-
func (l *wrapper) GetSpec() (spec.Interface, error) {
179-
deviceSpecs, err := l.GetAllDeviceSpecs()
180-
if err != nil {
181-
return nil, err
182-
}
183-
184-
edits, err := l.GetCommonEdits()
185-
if err != nil {
186-
return nil, err
187-
}
188-
189-
return spec.New(
190-
spec.WithDeviceSpecs(deviceSpecs),
191-
spec.WithEdits(*edits.ContainerEdits),
192-
spec.WithVendor(l.vendor),
193-
spec.WithClass(l.class),
194-
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
195-
)
196-
}
197-
198-
// GetCommonEdits returns the wrapped edits and adds additional edits on top.
199-
func (m *wrapper) GetCommonEdits() (*cdi.ContainerEdits, error) {
200-
edits, err := m.Interface.GetCommonEdits()
201-
if err != nil {
202-
return nil, err
203-
}
204-
edits.Env = append(edits.Env, image.EnvVarNvidiaVisibleDevices+"=void")
205-
206-
return edits, nil
207-
}
208-
209165
// resolveMode resolves the mode for CDI spec generation based on the current system.
210166
func (l *nvcdilib) resolveMode() (rmode string) {
211167
if l.mode != ModeAuto {

pkg/nvcdi/wrapper.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
# Copyright 2024 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)