Skip to content

Commit 0aa7428

Browse files
committed
pkg/cdi: drop unused return value from NewCache function
Drop the unused (always nil) error returned by NewCache(). This eliminate some unreachable error paths in the code. NOTE: This change changes the exported API of "pkg/cdi" package as the prototype of NewCache() changes. Signed-off-by: Markus Lehtonen <[email protected]>
1 parent 2ae6e1a commit 0aa7428

File tree

4 files changed

+18
-35
lines changed

4 files changed

+18
-35
lines changed

cmd/cdi/cmd/cdi-api.go

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

191-
if cache, err = cdi.NewCache(); err != nil {
192-
return fmt.Errorf("failed to create CDI cache instance: %w", err)
193-
}
191+
cache = cdi.NewCache()
194192

195193
for _, ociSpecFile := range ociSpecFiles {
196194
ociSpec, err = readOCISpec(ociSpecFile)

pkg/cdi/cache.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ 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, error) {
65+
func NewCache(options ...Option) *Cache {
6666
c := &Cache{
6767
autoRefresh: true,
6868
watch: &watch{},
@@ -72,7 +72,8 @@ func NewCache(options ...Option) (*Cache, error) {
7272
c.Lock()
7373
defer c.Unlock()
7474

75-
return c, c.configure(options...)
75+
c.configure(options...)
76+
return c
7677
}
7778

7879
// Configure applies options to the Cache. Updates and refreshes the
@@ -85,12 +86,14 @@ func (c *Cache) Configure(options ...Option) error {
8586
c.Lock()
8687
defer c.Unlock()
8788

88-
return c.configure(options...)
89+
c.configure(options...)
90+
91+
return nil
8992
}
9093

9194
// Configure the Cache. Start/stop CDI Spec directory watch, refresh
9295
// the Cache if necessary.
93-
func (c *Cache) configure(options ...Option) error {
96+
func (c *Cache) configure(options ...Option) {
9497
for _, o := range options {
9598
o(c)
9699
}
@@ -103,8 +106,6 @@ func (c *Cache) configure(options ...Option) error {
103106
c.watch.start(&c.Mutex, c.refresh, c.dirErrors)
104107
}
105108
c.refresh()
106-
107-
return nil
108109
}
109110

110111
// Refresh rescans the CDI Spec directories and refreshes the Cache.

pkg/cdi/cache_test.go

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

186-
cache, err = NewCache(WithSpecDirs(
186+
cache = NewCache(WithSpecDirs(
187187
filepath.Join(dir, "etc"),
188188
filepath.Join(dir, "run")),
189189
)
@@ -196,9 +196,6 @@ devices:
196196
}
197197
}
198198

199-
if len(tc.errors) == 0 {
200-
require.Nil(t, err)
201-
}
202199
require.NotNil(t, cache)
203200

204201
for name, dev := range cache.devices {
@@ -557,8 +554,7 @@ devices:
557554
if !selfRefresh {
558555
opts = append(opts, WithAutoRefresh(false))
559556
}
560-
cache, err = NewCache(opts...)
561-
require.NoError(t, err)
557+
cache = NewCache(opts...)
562558
require.NotNil(t, cache)
563559
} else {
564560
err = updateSpecDirs(t, dir, update.etc, update.run)
@@ -789,13 +785,12 @@ devices:
789785
dir, err = createSpecDirs(t, tc.updates[0].etc, tc.updates[0].run)
790786
require.NoError(t, err)
791787

792-
cache, err = NewCache(
788+
cache = NewCache(
793789
WithSpecDirs(
794790
filepath.Join(dir, "etc"),
795791
filepath.Join(dir, "run"),
796792
),
797793
)
798-
require.NoError(t, err)
799794
require.NotNil(t, cache)
800795

801796
go injector()
@@ -1176,13 +1171,12 @@ devices:
11761171
t.Errorf("failed to create test directory: %v", err)
11771172
return
11781173
}
1179-
cache, err = NewCache(
1174+
cache = NewCache(
11801175
WithSpecDirs(
11811176
filepath.Join(dir, "etc"),
11821177
filepath.Join(dir, "run"),
11831178
),
11841179
)
1185-
require.Nil(t, err)
11861180
require.NotNil(t, cache)
11871181

11881182
unresolved, err := cache.InjectDevices(tc.ociSpec, tc.devices...)
@@ -1415,13 +1409,12 @@ devices:
14151409
t.Errorf("failed to create test directory: %v", err)
14161410
return
14171411
}
1418-
cache, err = NewCache(
1412+
cache = NewCache(
14191413
WithSpecDirs(
14201414
filepath.Join(dir, "etc"),
14211415
filepath.Join(dir, "run"),
14221416
),
14231417
)
1424-
require.Nil(t, err)
14251418
require.NotNil(t, cache)
14261419

14271420
vendors := cache.ListVendors()
@@ -1571,15 +1564,14 @@ containerEdits:
15711564
if len(tc.invalid) != 0 {
15721565
dir, err = createSpecDirs(t, nil, nil)
15731566
require.NoError(t, err)
1574-
cache, err = NewCache(
1567+
cache = NewCache(
15751568
WithSpecDirs(
15761569
filepath.Join(dir, "etc"),
15771570
filepath.Join(dir, "run"),
15781571
),
15791572
WithAutoRefresh(false),
15801573
)
15811574

1582-
require.NoError(t, err)
15831575
require.NotNil(t, cache)
15841576

15851577
etc = map[string]string{}
@@ -1604,21 +1596,19 @@ containerEdits:
16041596
dir, err = createSpecDirs(t, etc, nil)
16051597
require.NoError(t, err)
16061598

1607-
cache, err = NewCache(
1599+
cache = NewCache(
16081600
WithSpecDirs(
16091601
filepath.Join(dir, "etc"),
16101602
),
16111603
)
1612-
require.NoError(t, err)
16131604
require.NotNil(t, cache)
16141605

1615-
other, err = NewCache(
1606+
other = NewCache(
16161607
WithSpecDirs(
16171608
filepath.Join(dir, "run"),
16181609
),
16191610
WithAutoRefresh(false),
16201611
)
1621-
require.NoError(t, err)
16221612
require.NotNil(t, other)
16231613

16241614
cSpecs := map[string]*cdi.Spec{}
@@ -1796,15 +1786,14 @@ devices:
17961786

17971787
dir, err = createSpecDirs(t, nil, nil)
17981788
require.NoError(t, err)
1799-
cache, err = NewCache(
1789+
cache = NewCache(
18001790
WithSpecDirs(
18011791
filepath.Join(dir, "etc"),
18021792
filepath.Join(dir, "run"),
18031793
),
18041794
WithAutoRefresh(false),
18051795
)
18061796

1807-
require.NoError(t, err)
18081797
require.NotNil(t, cache)
18091798

18101799
for idx, data := range tc.specs {

pkg/cdi/registry.go

Lines changed: 1 addition & 6 deletions
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, _ = getRegistry(options...)
127+
reg = &registry{NewCache(options...)}
128128
new = true
129129
})
130130
if !new && len(options) > 0 {
@@ -144,8 +144,3 @@ func (r *registry) DeviceDB() RegistryDeviceDB {
144144
func (r *registry) SpecDB() RegistrySpecDB {
145145
return r
146146
}
147-
148-
func getRegistry(options ...Option) (*registry, error) {
149-
c, err := NewCache(options...)
150-
return &registry{c}, err
151-
}

0 commit comments

Comments
 (0)