2022-03-09 15:52:50 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
// charDevices is a discover for a list of character devices
|
|
|
|
type charDevices mounts
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
var _ Discover = (*charDevices)(nil)
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-03-15 12:17:49 +00:00
|
|
|
// NewFromCSVFiles creates a discoverer for the specified CSV files. A logger is also supplied.
|
|
|
|
// The constructed discoverer is comprised of a list, with each element in the list being associated with a
|
|
|
|
// single CSV files.
|
|
|
|
func NewFromCSVFiles(logger *logrus.Logger, files []string, root string) (Discover, error) {
|
2022-03-09 15:52:50 +00:00
|
|
|
if len(files) == 0 {
|
2022-03-15 12:17:49 +00:00
|
|
|
logger.Warnf("No CSV files specified")
|
2022-03-09 15:52:50 +00:00
|
|
|
return None{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
symlinkLocator := lookup.NewSymlinkLocator(logger, root)
|
2022-04-07 09:12:41 +00:00
|
|
|
locators := map[csv.MountSpecType]lookup.Locator{
|
|
|
|
csv.MountSpecDev: lookup.NewCharDeviceLocator(logger, root),
|
|
|
|
csv.MountSpecDir: lookup.NewDirectoryLocator(logger, root),
|
|
|
|
// Libraries and symlinks are handled in the same way
|
|
|
|
csv.MountSpecLib: symlinkLocator,
|
|
|
|
csv.MountSpecSym: symlinkLocator,
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
|
|
|
var discoverers []Discover
|
2022-03-10 13:00:24 +00:00
|
|
|
for _, filename := range files {
|
|
|
|
d, err := NewFromCSVFile(logger, locators, filename)
|
2022-03-09 15:52:50 +00:00
|
|
|
if err != nil {
|
2022-03-10 13:00:24 +00:00
|
|
|
logger.Warnf("Skipping CSV file %v: %v", filename, err)
|
2022-03-09 15:52:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
discoverers = append(discoverers, d)
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-03-10 13:00:24 +00:00
|
|
|
return &list{discoverers: discoverers}, nil
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-03-15 12:17:49 +00:00
|
|
|
// NewFromCSVFile creates a discoverer for the specified CSV file. A logger is also supplied.
|
|
|
|
// The constructed discoverer is comprised of a list, with each element in the list being associated with a particular
|
|
|
|
// MountSpecType.
|
2022-03-10 13:00:24 +00:00
|
|
|
func NewFromCSVFile(logger *logrus.Logger, locators map[csv.MountSpecType]lookup.Locator, filename string) (Discover, error) {
|
|
|
|
// Create a discoverer for each file-kind combination
|
2022-04-08 06:11:10 +00:00
|
|
|
targets, err := csv.NewCSVFileParser(logger, filename).Parse()
|
2022-03-10 13:00:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse CSV file: %v", err)
|
|
|
|
}
|
|
|
|
if len(targets) == 0 {
|
|
|
|
return nil, fmt.Errorf("CSV file is empty")
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
return newFromMountSpecs(logger, locators, targets)
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:00:24 +00:00
|
|
|
// newFromMountSpecs creates a discoverer for the CSV file. A logger is also supplied.
|
2022-03-15 12:17:49 +00:00
|
|
|
// A list of csvDiscoverers is returned, with each being associated with a single MountSpecType.
|
2022-04-08 06:52:07 +00:00
|
|
|
func newFromMountSpecs(logger *logrus.Logger, locators map[csv.MountSpecType]lookup.Locator, targets []*csv.MountSpec) (Discover, error) {
|
|
|
|
if len(targets) == 0 {
|
|
|
|
return &None{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var discoverers []Discover
|
2022-04-08 09:59:26 +00:00
|
|
|
var mountSpecTypes []csv.MountSpecType
|
2022-03-10 13:00:24 +00:00
|
|
|
candidatesByType := make(map[csv.MountSpecType][]string)
|
|
|
|
for _, t := range targets {
|
2022-04-08 09:59:26 +00:00
|
|
|
if _, exists := candidatesByType[t.Type]; !exists {
|
|
|
|
mountSpecTypes = append(mountSpecTypes, t.Type)
|
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
candidatesByType[t.Type] = append(candidatesByType[t.Type], t.Path)
|
|
|
|
}
|
|
|
|
|
2022-04-08 09:59:26 +00:00
|
|
|
for _, t := range mountSpecTypes {
|
2022-03-10 13:00:24 +00:00
|
|
|
locator, exists := locators[t]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("no locator defined for '%v'", t)
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
m := &mounts{
|
|
|
|
logger: logger,
|
|
|
|
lookup: locator,
|
2022-04-08 09:59:26 +00:00
|
|
|
required: candidatesByType[t],
|
2022-04-08 06:52:07 +00:00
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
switch t {
|
|
|
|
case csv.MountSpecDev:
|
|
|
|
// For device mount specs, we insert a charDevices into the list of discoverers.
|
|
|
|
discoverers = append(discoverers, (*charDevices)(m))
|
|
|
|
default:
|
|
|
|
discoverers = append(discoverers, m)
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
return &list{discoverers: discoverers}, nil
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
// Mounts returns the discovered mounts for the charDevices. Since this explicitly specifies a
|
|
|
|
// device list, the mounts are nil.
|
|
|
|
func (d *charDevices) Mounts() ([]Mount, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
|
2022-04-08 06:52:07 +00:00
|
|
|
// Devices returns the discovered devices for the charDevices. Here the device nodes are first
|
|
|
|
// discovered as mounts and these are converted to devices.
|
|
|
|
func (d *charDevices) Devices() ([]Device, error) {
|
|
|
|
devicesAsMounts, err := (*mounts)(d).Mounts()
|
2022-03-10 13:00:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var devices []Device
|
2022-04-08 06:52:07 +00:00
|
|
|
for _, mount := range devicesAsMounts {
|
|
|
|
devices = append(devices, Device(mount))
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|