Skip to content

Revert NewCache function signature to return an error #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion cmd/cdi/cmd/cdi-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func cdiResolveDevices(ociSpecFiles ...string) error {
err error
)

cache = cdi.NewCache()
cache, _ = cdi.NewCache()

for _, ociSpecFile := range ociSpecFiles {
ociSpec, err = readOCISpec(ociSpecFile)
Expand Down
14 changes: 13 additions & 1 deletion pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ func WithAutoRefresh(autoRefresh bool) Option {
// NewCache creates a new CDI Cache. The cache is populated from a set
// of CDI Spec directories. These can be specified using a WithSpecDirs
// option. The default set of directories is exposed in DefaultSpecDirs.
func NewCache(options ...Option) *Cache {
//
// Note:
//
// The error returned by this function is always nil and it is only
// returned to maintain API compatibility with consumers.
func NewCache(options ...Option) (*Cache, error) {
return newCache(options...), nil
}

// newCache creates a CDI cache with the supplied options.
// This function allows testing without handling the nil error returned by the
// NewCache function.
func newCache(options ...Option) *Cache {
c := &Cache{
autoRefresh: true,
watch: &watch{},
Expand Down
18 changes: 9 additions & 9 deletions pkg/cdi/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ devices:
}
}

cache = NewCache(WithSpecDirs(
cache = newCache(WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run")),
)
Expand Down Expand Up @@ -554,7 +554,7 @@ devices:
if !selfRefresh {
opts = append(opts, WithAutoRefresh(false))
}
cache = NewCache(opts...)
cache = newCache(opts...)
require.NotNil(t, cache)
} else {
err = updateSpecDirs(t, dir, update.etc, update.run)
Expand Down Expand Up @@ -785,7 +785,7 @@ devices:
dir, err = createSpecDirs(t, tc.updates[0].etc, tc.updates[0].run)
require.NoError(t, err)

cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run"),
Expand Down Expand Up @@ -1171,7 +1171,7 @@ devices:
t.Errorf("failed to create test directory: %v", err)
return
}
cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run"),
Expand Down Expand Up @@ -1409,7 +1409,7 @@ devices:
t.Errorf("failed to create test directory: %v", err)
return
}
cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run"),
Expand Down Expand Up @@ -1564,7 +1564,7 @@ containerEdits:
if len(tc.invalid) != 0 {
dir, err = createSpecDirs(t, nil, nil)
require.NoError(t, err)
cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run"),
Expand Down Expand Up @@ -1596,14 +1596,14 @@ containerEdits:
dir, err = createSpecDirs(t, etc, nil)
require.NoError(t, err)

cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
),
)
require.NotNil(t, cache)

other = NewCache(
other = newCache(
WithSpecDirs(
filepath.Join(dir, "run"),
),
Expand Down Expand Up @@ -1786,7 +1786,7 @@ devices:

dir, err = createSpecDirs(t, nil, nil)
require.NoError(t, err)
cache = NewCache(
cache = newCache(
WithSpecDirs(
filepath.Join(dir, "etc"),
filepath.Join(dir, "run"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/cdi/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var (
func GetRegistry(options ...Option) Registry {
var new bool
initOnce.Do(func() {
reg = &registry{NewCache(options...)}
reg = &registry{newCache(options...)}
new = true
})
if !new && len(options) > 0 {
Expand Down