Skip to content

Commit

Permalink
Switch to CDI producer API
Browse files Browse the repository at this point in the history
This change switces to the CDI producer API to write CDI specifications.

Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Jan 21, 2025
1 parent 5ef7a39 commit 43fe7c4
Show file tree
Hide file tree
Showing 26 changed files with 547 additions and 885 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
golang.org/x/mod v0.22.0
golang.org/x/sys v0.29.0
tags.cncf.io/container-device-interface v0.8.0
tags.cncf.io/container-device-interface/api/producer v0.0.0
tags.cncf.io/container-device-interface/specs-go v0.8.0
)

Expand All @@ -39,6 +40,6 @@ require (

replace (
tags.cncf.io/container-device-interface => ../container-device-interface
tags.cncf.io/container-device-interface/specs-go => ../container-device-interface/specs-go
tags.cncf.io/container-device-interface/api/producer => ../container-device-interface/api/producer
tags.cncf.io/container-device-interface/specs-go => ../container-device-interface/specs-go
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,3 @@ 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.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
tags.cncf.io/container-device-interface v0.8.0 h1:8bCFo/g9WODjWx3m6EYl3GfUG31eKJbaggyBDxEldRc=
tags.cncf.io/container-device-interface v0.8.0/go.mod h1:Apb7N4VdILW0EVdEMRYXIDVRZfNJZ+kmEUss2kRRQ6Y=
tags.cncf.io/container-device-interface/specs-go v0.8.0 h1:QYGFzGxvYK/ZLMrjhvY0RjpUavIn4KcmRmVP/JjdBTA=
tags.cncf.io/container-device-interface/specs-go v0.8.0/go.mod h1:BhJIkjjPh4qpys+qm4DAYtUyryaTDg9zris+AczXyws=
44 changes: 30 additions & 14 deletions pkg/nvcdi/spec/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@ import (
"fmt"
"os"

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

"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
)

type builder struct {
raw *specs.Spec
raw *cdi.Spec
version string
vendor string
class string
deviceSpecs []specs.Device
edits specs.ContainerEdits
deviceSpecs []cdi.Device
edits cdi.ContainerEdits
format string

mergedDeviceOptions []transform.MergedDeviceOption
noSimplify bool
permissions os.FileMode

transformOnSave transform.Transformer
detectMinimumVersion bool
transformOnSave transform.Transformer
}

// newBuilder creates a new spec builder with the supplied options
Expand All @@ -64,7 +65,7 @@ func newBuilder(opts ...Option) *builder {
}
}
if s.version == "" || s.version == DetectMinimumVersion {
s.transformOnSave = &setMinimumRequiredVersion{}
s.detectMinimumVersion = true
s.version = cdi.CurrentVersion
}
if s.vendor == "" {
Expand All @@ -86,7 +87,7 @@ func newBuilder(opts ...Option) *builder {
func (o *builder) Build() (*spec, error) {
raw := o.raw
if raw == nil {
raw = &specs.Spec{
raw = &cdi.Spec{
Version: o.version,
Kind: fmt.Sprintf("%s/%s", o.vendor, o.class),
Devices: o.deviceSpecs,
Expand Down Expand Up @@ -114,11 +115,26 @@ func (o *builder) Build() (*spec, error) {
}
}

options := []producer.Option{
producer.WithDetectMinimumVersion(o.detectMinimumVersion),
producer.WithPermissions(o.permissions),
}
switch o.format {
case FormatJSON:
options = append(options, producer.WithSpecFormat(producer.SpecFormatJSON))
case FormatYAML:
options = append(options, producer.WithSpecFormat(producer.SpecFormatYAML))
}

producer, err := producer.NewSpecWriter(options...)
if err != nil {
return nil, err
}

s := spec{
Spec: raw,
format: o.format,
permissions: o.permissions,
raw: raw,
transformOnSave: o.transformOnSave,
SpecWriter: producer,
}
return &s, nil
}
Expand All @@ -127,14 +143,14 @@ func (o *builder) Build() (*spec, error) {
type Option func(*builder)

// WithDeviceSpecs sets the device specs for the spec builder
func WithDeviceSpecs(deviceSpecs []specs.Device) Option {
func WithDeviceSpecs(deviceSpecs []cdi.Device) Option {
return func(o *builder) {
o.deviceSpecs = deviceSpecs
}
}

// WithEdits sets the container edits for the spec builder
func WithEdits(edits specs.ContainerEdits) Option {
func WithEdits(edits cdi.ContainerEdits) Option {
return func(o *builder) {
o.edits = edits
}
Expand Down Expand Up @@ -176,7 +192,7 @@ func WithNoSimplify(noSimplify bool) Option {
}

// WithRawSpec sets the raw spec for the spec builder
func WithRawSpec(raw *specs.Spec) Option {
func WithRawSpec(raw *cdi.Spec) Option {
return func(o *builder) {
o.raw = raw
}
Expand Down
35 changes: 0 additions & 35 deletions pkg/nvcdi/spec/set-minimum-version.go

This file was deleted.

108 changes: 20 additions & 88 deletions pkg/nvcdi/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@
package spec

import (
"fmt"
"io"
"os"
"path/filepath"

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

"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
)

type spec struct {
*specs.Spec
format string
permissions os.FileMode
raw *cdi.Spec
transformOnSave transform.Transformer
*producer.SpecWriter
}

var _ Interface = (*spec)(nil)
Expand All @@ -42,96 +38,32 @@ func New(opts ...Option) (Interface, error) {
return newBuilder(opts...).Build()
}

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

// 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())
err := s.transformOnSave.Transform(s.raw)
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)
}

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

if err := os.Chmod(path, s.permissions); err != nil {
return fmt.Errorf("failed to set permissions on spec file: %w", err)
}

return nil
// TODO: We should add validation here.
_, err := s.SpecWriter.Save(s.raw, path)
return err
}

// WriteTo writes the spec to the specified writer.
// WriteTo writes the configured spec to the specified writer.
func (s *spec) WriteTo(w io.Writer) (int64, error) {
name, err := cdi.GenerateNameForSpec(s.Raw())
if err != nil {
return 0, err
}

path, _ := s.normalizePath(name)
tmpFile, err := os.CreateTemp("", "*"+filepath.Base(path))
if err != nil {
return 0, err
}
defer os.Remove(tmpFile.Name())

if err := s.Save(tmpFile.Name()); err != nil {
return 0, err
}

err = tmpFile.Close()
if err != nil {
return 0, fmt.Errorf("failed to close temporary file: %w", err)
}

r, err := os.Open(tmpFile.Name())
if err != nil {
return 0, fmt.Errorf("failed to open temporary file: %w", err)
}
defer r.Close()

return io.Copy(w, r)
}

// 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()
}

if filepath.Clean(filepath.Dir(path)) == "." {
pwd, err := os.Getwd()
if s.transformOnSave != nil {
err := s.transformOnSave.Transform(s.raw)
if err != nil {
return path, fmt.Errorf("failed to get current working directory: %v", err)
return 0, err
}
path = filepath.Join(pwd, path)
}

return path, nil
}

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

return ".yaml"
// TODO: We should add validation here.
return s.SpecWriter.WriteSpecTo(s.raw, w)
}
10 changes: 8 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ gopkg.in/yaml.v3
# sigs.k8s.io/yaml v1.3.0
## explicit; go 1.12
sigs.k8s.io/yaml
# tags.cncf.io/container-device-interface v0.8.0
# tags.cncf.io/container-device-interface v0.8.0 => ../container-device-interface
## explicit; go 1.20
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/parser
# tags.cncf.io/container-device-interface/specs-go v0.8.0
# tags.cncf.io/container-device-interface/api/producer v0.0.0 => ../container-device-interface/api/producer
## explicit; go 1.20
tags.cncf.io/container-device-interface/api/producer
# tags.cncf.io/container-device-interface/specs-go v0.8.0 => ../container-device-interface/specs-go
## explicit; go 1.19
tags.cncf.io/container-device-interface/specs-go
# tags.cncf.io/container-device-interface => ../container-device-interface
# tags.cncf.io/container-device-interface/api/producer => ../container-device-interface/api/producer
# tags.cncf.io/container-device-interface/specs-go => ../container-device-interface/specs-go
34 changes: 34 additions & 0 deletions vendor/tags.cncf.io/container-device-interface/api/producer/api.go

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

Loading

0 comments on commit 43fe7c4

Please sign in to comment.