Skip to content

Commit a103795

Browse files
committed
Add EnvVar to Discover interface
This change adds environment variables to the Discover interface. Signed-off-by: Evan Lezar <[email protected]>
1 parent b4edc3e commit a103795

13 files changed

+204
-0
lines changed

internal/discover/cache.go

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type cache struct {
2323

2424
sync.Mutex
2525
devices []Device
26+
envVars []EnvVar
2627
hooks []Hook
2728
mounts []Mount
2829
}
@@ -51,6 +52,20 @@ func (c *cache) Devices() ([]Device, error) {
5152
return c.devices, nil
5253
}
5354

55+
func (c *cache) EnvVars() ([]EnvVar, error) {
56+
c.Lock()
57+
defer c.Unlock()
58+
59+
if c.envVars == nil {
60+
envVars, err := c.d.EnvVars()
61+
if err != nil {
62+
return nil, err
63+
}
64+
c.envVars = envVars
65+
}
66+
return c.envVars, nil
67+
}
68+
5469
func (c *cache) Hooks() ([]Hook, error) {
5570
c.Lock()
5671
defer c.Unlock()

internal/discover/discover.go

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ type Device struct {
2222
Path string
2323
}
2424

25+
// EnvVar represents a discovered environment variable.
26+
type EnvVar struct {
27+
Name string
28+
Value string
29+
}
30+
2531
// Mount represents a discovered mount.
2632
type Mount struct {
2733
HostPath string
@@ -41,6 +47,7 @@ type Hook struct {
4147
//go:generate moq -rm -fmt=goimports -stub -out discover_mock.go . Discover
4248
type Discover interface {
4349
Devices() ([]Device, error)
50+
EnvVars() ([]EnvVar, error)
4451
Mounts() ([]Mount, error)
4552
Hooks() ([]Hook, error)
4653
}

internal/discover/discover_mock.go

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/discover/envvar.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
**/
17+
18+
package discover
19+
20+
var _ Discover = (*EnvVar)(nil)
21+
22+
// Devices returns an empty list of devices for a EnvVar discoverer.
23+
func (e EnvVar) Devices() ([]Device, error) {
24+
return nil, nil
25+
}
26+
27+
// EnvVars returns an empty list of envs for a EnvVar discoverer.
28+
func (e EnvVar) EnvVars() ([]EnvVar, error) {
29+
return []EnvVar{e}, nil
30+
}
31+
32+
// Mounts returns an empty list of mounts for a EnvVar discoverer.
33+
func (e EnvVar) Mounts() ([]Mount, error) {
34+
return nil, nil
35+
}
36+
37+
// Hooks allows the Hook type to also implement the Discoverer interface.
38+
// It returns a single hook
39+
func (e EnvVar) Hooks() ([]Hook, error) {
40+
return nil, nil
41+
}

internal/discover/first-valid.go

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ func (f firstOf) Devices() ([]Device, error) {
4545
return nil, errs
4646
}
4747

48+
func (f firstOf) EnvVars() ([]EnvVar, error) {
49+
var errs error
50+
for _, d := range f {
51+
envs, err := d.EnvVars()
52+
if err != nil {
53+
errs = errors.Join(errs, err)
54+
continue
55+
}
56+
return envs, nil
57+
}
58+
return nil, errs
59+
}
60+
4861
func (f firstOf) Hooks() ([]Hook, error) {
4962
var errs error
5063
for _, d := range f {

internal/discover/hooks.go

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func (h Hook) Devices() ([]Device, error) {
2929
return nil, nil
3030
}
3131

32+
// EnvVars returns an empty list of envs for a Hook discoverer.
33+
func (h Hook) EnvVars() ([]EnvVar, error) {
34+
return nil, nil
35+
}
36+
3237
// Mounts returns an empty list of mounts for a Hook discoverer.
3338
func (h Hook) Mounts() ([]Mount, error) {
3439
return nil, nil

internal/discover/list.go

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ func (d list) Devices() ([]Device, error) {
5151
return allDevices, nil
5252
}
5353

54+
// EnvVars returns all environment variables from the included discoverers.
55+
func (d list) EnvVars() ([]EnvVar, error) {
56+
var allEnvs []EnvVar
57+
58+
for i, di := range d.discoverers {
59+
envs, err := di.EnvVars()
60+
if err != nil {
61+
return nil, fmt.Errorf("error discovering envs for discoverer %v: %w", i, err)
62+
}
63+
allEnvs = append(allEnvs, envs...)
64+
}
65+
66+
return allEnvs, nil
67+
}
68+
5469
// Mounts returns all mounts from the included discoverers
5570
func (d list) Mounts() ([]Mount, error) {
5671
var allMounts []Mount

internal/discover/none.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func (e None) Devices() ([]Device, error) {
2727
return nil, nil
2828
}
2929

30+
// EnvVars returns an empty list of devices
31+
func (e None) EnvVars() ([]EnvVar, error) {
32+
return nil, nil
33+
}
34+
3035
// Mounts returns an empty list of mounts
3136
func (e None) Mounts() ([]Mount, error) {
3237
return nil, nil

internal/edits/edits.go

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func FromDiscoverer(d discover.Discover) (*cdi.ContainerEdits, error) {
5555
return nil, fmt.Errorf("failed to discover devices: %v", err)
5656
}
5757

58+
envs, err := d.EnvVars()
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to discover environment variables: %w", err)
61+
}
62+
5863
mounts, err := d.Mounts()
5964
if err != nil {
6065
return nil, fmt.Errorf("failed to discover mounts: %v", err)
@@ -74,6 +79,10 @@ func FromDiscoverer(d discover.Discover) (*cdi.ContainerEdits, error) {
7479
c.Append(edits)
7580
}
7681

82+
for _, e := range envs {
83+
c.Append(envvar(e).toEdits())
84+
}
85+
7786
for _, m := range mounts {
7887
c.Append(mount(m).toEdits())
7988
}

internal/edits/envvar.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
**/
17+
18+
package edits
19+
20+
import (
21+
"fmt"
22+
23+
"tags.cncf.io/container-device-interface/pkg/cdi"
24+
"tags.cncf.io/container-device-interface/specs-go"
25+
26+
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
27+
)
28+
29+
type envvar discover.EnvVar
30+
31+
// toEdits converts a discovered envvar to CDI Container Edits.
32+
func (d envvar) toEdits() *cdi.ContainerEdits {
33+
e := cdi.ContainerEdits{
34+
ContainerEdits: &specs.ContainerEdits{
35+
Env: []string{fmt.Sprintf("%s=%s", d.Name, d.Value)},
36+
},
37+
}
38+
return &e
39+
}

internal/platform-support/dgpu/by-path-hooks.go

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func (d *byPathHookDiscoverer) Devices() ([]discover.Device, error) {
4141
return nil, nil
4242
}
4343

44+
// EnvVars returns the empty list for the by-path hook discoverer
45+
func (d *byPathHookDiscoverer) EnvVars() ([]discover.EnvVar, error) {
46+
return nil, nil
47+
}
48+
4449
// Hooks returns the hooks for the GPU device.
4550
// The following hooks are detected:
4651
// 1. A hook to create /dev/dri/by-path symlinks

internal/platform-support/dgpu/nvsandboxutils.go

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (d *nvsandboxutilsDGPU) Devices() ([]discover.Device, error) {
106106
return devices, nil
107107
}
108108

109+
func (d *nvsandboxutilsDGPU) EnvVars() ([]discover.EnvVar, error) {
110+
return nil, nil
111+
}
112+
109113
// Hooks returns a hook to create the by-path symlinks for the discovered devices.
110114
func (d *nvsandboxutilsDGPU) Hooks() ([]discover.Hook, error) {
111115
if len(d.deviceLinks) == 0 {

pkg/nvcdi/workarounds-device-folder-permissions.go

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func (d *deviceFolderPermissions) Devices() ([]discover.Device, error) {
5555
return nil, nil
5656
}
5757

58+
// EnvVars are empty for this discoverer
59+
func (d *deviceFolderPermissions) EnvVars() ([]discover.EnvVar, error) {
60+
return nil, nil
61+
}
62+
5863
// Hooks returns a set of hooks that sets the file mode to 755 of parent folders for nested device nodes.
5964
func (d *deviceFolderPermissions) Hooks() ([]discover.Hook, error) {
6065
folders, err := d.getDeviceSubfolders()

0 commit comments

Comments
 (0)