2022-02-28 12:32:27 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2021 The CDI Authors
|
|
|
|
|
|
|
|
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 cdi
|
|
|
|
|
|
|
|
import (
|
2022-06-20 15:02:34 +00:00
|
|
|
"encoding/json"
|
2023-01-19 10:44:02 +00:00
|
|
|
"fmt"
|
2022-02-28 12:32:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-01-19 10:44:02 +00:00
|
|
|
"strings"
|
2022-09-28 11:46:25 +00:00
|
|
|
"sync"
|
2022-02-28 12:32:27 +00:00
|
|
|
|
|
|
|
oci "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
|
|
|
|
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
|
|
|
|
)
|
|
|
|
|
2023-01-19 10:44:02 +00:00
|
|
|
const (
|
|
|
|
// defaultSpecExt is the file extension for the default encoding.
|
|
|
|
defaultSpecExt = ".yaml"
|
|
|
|
)
|
2022-06-20 15:02:34 +00:00
|
|
|
|
2023-01-19 10:44:02 +00:00
|
|
|
var (
|
2022-06-20 15:02:34 +00:00
|
|
|
// Externally set CDI Spec validation function.
|
|
|
|
specValidator func(*cdi.Spec) error
|
2022-09-28 11:46:25 +00:00
|
|
|
validatorLock sync.RWMutex
|
2022-02-28 12:32:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Spec represents a single CDI Spec. It is usually loaded from a
|
|
|
|
// file and stored in a cache. The Spec has an associated priority.
|
|
|
|
// This priority is inherited from the associated priority of the
|
|
|
|
// CDI Spec directory that contains the CDI Spec file and is used
|
|
|
|
// to resolve conflicts if multiple CDI Spec files contain entries
|
|
|
|
// for the same fully qualified device.
|
|
|
|
type Spec struct {
|
|
|
|
*cdi.Spec
|
|
|
|
vendor string
|
|
|
|
class string
|
|
|
|
path string
|
|
|
|
priority int
|
|
|
|
devices map[string]*Device
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSpec reads the given CDI Spec file. The resulting Spec is
|
|
|
|
// assigned the given priority. If reading or parsing the Spec
|
|
|
|
// data fails ReadSpec returns a nil Spec and an error.
|
|
|
|
func ReadSpec(path string, priority int) (*Spec, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
switch {
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
return nil, err
|
|
|
|
case err != nil:
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("failed to read CDI Spec %q: %w", path, err)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
raw, err := ParseSpec(data)
|
2022-02-28 12:32:27 +00:00
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse CDI Spec %q: %w", path, err)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
2022-06-20 15:02:34 +00:00
|
|
|
if raw == nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse CDI Spec %q, no Spec data", path)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:44:02 +00:00
|
|
|
spec, err := newSpec(raw, path, priority)
|
2022-06-20 15:02:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-28 12:32:27 +00:00
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
return spec, nil
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:44:02 +00:00
|
|
|
// newSpec creates a new Spec from the given CDI Spec data. The
|
2022-02-28 12:32:27 +00:00
|
|
|
// Spec is marked as loaded from the given path with the given
|
2023-01-19 10:44:02 +00:00
|
|
|
// priority. If Spec data validation fails newSpec returns a nil
|
2022-02-28 12:32:27 +00:00
|
|
|
// Spec and an error.
|
2023-01-19 10:44:02 +00:00
|
|
|
func newSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
|
2022-06-20 15:02:34 +00:00
|
|
|
err := validateSpec(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-28 12:32:27 +00:00
|
|
|
|
|
|
|
spec := &Spec{
|
|
|
|
Spec: raw,
|
|
|
|
path: filepath.Clean(path),
|
|
|
|
priority: priority,
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:44:02 +00:00
|
|
|
if ext := filepath.Ext(spec.path); ext != ".yaml" && ext != ".json" {
|
|
|
|
spec.path += defaultSpecExt
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
spec.vendor, spec.class = ParseQualifier(spec.Kind)
|
|
|
|
|
|
|
|
if spec.devices, err = spec.validate(); err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("invalid CDI Spec: %w", err)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spec, nil
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
// Write the CDI Spec to the file associated with it during instantiation
|
2023-01-19 10:44:02 +00:00
|
|
|
// by newSpec() or ReadSpec().
|
|
|
|
func (s *Spec) write(overwrite bool) error {
|
2022-06-20 15:02:34 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
dir string
|
|
|
|
tmp *os.File
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
err = validateSpec(s.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(s.path) == ".yaml" {
|
|
|
|
data, err = yaml.Marshal(s.Spec)
|
|
|
|
} else {
|
|
|
|
data, err = json.Marshal(s.Spec)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return fmt.Errorf("failed to marshal Spec file: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dir = filepath.Dir(s.path)
|
|
|
|
err = os.MkdirAll(dir, 0o755)
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return fmt.Errorf("failed to create Spec dir: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err = os.CreateTemp(dir, "spec.*.tmp")
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return fmt.Errorf("failed to create Spec file: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
_, err = tmp.Write(data)
|
|
|
|
tmp.Close()
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return fmt.Errorf("failed to write Spec file: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = renameIn(dir, filepath.Base(tmp.Name()), filepath.Base(s.path), overwrite)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(tmp.Name())
|
2023-01-19 10:44:02 +00:00
|
|
|
err = fmt.Errorf("failed to write Spec file: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
// GetVendor returns the vendor of this Spec.
|
|
|
|
func (s *Spec) GetVendor() string {
|
|
|
|
return s.vendor
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClass returns the device class of this Spec.
|
|
|
|
func (s *Spec) GetClass() string {
|
|
|
|
return s.class
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevice returns the device for the given unqualified name.
|
|
|
|
func (s *Spec) GetDevice(name string) *Device {
|
|
|
|
return s.devices[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPath returns the filesystem path of this Spec.
|
|
|
|
func (s *Spec) GetPath() string {
|
|
|
|
return s.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPriority returns the priority of this Spec.
|
|
|
|
func (s *Spec) GetPriority() int {
|
|
|
|
return s.priority
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyEdits applies the Spec's global-scope container edits to an OCI Spec.
|
|
|
|
func (s *Spec) ApplyEdits(ociSpec *oci.Spec) error {
|
|
|
|
return s.edits().Apply(ociSpec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// edits returns the applicable global container edits for this spec.
|
|
|
|
func (s *Spec) edits() *ContainerEdits {
|
|
|
|
return &ContainerEdits{&s.ContainerEdits}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the Spec.
|
|
|
|
func (s *Spec) validate() (map[string]*Device, error) {
|
|
|
|
if err := validateVersion(s.Version); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-19 10:44:02 +00:00
|
|
|
|
|
|
|
minVersion, err := MinimumRequiredVersion(s.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not determine minumum required version: %v", err)
|
|
|
|
}
|
|
|
|
if newVersion(minVersion).IsGreaterThan(newVersion(s.Version)) {
|
|
|
|
return nil, fmt.Errorf("the spec version must be at least v%v", minVersion)
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
if err := ValidateVendorName(s.vendor); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := ValidateClassName(s.class); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.edits().Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
devices := make(map[string]*Device)
|
|
|
|
for _, d := range s.Devices {
|
|
|
|
dev, err := newDevice(s, d)
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("failed add device %q: %w", d.Name, err)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
if _, conflict := devices[d.Name]; conflict {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("invalid spec, multiple device %q", d.Name)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
devices[d.Name] = dev
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateVersion checks whether the specified spec version is supported.
|
|
|
|
func validateVersion(version string) error {
|
2023-01-19 10:44:02 +00:00
|
|
|
if !validSpecVersions.isValidVersion(version) {
|
|
|
|
return fmt.Errorf("invalid version %q", version)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
// ParseSpec parses CDI Spec data into a raw CDI Spec.
|
|
|
|
func ParseSpec(data []byte) (*cdi.Spec, error) {
|
|
|
|
var raw *cdi.Spec
|
2022-02-28 12:32:27 +00:00
|
|
|
err := yaml.UnmarshalStrict(data, &raw)
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal CDI Spec: %w", err)
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
2022-06-20 15:02:34 +00:00
|
|
|
return raw, nil
|
2022-02-28 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
// SetSpecValidator sets a CDI Spec validator function. This function
|
|
|
|
// is used for extra CDI Spec content validation whenever a Spec file
|
2023-01-19 10:44:02 +00:00
|
|
|
// loaded (using ReadSpec() or written (using WriteSpec()).
|
2022-06-20 15:02:34 +00:00
|
|
|
func SetSpecValidator(fn func(*cdi.Spec) error) {
|
2022-09-28 11:46:25 +00:00
|
|
|
validatorLock.Lock()
|
|
|
|
defer validatorLock.Unlock()
|
2022-06-20 15:02:34 +00:00
|
|
|
specValidator = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateSpec validates the Spec using the extneral validator.
|
|
|
|
func validateSpec(raw *cdi.Spec) error {
|
2022-09-28 11:46:25 +00:00
|
|
|
validatorLock.RLock()
|
|
|
|
defer validatorLock.RUnlock()
|
|
|
|
|
2022-06-20 15:02:34 +00:00
|
|
|
if specValidator == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := specValidator(raw)
|
|
|
|
if err != nil {
|
2023-01-19 10:44:02 +00:00
|
|
|
return fmt.Errorf("Spec validation failed: %w", err)
|
2022-06-20 15:02:34 +00:00
|
|
|
}
|
2022-02-28 12:32:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-19 10:44:02 +00:00
|
|
|
|
|
|
|
// GenerateSpecName generates a vendor+class scoped Spec file name. The
|
|
|
|
// name can be passed to WriteSpec() to write a Spec file to the file
|
|
|
|
// system.
|
|
|
|
//
|
|
|
|
// vendor and class should match the vendor and class of the CDI Spec.
|
|
|
|
// The file name is generated without a ".json" or ".yaml" extension.
|
|
|
|
// The caller can append the desired extension to choose a particular
|
|
|
|
// encoding. Otherwise WriteSpec() will use its default encoding.
|
|
|
|
//
|
|
|
|
// This function always returns the same name for the same vendor/class
|
|
|
|
// combination. Therefore it cannot be used as such to generate multiple
|
|
|
|
// Spec file names for a single vendor and class.
|
|
|
|
func GenerateSpecName(vendor, class string) string {
|
|
|
|
return vendor + "-" + class
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateTransientSpecName generates a vendor+class scoped transient
|
|
|
|
// Spec file name. The name can be passed to WriteSpec() to write a Spec
|
|
|
|
// file to the file system.
|
|
|
|
//
|
|
|
|
// Transient Specs are those whose lifecycle is tied to that of some
|
|
|
|
// external entity, for instance a container. vendor and class should
|
|
|
|
// match the vendor and class of the CDI Spec. transientID should be
|
|
|
|
// unique among all CDI users on the same host that might generate
|
|
|
|
// transient Spec files using the same vendor/class combination. If
|
|
|
|
// the external entity to which the lifecycle of the tranient Spec
|
|
|
|
// is tied to has a unique ID of its own, then this is usually a
|
|
|
|
// good choice for transientID.
|
|
|
|
//
|
|
|
|
// The file name is generated without a ".json" or ".yaml" extension.
|
|
|
|
// The caller can append the desired extension to choose a particular
|
|
|
|
// encoding. Otherwise WriteSpec() will use its default encoding.
|
|
|
|
func GenerateTransientSpecName(vendor, class, transientID string) string {
|
|
|
|
transientID = strings.ReplaceAll(transientID, "/", "_")
|
|
|
|
return GenerateSpecName(vendor, class) + "_" + transientID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateNameForSpec generates a name for the given Spec using
|
|
|
|
// GenerateSpecName with the vendor and class taken from the Spec.
|
|
|
|
// On success it returns the generated name and a nil error. If
|
|
|
|
// the Spec does not contain a valid vendor or class, it returns
|
|
|
|
// an empty name and a non-nil error.
|
|
|
|
func GenerateNameForSpec(raw *cdi.Spec) (string, error) {
|
|
|
|
vendor, class := ParseQualifier(raw.Kind)
|
|
|
|
if vendor == "" {
|
|
|
|
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenerateSpecName(vendor, class), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateNameForTransientSpec generates a name for the given transient
|
|
|
|
// Spec using GenerateTransientSpecName with the vendor and class taken
|
|
|
|
// from the Spec. On success it returns the generated name and a nil error.
|
|
|
|
// If the Spec does not contain a valid vendor or class, it returns an
|
|
|
|
// an empty name and a non-nil error.
|
|
|
|
func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, error) {
|
|
|
|
vendor, class := ParseQualifier(raw.Kind)
|
|
|
|
if vendor == "" {
|
|
|
|
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenerateTransientSpecName(vendor, class, transientID), nil
|
|
|
|
}
|