|
| 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