Use DefaultExecutableDir to determine default paths

This change adds a DefaultExecutableDir = /usr/bin constant that is used
to construct default paths for executables instead of specifying these
explicitly.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-04-08 07:18:49 +02:00
parent 1ebd48dea6
commit f07d110e85
3 changed files with 13 additions and 8 deletions

View File

@ -19,18 +19,18 @@ 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 ( const (
nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
nvidiaContainerRuntimeHookDefaultPath = "/usr/bin/nvidia-container-runtime-hook" nvidiaContainerToolkitExecutable = "nvidia-container-toolkit"
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
@ -52,7 +52,7 @@ type stableRuntimeModifier struct {
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(nvidiaContainerRuntimeHookExecutable)
if err != nil { if err != nil {
path = nvidiaContainerRuntimeHookDefaultPath path = filepath.Join(config.DefaultExecutableDir, nvidiaContainerRuntimeHookExecutable)
_, err = os.Stat(path) _, err = os.Stat(path)
if err != nil { if err != nil {
return err return err

View File

@ -31,6 +31,9 @@ const (
) )
var ( var (
// DefaultExecutableDir specifies the default path to use for executables if they cannot be located in the path.
DefaultExecutableDir = "/usr/bin"
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"
@ -41,14 +44,13 @@ type legacy struct {
var _ Discover = (*legacy)(nil) var _ Discover = (*legacy)(nil)
const ( const (
nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
nvidiaContainerRuntimeHookDefaultFilePath = "/usr/bin/nvidia-container-runtime-hook"
) )
// Hooks returns the "legacy" NVIDIA Container Runtime hook. This mirrors the behaviour of the stable // Hooks returns the "legacy" NVIDIA Container Runtime hook. This mirrors the behaviour of the stable
// modifier. // modifier.
func (d legacy) Hooks() ([]Hook, error) { func (d legacy) Hooks() ([]Hook, error) {
hookPath := nvidiaContainerRuntimeHookDefaultFilePath hookPath := filepath.Join(config.DefaultExecutableDir, nvidiaContainerRuntimeHookExecutable)
targets, err := d.lookup.Locate(nvidiaContainerRuntimeHookExecutable) targets, err := d.lookup.Locate(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", nvidiaContainerRuntimeHookExecutable, err)