Skip to content

Commit 44fef1d

Browse files
authored
Merge pull request #232 from klihub/fixes/golangci-lint
fixes: enable golanci-lint in CI, fix complaints.
2 parents 91c77d0 + 84ea77f commit 44fef1d

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
permissions:
11+
contents: read
12+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
13+
# pull-requests: read
14+
15+
jobs:
16+
golangci:
17+
name: lint
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: actions/setup-go@v5
22+
with:
23+
go-version: stable
24+
- name: golangci-lint
25+
uses: golangci/golangci-lint-action@v6
26+
with:
27+
version: v1.61.0

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ GO_TEST := $(GO_CMD) test -race -v -cover
1616
GO_LINT := golint -set_exit_status
1717
GO_FMT := gofmt
1818
GO_VET := $(GO_CMD) vet
19+
CI_LINT := golangci-lint
1920

2021
CDI_PKG := $(shell grep ^module go.mod | sed 's/^module *//g')
2122

@@ -43,7 +44,7 @@ test: test-gopkgs test-schema
4344
# validation targets
4445
#
4546

46-
pre-pr-checks pr-checks: test fmt lint vet
47+
pre-pr-checks pr-checks: test fmt lint vet ci-lint
4748

4849
fmt format:
4950
$(Q)$(GO_FMT) -s -d -w -e .
@@ -52,6 +53,9 @@ lint:
5253
$(Q)$(GO_LINT) -set_exit_status ./...
5354
vet:
5455
$(Q)$(GO_VET) ./...
56+
golangci-lint ci-lint:
57+
$(Q)$(CI_LINT) run
58+
5559

5660
#
5761
# build targets

pkg/cdi/cache.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (c *Cache) configure(options ...Option) {
116116
c.watch.setup(c.specDirs, c.dirErrors)
117117
c.watch.start(&c.Mutex, c.refresh, c.dirErrors)
118118
}
119-
c.refresh()
119+
_ = c.refresh() // we record but ignore errors
120120
}
121121

122122
// Refresh rescans the CDI Spec directories and refreshes the Cache.
@@ -222,7 +222,8 @@ func (c *Cache) refreshIfRequired(force bool) (bool, error) {
222222

223223
// InjectDevices injects the given qualified devices to an OCI Spec. It
224224
// returns any unresolvable devices and an error if injection fails for
225-
// any of the devices.
225+
// any of the devices. Might trigger a cache refresh, in which case any
226+
// errors encountered can be obtained using GetErrors().
226227
func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, error) {
227228
var unresolved []string
228229

@@ -233,7 +234,7 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
233234
c.Lock()
234235
defer c.Unlock()
235236

236-
c.refreshIfRequired(false)
237+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
237238

238239
edits := &ContainerEdits{}
239240
specs := map[*Spec]struct{}{}
@@ -335,24 +336,27 @@ func (c *Cache) RemoveSpec(name string) error {
335336
return err
336337
}
337338

338-
// GetDevice returns the cached device for the given qualified name.
339+
// GetDevice returns the cached device for the given qualified name. Might trigger
340+
// a cache refresh, in which case any errors encountered can be obtained using
341+
// GetErrors().
339342
func (c *Cache) GetDevice(device string) *Device {
340343
c.Lock()
341344
defer c.Unlock()
342345

343-
c.refreshIfRequired(false)
346+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
344347

345348
return c.devices[device]
346349
}
347350

348-
// ListDevices lists all cached devices by qualified name.
351+
// ListDevices lists all cached devices by qualified name. Might trigger a cache
352+
// refresh, in which case any errors encountered can be obtained using GetErrors().
349353
func (c *Cache) ListDevices() []string {
350354
var devices []string
351355

352356
c.Lock()
353357
defer c.Unlock()
354358

355-
c.refreshIfRequired(false)
359+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
356360

357361
for name := range c.devices {
358362
devices = append(devices, name)
@@ -362,14 +366,15 @@ func (c *Cache) ListDevices() []string {
362366
return devices
363367
}
364368

365-
// ListVendors lists all vendors known to the cache.
369+
// ListVendors lists all vendors known to the cache. Might trigger a cache refresh,
370+
// in which case any errors encountered can be obtained using GetErrors().
366371
func (c *Cache) ListVendors() []string {
367372
var vendors []string
368373

369374
c.Lock()
370375
defer c.Unlock()
371376

372-
c.refreshIfRequired(false)
377+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
373378

374379
for vendor := range c.specs {
375380
vendors = append(vendors, vendor)
@@ -379,7 +384,8 @@ func (c *Cache) ListVendors() []string {
379384
return vendors
380385
}
381386

382-
// ListClasses lists all device classes known to the cache.
387+
// ListClasses lists all device classes known to the cache. Might trigger a cache
388+
// refresh, in which case any errors encountered can be obtained using GetErrors().
383389
func (c *Cache) ListClasses() []string {
384390
var (
385391
cmap = map[string]struct{}{}
@@ -389,7 +395,7 @@ func (c *Cache) ListClasses() []string {
389395
c.Lock()
390396
defer c.Unlock()
391397

392-
c.refreshIfRequired(false)
398+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
393399

394400
for _, specs := range c.specs {
395401
for _, spec := range specs {
@@ -404,12 +410,13 @@ func (c *Cache) ListClasses() []string {
404410
return classes
405411
}
406412

407-
// GetVendorSpecs returns all specs for the given vendor.
413+
// GetVendorSpecs returns all specs for the given vendor. Might trigger a cache
414+
// refresh, in which case any errors encountered can be obtained using GetErrors().
408415
func (c *Cache) GetVendorSpecs(vendor string) []*Spec {
409416
c.Lock()
410417
defer c.Unlock()
411418

412-
c.refreshIfRequired(false)
419+
_, _ = c.refreshIfRequired(false) // we record but ignore errors
413420

414421
return c.specs[vendor]
415422
}
@@ -544,7 +551,7 @@ func (w *watch) watch(fsw *fsnotify.Watcher, m *sync.Mutex, refresh func() error
544551
} else {
545552
w.update(dirErrors)
546553
}
547-
refresh()
554+
_ = refresh()
548555
m.Unlock()
549556

550557
case _, ok := <-watch.Errors:

pkg/cdi/default-cache_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ devices:
115115
filepath.Join(dir, "run"),
116116
),
117117
}
118-
Configure(opts...)
118+
err = Configure(opts...)
119+
require.NoError(t, err, "unexpected Configure() error")
119120

120121
devices := cache.ListDevices()
121122
if len(tc.devices) == 0 {
@@ -261,7 +262,8 @@ devices:
261262
filepath.Join(dir, "run"),
262263
),
263264
}
264-
Configure(opts...)
265+
err = Configure(opts...)
266+
require.NoError(t, err, "unexpected Configure() error")
265267
} else {
266268
err = updateSpecDirs(dir, update.etc, update.run)
267269
if err != nil {
@@ -395,12 +397,13 @@ devices:
395397
return
396398
}
397399

398-
Configure(
400+
err = Configure(
399401
WithSpecDirs(
400402
filepath.Join(dir, "etc"),
401403
filepath.Join(dir, "run"),
402404
),
403405
)
406+
require.NoError(t, err, "unexpected Configure() error")
404407

405408
unresolved, err := InjectDevices(tc.ociSpec, tc.devices...)
406409
if len(tc.unresolved) != 0 {

0 commit comments

Comments
 (0)