Skip to content

Commit

Permalink
Merge pull request #219 from bart0sh/PR020-remove-registry-API
Browse files Browse the repository at this point in the history
Remove deprecated registry API
  • Loading branch information
elezar authored Aug 28, 2024
2 parents b355496 + 602c093 commit 96643f9
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 1,066 deletions.
2 changes: 1 addition & 1 deletion cmd/cdi/cmd/cdi-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func cdiPrintCacheErrors() {
return
}

fmt.Printf("CDI Cache has errors:\n")
fmt.Printf("CDI cache has errors:\n")
for path, specErrors := range cdiErrors {
fmt.Printf("Spec file %s:\n", path)
for idx, err := range specErrors {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cdi/cmd/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"github.com/spf13/cobra"
)

// classesCmd is our command for listing device classes in the registry.
// classesCmd is our command for listing device classes in the cache.
var classesCmd = &cobra.Command{
Use: "classes",
Short: "List CDI device classes",
Long: `List CDI device classes found in the registry.`,
Long: `List CDI device classes found in the cache.`,
Run: func(cmd *cobra.Command, args []string) {
cdiListClasses()
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/cdi/cmd/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type devicesFlags struct {
output string
}

// devicesCmd is our command for listing devices found in the CDI registry.
// devicesCmd is our command for listing devices found in the CDI cache.
var devicesCmd = &cobra.Command{
Aliases: []string{"devs", "dev"},
Use: "devices",
Short: "List devices in the CDI registry",
Short: "List devices in the CDI cache",
Long: `
The 'devices' command lists devices found in the CDI registry.`,
The 'devices' command lists devices found in the CDI cache.`,
Run: func(cmd *cobra.Command, args []string) {
cdiListDevices(devicesCfg.verbose, devicesCfg.output)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/cdi/cmd/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var dirsCmd = &cobra.Command{
Use: "dirs",
Short: "Show CDI Spec directories in use",
Long: `
Show which directories are used by the registry to discover and
Show which directories are used by the cache to discover and
load CDI Specs. The later an entry is in the list the higher its
priority. This priority is inherited by Spec files loaded from
the directory and is used to resolve device conflicts. If there
Expand Down
6 changes: 3 additions & 3 deletions cmd/cdi/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ information to show upon each refresh.`,

func monitorSpecDirs(args ...string) {
var (
registry = cdi.GetRegistry()
specDirs = registry.GetSpecDirectories()
cache = cdi.GetDefaultCache()
specDirs = cache.GetSpecDirectories()
dirWatch *fsnotify.Watcher
err error
done chan error
Expand Down Expand Up @@ -81,7 +81,7 @@ func monitorSpecDirs(args ...string) {

go func() {
var (
// don't print registry content more often than this
// don't print cache content more often than this
oneSecond = 1 * time.Second
timer *time.Timer
)
Expand Down
17 changes: 11 additions & 6 deletions cmd/cdi/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ var (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cdi",
Short: "Inspect and interact with the CDI Registry",
Short: "Inspect and interact with the CDI cache",
Long: `
The 'cdi' utility allows you to inspect and interact with the
CDI Registry. Various commands are available for listing CDI
CDI cache. Various commands are available for listing CDI
Spec files, vendors, classes, devices, validating the content
of the registry, injecting devices into OCI Specs, and for
monitoring changes in the Registry.
of the cache, injecting devices into OCI Specs, and for
monitoring changes in the cache.
See cdi --help for a list of available commands. You can get
additional help about <command> by using 'cdi <command> -h'.`,
Expand All @@ -68,11 +68,16 @@ func initSpecDirs() {
cdi.SetSpecValidator(validate.WithSchema(s))

if len(specDirs) > 0 {
cdi.GetRegistry(
cache, err := cdi.NewCache(
cdi.WithSpecDirs(specDirs...),
)
if len(cdi.GetRegistry().GetErrors()) > 0 {
if err != nil {
fmt.Printf("failed to create CDI cache: %v\n", err)
os.Exit(1)
}
if len(cache.GetErrors()) > 0 {
cdiPrintCacheErrors()
os.Exit(1)
}
}
}
6 changes: 3 additions & 3 deletions cmd/cdi/cmd/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var specsCmd = &cobra.Command{
Use: "specs [vendor-list]",
Short: "List available CDI Specs",
Long: fmt.Sprintf(`
The 'specs' command lists all CDI Specs present in the registry.
The 'specs' command lists all CDI Specs present in the cache.
If a vendor list is given, only CDI Specs by the given vendors are
listed. The CDI Specs are discovered and loaded to the registry
from CDI Spec directories. The default CDI Spec directories are:
listed. The CDI Specs are discovered and loaded to the cache from
CDI Spec directories. The default CDI Spec directories are:
%s.`, strings.Join(cdi.DefaultSpecDirs, ", ")),
Run: func(cmd *cobra.Command, vendors []string) {
cdiListSpecs(specCfg.verbose, specCfg.output, vendors...)
Expand Down
15 changes: 8 additions & 7 deletions cmd/cdi/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ import (
"tags.cncf.io/container-device-interface/pkg/cdi"
)

// validateCmd is our CDI command for validating CDI Spec files in the registry.
// validateCmd is our CDI command for validating CDI Spec files in the cache.
var validateCmd = &cobra.Command{
Use: "validate",
Short: "List CDI registry errors",
Short: "List CDI cache errors",
Long: `
The 'validate' command lists errors encountered during the population
of the CDI registry. It exits with an exit status of 1 if any errors
were reported by the registry.`,
of the CDI cache. It exits with an exit status of 1 if any errors
were reported by the cache.`,
Run: func(cmd *cobra.Command, args []string) {
cdiErrors := cdi.GetRegistry().GetErrors()
cache := cdi.GetDefaultCache()
cdiErrors := cache.GetErrors()
if len(cdiErrors) == 0 {
fmt.Printf("No CDI Registry errors.\n")
fmt.Printf("No CDI cache errors.\n")
return
}

fmt.Printf("CDI Registry has errors:\n")
fmt.Printf("CDI cache has errors:\n")
for path, specErrors := range cdiErrors {
fmt.Printf("Spec file %s:\n", path)
for idx, err := range specErrors {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cdi/cmd/vendors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var vendorsCmd = &cobra.Command{
Use: "vendors",
Short: "List vendors",
Long: `List vendors with CDI Specs in the registry.`,
Long: `List vendors with CDI Specs in the cache.`,
Run: func(cmd *cobra.Command, args []string) {
cdiListVendors()
},
Expand Down
51 changes: 19 additions & 32 deletions pkg/cdi/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,11 @@
// available and instantiated the first time it is referenced directly
// or indirectly. The most frequently used cache functions are available
// as identically named package level functions which operate on the
// default cache instance. Moreover, the registry also operates on the
// same default cache. We plan to deprecate the registry and eventually
// remove it in a future release.
//
// # CDI Registry
//
// Note: the Registry and its related interfaces are deprecated and will
// be removed in a future version. Please use the default cache and its
// related package-level function instead.
//
// The primary interface to interact with CDI devices is the Registry. It
// is essentially a cache of all Specs and devices discovered in standard
// CDI directories on the host. The registry has two main functionality,
// injecting devices into an OCI Spec and refreshing the cache of CDI
// Specs and devices.
// default cache instance.
//
// # Device Injection
//
// Using the Registry one can inject CDI devices into a container with code
// Using the Cache one can inject CDI devices into a container with code
// similar to the following snippet:
//
// import (
Expand All @@ -63,13 +49,14 @@
// log "github.com/sirupsen/logrus"
//
// "tags.cncf.io/container-device-interface/pkg/cdi"
// oci "github.com/opencontainers/runtime-spec/specs-go"
// "github.com/opencontainers/runtime-spec/specs-go"
// )
//
// func injectCDIDevices(spec *oci.Spec, devices []string) error {
// func injectCDIDevices(spec *specs.Spec, devices []string) error {
// log.Debug("pristine OCI Spec: %s", dumpSpec(spec))
//
// unresolved, err := cdi.GetRegistry().InjectDevices(spec, devices)
// cache := cdi.GetDefaultCache()
// unresolved, err := cache.InjectDevices(spec, devices)
// if err != nil {
// return fmt.Errorf("CDI device injection failed: %w", err)
// }
Expand Down Expand Up @@ -106,17 +93,17 @@
// log "github.com/sirupsen/logrus"
//
// "tags.cncf.io/container-device-interface/pkg/cdi"
// oci "github.com/opencontainers/runtime-spec/specs-go"
// "github.com/opencontainers/runtime-spec/specs-go"
// )
//
// func injectCDIDevices(spec *oci.Spec, devices []string) error {
// registry := cdi.GetRegistry()
// func injectCDIDevices(spec *specs.Spec, devices []string) error {
// cache := cdi.GetDefaultCache()
//
// if err := registry.Refresh(); err != nil {
// if err := cache.Refresh(); err != nil {
// // Note:
// // It is up to the implementation to decide whether
// // to abort injection on errors. A failed Refresh()
// // does not necessarily render the registry unusable.
// // does not necessarily render the cache unusable.
// // For instance, a parse error in a Spec file for
// // vendor A does not have any effect on devices of
// // vendor B...
Expand All @@ -125,7 +112,7 @@
//
// log.Debug("pristine OCI Spec: %s", dumpSpec(spec))
//
// unresolved, err := registry.InjectDevices(spec, devices)
// unresolved, err := cache.InjectDevices(spec, devices)
// if err != nil {
// return fmt.Errorf("CDI device injection failed: %w", err)
// }
Expand Down Expand Up @@ -192,7 +179,7 @@
// )
//
// func generateDeviceSpecs() error {
// registry := cdi.GetRegistry()
// cache := specs.GetDefaultCache()
// spec := &specs.Spec{
// Version: specs.CurrentVersion,
// Kind: vendor+"/"+class,
Expand All @@ -210,7 +197,7 @@
// return fmt.Errorf("failed to generate Spec name: %w", err)
// }
//
// return registry.SpecDB().WriteSpec(spec, specName)
// return cache.WriteSpec(spec, specName)
// }
//
// Similarly, generating and later cleaning up transient Spec files can be
Expand All @@ -229,7 +216,7 @@
// )
//
// func generateTransientSpec(ctr Container) error {
// registry := cdi.GetRegistry()
// cache := specs.GetDefaultCache()
// devices := getContainerDevs(ctr, vendor, class)
// spec := &specs.Spec{
// Version: specs.CurrentVersion,
Expand Down Expand Up @@ -257,21 +244,21 @@
// return fmt.Errorf("failed to generate Spec name: %w", err)
// }
//
// return registry.SpecDB().WriteSpec(spec, specName)
// return cache.WriteSpec(spec, specName)
// }
//
// func removeTransientSpec(ctr Container) error {
// registry := cdi.GetRegistry()
// cache := specs.GetDefaultCache()
// transientID := getSomeSufficientlyUniqueIDForContainer(ctr)
// specName := cdi.GenerateNameForTransientSpec(vendor, class, transientID)
//
// return registry.SpecDB().RemoveSpec(specName)
// return cache.RemoveSpec(specName)
// }
//
// # CDI Spec Validation
//
// This package performs both syntactic and semantic validation of CDI
// Spec file data when a Spec file is loaded via the registry or using
// Spec file data when a Spec file is loaded via the cache or using
// the ReadSpec API function. As part of the semantic verification, the
// Spec file is verified against the CDI Spec JSON validation schema.
//
Expand Down
Loading

0 comments on commit 96643f9

Please sign in to comment.