Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions cmd/nvidia-ctk/cdi/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
Expand Down Expand Up @@ -352,14 +351,6 @@ type generatedSpecs struct {
func (g *generatedSpecs) Save(filename string) error {
filename = g.updateFilename(filename)

if filename == "" {
_, err := g.WriteTo(os.Stdout)
if err != nil {
return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err)
}
return nil
}

return g.Interface.Save(filename)
}

Expand Down
8 changes: 0 additions & 8 deletions cmd/nvidia-ctk/cdi/transform/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,5 @@ func (o transformOptions) getContents() ([]byte, error) {

// Save saves the CDI specification to the output file
func (o transformOptions) Save(s spec.Interface) error {
if o.output == "" {
_, err := s.WriteTo(os.Stdout)
if err != nil {
return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err)
}
return nil
}

return s.Save(o.output)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace tags.cncf.io/container-device-interface => ../container-device-interface
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
tags.cncf.io/container-device-interface v1.1.0 h1:RnxNhxF1JOu6CJUVpetTYvrXHdxw9j9jFYgZpI+anSY=
tags.cncf.io/container-device-interface v1.1.0/go.mod h1:76Oj0Yqp9FwTx/pySDc8Bxjpg+VqXfDb50cKAXVJ34Q=
tags.cncf.io/container-device-interface/specs-go v1.1.0 h1:QRZVeAceQM+zTZe12eyfuJuuzp524EKYwhmvLd+h+yQ=
tags.cncf.io/container-device-interface/specs-go v1.1.0/go.mod h1:u86hoFWqnh3hWz3esofRFKbI261bUlvUfLKGrDhJkgQ=
140 changes: 68 additions & 72 deletions pkg/nvcdi/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"io"
"os"
"path/filepath"
"strings"

"tags.cncf.io/container-device-interface/pkg/cdi"
producer "tags.cncf.io/container-device-interface/pkg/cdi-producer"
"tags.cncf.io/container-device-interface/specs-go"

"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
Expand All @@ -42,104 +44,98 @@ func New(opts ...Option) (Interface, error) {
return newBuilder(opts...).Build()
}

// Save writes the spec to the specified path and overwrites the file if it exists.
func (s *spec) Save(path string) error {
if s.transformOnSave != nil {
err := s.transformOnSave.Transform(s.Raw())
type Validator interface {
Validate(*specs.Spec) error
}

type Validators []Validator

func (v Validators) Validate(s *specs.Spec) error {
for _, vv := range v {
if vv == nil {
continue
}
err := vv.Validate(s)
if err != nil {
return fmt.Errorf("error applying transform: %w", err)
return err
}
}
path, err := s.normalizePath(path)
if err != nil {
return fmt.Errorf("failed to normalize path: %w", err)
}
return nil
}

specDir, filename := filepath.Split(path)
cache, _ := cdi.NewCache(
cdi.WithAutoRefresh(false),
cdi.WithSpecDirs(specDir),
)
if err := cache.WriteSpec(s.Raw(), filename); err != nil {
return fmt.Errorf("failed to write spec: %w", err)
type transfromAsValidator struct {
transform.Transformer
}

func fromTransform(t transform.Transformer) Validator {
if t == nil {
return nil
}
return &transfromAsValidator{t}
}

specDirAsRoot, err := os.OpenRoot(specDir)
if err != nil {
return fmt.Errorf("failed to open root: %w", err)
func (t *transfromAsValidator) Validate(s *specs.Spec) error {
if t == nil || t.Transformer == nil {
return nil
}
if err := t.Transform(s); err != nil {
return fmt.Errorf("error applying transform: %w", err)
}
defer specDirAsRoot.Close()
return nil
}

if err := specDirAsRoot.Chmod(filename, s.permissions); err != nil {
return fmt.Errorf("failed to set permissions on spec file: %w", err)
// Save writes the spec to the specified path and overwrites the file if it exists.
func (s *spec) Save(path string) error {
if pathWithExtension := s.ensureExtension(path); pathWithExtension != "" {
return producer.Save(s.Raw(), pathWithExtension,
s.producerOptions()...,
)
}
if _, err := s.WriteTo(os.Stdout); err != nil {
return fmt.Errorf("failed to write CDI spec to STDOUT: %w", err)
}

return nil
}

// WriteTo writes the spec to the specified writer.
func (s *spec) WriteTo(w io.Writer) (int64, error) {
tmpFile, err := os.CreateTemp("", "nvcdi-spec-*"+s.extension())
if err != nil {
return 0, err
}
tmpDir, tmpFileName := filepath.Split(tmpFile.Name())

tmpDirRoot, err := os.OpenRoot(tmpDir)
if err != nil {
return 0, err
}
defer tmpDirRoot.Close()
defer func() {
_ = tmpDirRoot.Remove(tmpFileName)
}()

if err := s.Save(tmpFile.Name()); err != nil {
return 0, err
}
return producer.WriteTo(s.Raw(), w,
s.producerOptions()...,
)
}

if err := tmpFile.Close(); err != nil {
return 0, fmt.Errorf("failed to close temporary file: %w", err)
func (s *spec) producerOptions() []producer.Option {
var validators Validators
if s.transformOnSave != nil {
validators = append(validators, fromTransform(s.transformOnSave))
}
validators = append(validators, cdi.SpecContentValidator)

savedFile, err := tmpDirRoot.Open(tmpFileName)
if err != nil {
return 0, fmt.Errorf("failed to open temporary file: %w", err)
return []producer.Option{
producer.WithOutputFormat(s.format),
producer.WithOverwrite(true),
producer.WithPermissions(s.permissions),
producer.WithValidator(validators),
}
defer savedFile.Close()

return savedFile.WriteTo(w)
}

// Raw returns a pointer to the raw spec.
func (s *spec) Raw() *specs.Spec {
return s.Spec
}

// normalizePath ensures that the specified path has a supported extension
func (s *spec) normalizePath(path string) (string, error) {
if ext := filepath.Ext(path); ext != ".yaml" && ext != ".json" {
path += s.extension()
func (s *spec) ensureExtension(filename string) string {
if filename == "" {
return ""
}

if filepath.Clean(filepath.Dir(path)) == "." {
pwd, err := os.Getwd()
if err != nil {
return path, fmt.Errorf("failed to get current working directory: %v", err)
}
path = filepath.Join(pwd, path)
ext := filepath.Ext(filename)
switch ext {
case ".yaml", ".json":
return filename
case ".yml":
return strings.TrimSuffix(filename, ".yml") + ".yaml"
default:
return filename + "." + s.format
}

return path, nil
}

func (s *spec) extension() string {
switch s.format {
case FormatJSON:
return ".json"
case FormatYAML:
return ".yaml"
}

return ".yaml"
}
4 changes: 3 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,14 @@ gopkg.in/yaml.v3
## explicit; go 1.12
sigs.k8s.io/yaml
sigs.k8s.io/yaml/goyaml.v2
# tags.cncf.io/container-device-interface v1.1.0
# tags.cncf.io/container-device-interface v1.1.0 => ../container-device-interface
## explicit; go 1.21
tags.cncf.io/container-device-interface/internal/validation
tags.cncf.io/container-device-interface/internal/validation/k8s
tags.cncf.io/container-device-interface/pkg/cdi
tags.cncf.io/container-device-interface/pkg/cdi-producer
tags.cncf.io/container-device-interface/pkg/parser
# tags.cncf.io/container-device-interface/specs-go v1.1.0
## explicit; go 1.19
tags.cncf.io/container-device-interface/specs-go
# tags.cncf.io/container-device-interface => ../container-device-interface

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading