Skip to content

Commit 1365857

Browse files
committed
Revert NewCache function signature to return an error
This change reverts the NewCache function signature to also return an error. This was removed in #188 but does break clients such as moby using the NewCache API. Instead, a private function is added that returns no error and this is used internally and in tests. Signed-off-by: Evan Lezar <[email protected]>
1 parent bb0e178 commit 1365857

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

cmd/cdi/cmd/cdi-api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func cdiResolveDevices(ociSpecFiles ...string) error {
188188
err error
189189
)
190190

191-
cache = cdi.NewCache()
191+
cache, _ = cdi.NewCache()
192192

193193
for _, ociSpecFile := range ociSpecFiles {
194194
ociSpec, err = readOCISpec(ociSpecFile)

pkg/cdi/cache.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ func WithAutoRefresh(autoRefresh bool) Option {
6262
// NewCache creates a new CDI Cache. The cache is populated from a set
6363
// of CDI Spec directories. These can be specified using a WithSpecDirs
6464
// option. The default set of directories is exposed in DefaultSpecDirs.
65-
func NewCache(options ...Option) *Cache {
65+
//
66+
// Note:
67+
//
68+
// The error returned by this function is always nil and it is only
69+
// returned to maintain API compatibility with consumers.
70+
func NewCache(options ...Option) (*Cache, error) {
71+
return newCache(options...), nil
72+
}
73+
74+
// newCache creates a CDI cache with the supplied options.
75+
// This function allows testing without handling the nil error returned by the
76+
// NewCache function.
77+
func newCache(options ...Option) *Cache {
6678
c := &Cache{
6779
autoRefresh: true,
6880
watch: &watch{},

pkg/cdi/cache_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ devices:
183183
}
184184
}
185185

186-
cache = NewCache(WithSpecDirs(
186+
cache = newCache(WithSpecDirs(
187187
filepath.Join(dir, "etc"),
188188
filepath.Join(dir, "run")),
189189
)
@@ -554,7 +554,7 @@ devices:
554554
if !selfRefresh {
555555
opts = append(opts, WithAutoRefresh(false))
556556
}
557-
cache = NewCache(opts...)
557+
cache = newCache(opts...)
558558
require.NotNil(t, cache)
559559
} else {
560560
err = updateSpecDirs(t, dir, update.etc, update.run)
@@ -785,7 +785,7 @@ devices:
785785
dir, err = createSpecDirs(t, tc.updates[0].etc, tc.updates[0].run)
786786
require.NoError(t, err)
787787

788-
cache = NewCache(
788+
cache = newCache(
789789
WithSpecDirs(
790790
filepath.Join(dir, "etc"),
791791
filepath.Join(dir, "run"),
@@ -1171,7 +1171,7 @@ devices:
11711171
t.Errorf("failed to create test directory: %v", err)
11721172
return
11731173
}
1174-
cache = NewCache(
1174+
cache = newCache(
11751175
WithSpecDirs(
11761176
filepath.Join(dir, "etc"),
11771177
filepath.Join(dir, "run"),
@@ -1409,7 +1409,7 @@ devices:
14091409
t.Errorf("failed to create test directory: %v", err)
14101410
return
14111411
}
1412-
cache = NewCache(
1412+
cache = newCache(
14131413
WithSpecDirs(
14141414
filepath.Join(dir, "etc"),
14151415
filepath.Join(dir, "run"),
@@ -1564,7 +1564,7 @@ containerEdits:
15641564
if len(tc.invalid) != 0 {
15651565
dir, err = createSpecDirs(t, nil, nil)
15661566
require.NoError(t, err)
1567-
cache = NewCache(
1567+
cache = newCache(
15681568
WithSpecDirs(
15691569
filepath.Join(dir, "etc"),
15701570
filepath.Join(dir, "run"),
@@ -1596,14 +1596,14 @@ containerEdits:
15961596
dir, err = createSpecDirs(t, etc, nil)
15971597
require.NoError(t, err)
15981598

1599-
cache = NewCache(
1599+
cache = newCache(
16001600
WithSpecDirs(
16011601
filepath.Join(dir, "etc"),
16021602
),
16031603
)
16041604
require.NotNil(t, cache)
16051605

1606-
other = NewCache(
1606+
other = newCache(
16071607
WithSpecDirs(
16081608
filepath.Join(dir, "run"),
16091609
),
@@ -1786,7 +1786,7 @@ devices:
17861786

17871787
dir, err = createSpecDirs(t, nil, nil)
17881788
require.NoError(t, err)
1789-
cache = NewCache(
1789+
cache = newCache(
17901790
WithSpecDirs(
17911791
filepath.Join(dir, "etc"),
17921792
filepath.Join(dir, "run"),

pkg/cdi/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var (
124124
func GetRegistry(options ...Option) Registry {
125125
var new bool
126126
initOnce.Do(func() {
127-
reg = &registry{NewCache(options...)}
127+
reg = &registry{newCache(options...)}
128128
new = true
129129
})
130130
if !new && len(options) > 0 {

0 commit comments

Comments
 (0)