mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-01-22 18:47:32 +00:00
Create individual links instead of processing CSV
This change switches to generating a OCI runtime hook to create individual symlinks instead of processing a CSV file in the hook. This allows for better reuse of the logic generating CDI specifications, for example. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
927ec78b6e
commit
3be16d8077
@ -21,12 +21,16 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type symlinks struct {
|
||||
type symlinkHook struct {
|
||||
None
|
||||
logger *logrus.Logger
|
||||
driverRoot string
|
||||
nvidiaCTKPath string
|
||||
csvFiles []string
|
||||
mountsFrom Discover
|
||||
@ -34,7 +38,7 @@ type symlinks struct {
|
||||
|
||||
// NewCreateSymlinksHook creates a discoverer for a hook that creates required symlinks in the container
|
||||
func NewCreateSymlinksHook(logger *logrus.Logger, csvFiles []string, mounts Discover, nvidiaCTKPath string) (Discover, error) {
|
||||
d := symlinks{
|
||||
d := symlinkHook{
|
||||
logger: logger,
|
||||
nvidiaCTKPath: nvidiaCTKPath,
|
||||
csvFiles: csvFiles,
|
||||
@ -45,17 +49,17 @@ func NewCreateSymlinksHook(logger *logrus.Logger, csvFiles []string, mounts Disc
|
||||
}
|
||||
|
||||
// Hooks returns a hook to create the symlinks from the required CSV files
|
||||
func (d symlinks) Hooks() ([]Hook, error) {
|
||||
var args []string
|
||||
for _, f := range d.csvFiles {
|
||||
args = append(args, "--csv-filename", f)
|
||||
}
|
||||
|
||||
links, err := d.getSpecificLinkArgs()
|
||||
func (d symlinkHook) Hooks() ([]Hook, error) {
|
||||
specificLinks, err := d.getSpecificLinks()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to determine specific links: %v", err)
|
||||
}
|
||||
args = append(args, links...)
|
||||
|
||||
csvSymlinks := d.getCSVFileSymlinks()
|
||||
var args []string
|
||||
for _, link := range append(csvSymlinks, specificLinks...) {
|
||||
args = append(args, "--link", link)
|
||||
}
|
||||
|
||||
hook := CreateNvidiaCTKHook(
|
||||
d.nvidiaCTKPath,
|
||||
@ -66,8 +70,8 @@ func (d symlinks) Hooks() ([]Hook, error) {
|
||||
return []Hook{hook}, nil
|
||||
}
|
||||
|
||||
// getSpecificLinkArgs returns the required specic links that need to be created
|
||||
func (d symlinks) getSpecificLinkArgs() ([]string, error) {
|
||||
// getSpecificLinks returns the required specic links that need to be created
|
||||
func (d symlinkHook) getSpecificLinks() ([]string, error) {
|
||||
mounts, err := d.mountsFrom.Mounts()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to discover mounts for ldcache update: %v", err)
|
||||
@ -99,11 +103,60 @@ func (d symlinks) getSpecificLinkArgs() ([]string, error) {
|
||||
if linkProcessed[link] {
|
||||
continue
|
||||
}
|
||||
linkProcessed[link] = true
|
||||
|
||||
linkPath := filepath.Join(filepath.Dir(m.Path), link)
|
||||
links = append(links, "--link", fmt.Sprintf("%v::%v", target, linkPath))
|
||||
linkProcessed[link] = true
|
||||
links = append(links, fmt.Sprintf("%v::%v", target, linkPath))
|
||||
}
|
||||
|
||||
return links, nil
|
||||
}
|
||||
|
||||
func (d symlinkHook) getCSVFileSymlinks() []string {
|
||||
chainLocator := lookup.NewSymlinkChainLocator(d.logger, d.driverRoot)
|
||||
|
||||
var candidates []string
|
||||
for _, file := range d.csvFiles {
|
||||
mountSpecs, err := csv.NewCSVFileParser(d.logger, file).Parse()
|
||||
if err != nil {
|
||||
d.logger.Debugf("Skipping CSV file %v: %v", file, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, ms := range mountSpecs {
|
||||
if ms.Type != csv.MountSpecSym {
|
||||
continue
|
||||
}
|
||||
targets, err := chainLocator.Locate(ms.Path)
|
||||
if err != nil {
|
||||
d.logger.Warnf("Failed to locate symlink %v", ms.Path)
|
||||
}
|
||||
candidates = append(candidates, targets...)
|
||||
}
|
||||
}
|
||||
|
||||
var links []string
|
||||
created := make(map[string]bool)
|
||||
// candidates is a list of absolute paths to symlinks in a chain, or the final target of the chain.
|
||||
for _, candidate := range candidates {
|
||||
target, err := symlinks.Resolve(candidate)
|
||||
if err != nil {
|
||||
d.logger.Debugf("Skipping invalid link: %v", err)
|
||||
continue
|
||||
} else if target == candidate {
|
||||
d.logger.Debugf("%v is not a symlink", candidate)
|
||||
continue
|
||||
}
|
||||
|
||||
link := fmt.Sprintf("%v::%v", target, candidate)
|
||||
if created[link] {
|
||||
d.logger.Debugf("skipping duplicate link: %v", link)
|
||||
continue
|
||||
}
|
||||
created[link] = true
|
||||
|
||||
links = append(links, link)
|
||||
}
|
||||
|
||||
return links
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user