Merge branch 'cleanup-default-executable-dir' into 'master'

Clean up NVIDIA Container Runtime Hook executable specification

See merge request nvidia/container-toolkit/container-toolkit!126
This commit is contained in:
Evan Lezar 2022-04-08 10:29:25 +00:00
commit 43ee7f1cd2
5 changed files with 28 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -60,8 +61,8 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error {
// nvidia-container-runtime or docker when specifying the --gpus flag. // nvidia-container-runtime or docker when specifying the --gpus flag.
func isNVIDIAContainerRuntimeHook(hook *specs.Hook) bool { func isNVIDIAContainerRuntimeHook(hook *specs.Hook) bool {
bins := map[string]struct{}{ bins := map[string]struct{}{
nvidiaContainerRuntimeHookExecutable: {}, config.NVIDIAContainerRuntimeHookExecutable: {},
nvidiaContainerToolkitExecutable: {}, config.NVIDIAContainerToolkitExecutable: {},
} }
_, exists := bins[filepath.Base(hook.Path)] _, exists := bins[filepath.Base(hook.Path)]

View File

@ -19,20 +19,15 @@ package modifier
import ( import (
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const (
nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
nvidiaContainerRuntimeHookDefaultPath = "/usr/bin/nvidia-container-runtime-hook"
nvidiaContainerToolkitExecutable = "nvidia-container-toolkit"
)
// NewStableRuntimeModifier creates an OCI spec modifier that inserts the NVIDIA Container Runtime Hook into an OCI // NewStableRuntimeModifier creates an OCI spec modifier that inserts the NVIDIA Container Runtime Hook into an OCI
// spec. The specified logger is used to capture log output. // spec. The specified logger is used to capture log output.
func NewStableRuntimeModifier(logger *logrus.Logger) oci.SpecModifier { func NewStableRuntimeModifier(logger *logrus.Logger) oci.SpecModifier {
@ -50,9 +45,9 @@ type stableRuntimeModifier struct {
// Modify applies the required modification to the incoming OCI spec, inserting the nvidia-container-runtime-hook // Modify applies the required modification to the incoming OCI spec, inserting the nvidia-container-runtime-hook
// as a prestart hook. // as a prestart hook.
func (m stableRuntimeModifier) Modify(spec *specs.Spec) error { func (m stableRuntimeModifier) Modify(spec *specs.Spec) error {
path, err := exec.LookPath(nvidiaContainerRuntimeHookExecutable) path, err := exec.LookPath(config.NVIDIAContainerRuntimeHookExecutable)
if err != nil { if err != nil {
path = nvidiaContainerRuntimeHookDefaultPath path = filepath.Join(config.DefaultExecutableDir, config.NVIDIAContainerRuntimeHookExecutable)
_, err = os.Stat(path) _, err = os.Stat(path)
if err != nil { if err != nil {
return err return err
@ -66,7 +61,7 @@ func (m stableRuntimeModifier) Modify(spec *specs.Spec) error {
spec.Hooks = &specs.Hooks{} spec.Hooks = &specs.Hooks{}
} else if len(spec.Hooks.Prestart) != 0 { } else if len(spec.Hooks.Prestart) != 0 {
for _, hook := range spec.Hooks.Prestart { for _, hook := range spec.Hooks.Prestart {
if strings.Contains(hook.Path, nvidiaContainerRuntimeHookExecutable) { if strings.Contains(hook.Path, config.NVIDIAContainerRuntimeHookExecutable) {
m.logger.Infof("existing nvidia prestart hook found in OCI spec") m.logger.Infof("existing nvidia prestart hook found in OCI spec")
return nil return nil
} }

View File

@ -103,7 +103,7 @@ func (m command) run(c *cli.Context, cfg *config) error {
var candidates []string var candidates []string
for _, file := range csvFiles { for _, file := range csvFiles {
mountSpecs, err := csv.ParseFile(m.logger, file) mountSpecs, err := csv.NewCSVFileParser(m.logger, file).Parse()
if err != nil { if err != nil {
m.logger.Debugf("Skipping CSV file %v: %v", file, err) m.logger.Debugf("Skipping CSV file %v: %v", file, err)
continue continue

View File

@ -31,6 +31,14 @@ const (
) )
var ( var (
// DefaultExecutableDir specifies the default path to use for executables if they cannot be located in the path.
DefaultExecutableDir = "/usr/bin"
// NVIDIAContainerRuntimeHookExecutable is the executable name for the NVIDIA Container Runtime Hook
NVIDIAContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
// NVIDIAContainerToolkitExecutable is the executable name for the NVIDIA Container Toolkit (an alias for the NVIDIA Container Runtime Hook)
NVIDIAContainerToolkitExecutable = "nvidia-container-toolkit"
configDir = "/etc/" configDir = "/etc/"
) )

View File

@ -17,6 +17,9 @@
package discover package discover
import ( import (
"path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -40,22 +43,18 @@ type legacy struct {
var _ Discover = (*legacy)(nil) var _ Discover = (*legacy)(nil)
const ( // Hooks returns the "legacy" NVIDIA Container Runtime hook. This hook calls out
nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" // to the nvidia-container-cli to make modifications to the container as defined
nvidiaContainerRuntimeHookDefaultFilePath = "/usr/bin/nvidia-container-runtime-hook" // in libnvidia-container.
)
// Hooks returns the "legacy" NVIDIA Container Runtime hook. This mirrors the behaviour of the stable
// modifier.
func (d legacy) Hooks() ([]Hook, error) { func (d legacy) Hooks() ([]Hook, error) {
hookPath := nvidiaContainerRuntimeHookDefaultFilePath hookPath := filepath.Join(config.DefaultExecutableDir, config.NVIDIAContainerRuntimeHookExecutable)
targets, err := d.lookup.Locate(nvidiaContainerRuntimeHookExecutable) targets, err := d.lookup.Locate(config.NVIDIAContainerRuntimeHookExecutable)
if err != nil { if err != nil {
d.logger.Warnf("Failed to locate %v: %v", nvidiaContainerRuntimeHookExecutable, err) d.logger.Warnf("Failed to locate %v: %v", config.NVIDIAContainerRuntimeHookExecutable, err)
} else if len(targets) == 0 { } else if len(targets) == 0 {
d.logger.Warnf("%v not found", nvidiaContainerRuntimeHookExecutable) d.logger.Warnf("%v not found", config.NVIDIAContainerRuntimeHookExecutable)
} else { } else {
d.logger.Debugf("Found %v candidates: %v", nvidiaContainerRuntimeHookExecutable, targets) d.logger.Debugf("Found %v candidates: %v", config.NVIDIAContainerRuntimeHookExecutable, targets)
hookPath = targets[0] hookPath = targets[0]
} }
d.logger.Debugf("Using NVIDIA Container Runtime Hook path %v", hookPath) d.logger.Debugf("Using NVIDIA Container Runtime Hook path %v", hookPath)