Skip to content

Commit

Permalink
Address golangci-lint warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
  • Loading branch information
ArangoGutierrez committed Apr 4, 2024
1 parent ab773cb commit 48789b7
Show file tree
Hide file tree
Showing 34 changed files with 328 additions and 292 deletions.
23 changes: 23 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# please keep this alphabetized
linters:
enable:
- asciicheck
- contextcheck
- godot
- gofmt
- goimports
- misspell
# TODO: re-enable once we have addressed the warnings
disable:
- unused
- gocritic
- stylecheck
- forcetypeassert

run:
tests: true
timeout: 10m

linters-settings:
goimports:
local-prefixes: "github.com/NVIDIA/go-nvlib"
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PCI_IDS_URL ?= https://pci-ids.ucw.cz/v2.2/pci.ids

TARGETS := binary build all check fmt assert-fmt generate lint vet test coverage
DOCKER_TARGETS := $(patsubst %,docker-%, $(TARGETS))
.PHONY: $(TARGETS) $(DOCKER_TARGETS)
.PHONY: $(TARGETS) $(DOCKER_TARGETS) vendor check-vendor

GOOS := linux

Expand All @@ -30,6 +30,11 @@ build:
all: check build binary
check: assert-fmt lint vet

vendor:
go mod tidy
go mod vendor
go mod verify

check-vendor: vendor
git diff --quiet HEAD -- go.mod go.sum vendor

Expand Down Expand Up @@ -57,6 +62,14 @@ lint:
# We use `go list -f '{{.Dir}}' $(MODULE)/...` to skip the `vendor` folder.
go list -f '{{.Dir}}' $(MODULE)/... | grep -v pkg/nvml | xargs golint -set_exit_status

## goimports: Apply goimports -local to the codebase
goimports:
find . -name \*.go \
-not -name "zz_generated.deepcopy.go" \
-not -path "./vendor/*" \
-not -path "./pkg/nvidia.com/resource/clientset/versioned/*" \
-exec goimports -local $(MODULE) -w {} \;

vet:
go vet $(MODULE)/...

Expand Down Expand Up @@ -89,6 +102,7 @@ $(DOCKER_TARGETS): docker-%:
--rm \
-e GOCACHE=/tmp/.cache/go \
-e GOMODCACHE=/tmp/.cache/gomod \
-e GOLANGCI_LINT_CACHE=/tmp/.cache/golangci-lint \
-v $(PWD):/work \
-w /work \
--user $$(id -u):$$(id -g) \
Expand Down
18 changes: 0 additions & 18 deletions docker/Dockerfile.devel

This file was deleted.

12 changes: 6 additions & 6 deletions pkg/nvlib/device/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/NVIDIA/go-nvlib/pkg/nvml"
)

// Interface provides the API to the 'device' package
// Interface provides the API to the 'device' package.
type Interface interface {
AssertValidMigProfileFormat(profile string) error
GetDevices() ([]Device, error)
Expand All @@ -46,7 +46,7 @@ type devicelib struct {

var _ Interface = &devicelib{}

// New creates a new instance of the 'device' interface
// New creates a new instance of the 'device' interface.
func New(opts ...Option) Interface {
d := &devicelib{}
for _, opt := range opts {
Expand All @@ -68,21 +68,21 @@ func New(opts ...Option) Interface {
return d
}

// WithNvml provides an Option to set the NVML library used by the 'device' interface
// WithNvml provides an Option to set the NVML library used by the 'device' interface.
func WithNvml(nvml nvml.Interface) Option {
return func(d *devicelib) {
d.nvml = nvml
}
}

// WithVerifySymbols provides an option to toggle whether to verify select symbols exist in dynamic libraries before calling them
// WithVerifySymbols provides an option to toggle whether to verify select symbols exist in dynamic libraries before calling them.
func WithVerifySymbols(verify bool) Option {
return func(d *devicelib) {
d.verifySymbols = &verify
}
}

// WithSkippedDevices provides an Option to set devices to be skipped by model name
// WithSkippedDevices provides an Option to set devices to be skipped by model name.
func WithSkippedDevices(names ...string) Option {
return func(d *devicelib) {
if d.skippedDevices == nil {
Expand All @@ -94,5 +94,5 @@ func WithSkippedDevices(names ...string) Option {
}
}

// Option defines a function for passing options to the New() call
// Option defines a function for passing options to the New() call.
type Option func(*devicelib)
42 changes: 21 additions & 21 deletions pkg/nvlib/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/NVIDIA/go-nvlib/pkg/nvml"
)

// Device defines the set of extended functions associated with a device.Device
// Device defines the set of extended functions associated with a device.Device.
type Device interface {
nvml.Device
GetArchitectureAsString() (string, error)
Expand All @@ -44,12 +44,12 @@ type device struct {

var _ Device = &device{}

// NewDevice builds a new Device from an nvml.Device
// NewDevice builds a new Device from an nvml.Device.
func (d *devicelib) NewDevice(dev nvml.Device) (Device, error) {
return d.newDevice(dev)
}

// NewDeviceByUUID builds a new Device from a UUID
// NewDeviceByUUID builds a new Device from a UUID.
func (d *devicelib) NewDeviceByUUID(uuid string) (Device, error) {
dev, ret := d.nvml.DeviceGetHandleByUUID(uuid)
if ret != nvml.SUCCESS {
Expand All @@ -58,12 +58,12 @@ func (d *devicelib) NewDeviceByUUID(uuid string) (Device, error) {
return d.newDevice(dev)
}

// newDevice creates a device from an nvml.Device
// newDevice creates a device from an nvml.Device.
func (d *devicelib) newDevice(dev nvml.Device) (*device, error) {
return &device{dev, d, nil}, nil
}

// GetArchitectureAsString returns the Device architecture as a string
// GetArchitectureAsString returns the Device architecture as a string.
func (d *device) GetArchitectureAsString() (string, error) {
arch, ret := d.GetArchitecture()
if ret != nvml.SUCCESS {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (d *device) GetArchitectureAsString() (string, error) {
return "", fmt.Errorf("error interpreting device architecture as string: %v", arch)
}

// GetBrandAsString returns the Device architecture as a string
// GetBrandAsString returns the Device architecture as a string.
func (d *device) GetBrandAsString() (string, error) {
brand, ret := d.GetBrand()
if ret != nvml.SUCCESS {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (d *device) GetBrandAsString() (string, error) {
return "", fmt.Errorf("error interpreting device brand as string: %v", brand)
}

// GetCudaComputeCapabilityAsString returns the Device's CUDA compute capability as a version string
// GetCudaComputeCapabilityAsString returns the Device's CUDA compute capability as a version string.
func (d *device) GetCudaComputeCapabilityAsString() (string, error) {
major, minor, ret := d.GetCudaComputeCapability()
if ret != nvml.SUCCESS {
Expand All @@ -149,7 +149,7 @@ func (d *device) GetCudaComputeCapabilityAsString() (string, error) {
return fmt.Sprintf("%d.%d", major, minor), nil
}

// IsMigCapable checks if a device is capable of having MIG paprtitions created on it
// IsMigCapable checks if a device is capable of having MIG paprtitions created on it.
func (d *device) IsMigCapable() (bool, error) {
if !d.lib.hasSymbol("nvmlDeviceGetMigMode") {
return false, nil
Expand All @@ -166,7 +166,7 @@ func (d *device) IsMigCapable() (bool, error) {
return true, nil
}

// IsMigEnabled checks if a device has MIG mode currently enabled on it
// IsMigEnabled checks if a device has MIG mode currently enabled on it.
func (d *device) IsMigEnabled() (bool, error) {
if !d.lib.hasSymbol("nvmlDeviceGetMigMode") {
return false, nil
Expand All @@ -183,7 +183,7 @@ func (d *device) IsMigEnabled() (bool, error) {
return (mode == nvml.DEVICE_MIG_ENABLE), nil
}

// VisitMigDevices walks a top-level device and invokes a callback function for each MIG device configured on it
// VisitMigDevices walks a top-level device and invokes a callback function for each MIG device configured on it.
func (d *device) VisitMigDevices(visit func(int, MigDevice) error) error {
capable, err := d.IsMigCapable()
if err != nil {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (d *device) VisitMigDevices(visit func(int, MigDevice) error) error {
return nil
}

// VisitMigProfiles walks a top-level device and invokes a callback function for each unique MIG Profile that can be configured on it
// VisitMigProfiles walks a top-level device and invokes a callback function for each unique MIG Profile that can be configured on it.
func (d *device) VisitMigProfiles(visit func(MigProfile) error) error {
capable, err := d.IsMigCapable()
if err != nil {
Expand Down Expand Up @@ -283,7 +283,7 @@ func (d *device) VisitMigProfiles(visit func(MigProfile) error) error {
return nil
}

// GetMigDevices gets the set of MIG devices associated with a top-level device
// GetMigDevices gets the set of MIG devices associated with a top-level device.
func (d *device) GetMigDevices() ([]MigDevice, error) {
var migs []MigDevice
err := d.VisitMigDevices(func(j int, m MigDevice) error {
Expand All @@ -296,7 +296,7 @@ func (d *device) GetMigDevices() ([]MigDevice, error) {
return migs, nil
}

// GetMigProfiles gets the set of unique MIG profiles associated with a top-level device
// GetMigProfiles gets the set of unique MIG profiles associated with a top-level device.
func (d *device) GetMigProfiles() ([]MigProfile, error) {
// Return the cached list if available
if d.migProfiles != nil {
Expand All @@ -313,7 +313,7 @@ func (d *device) GetMigProfiles() ([]MigProfile, error) {
return nil, err
}

// And cache it before returning
// And cache it before returning.
d.migProfiles = profiles
return profiles, nil
}
Expand All @@ -332,7 +332,7 @@ func (d *device) isSkipped() (bool, error) {
return false, nil
}

// VisitDevices visits each top-level device and invokes a callback function for it
// VisitDevices visits each top-level device and invokes a callback function for it.
func (d *devicelib) VisitDevices(visit func(int, Device) error) error {
count, ret := d.nvml.DeviceGetCount()
if ret != nvml.SUCCESS {
Expand Down Expand Up @@ -365,7 +365,7 @@ func (d *devicelib) VisitDevices(visit func(int, Device) error) error {
return nil
}

// VisitMigDevices walks a top-level device and invokes a callback function for each MIG device configured on it
// VisitMigDevices walks a top-level device and invokes a callback function for each MIG device configured on it.
func (d *devicelib) VisitMigDevices(visit func(int, Device, int, MigDevice) error) error {
err := d.VisitDevices(func(i int, dev Device) error {
err := dev.VisitMigDevices(func(j int, mig MigDevice) error {
Expand All @@ -386,7 +386,7 @@ func (d *devicelib) VisitMigDevices(visit func(int, Device, int, MigDevice) erro
return nil
}

// VisitMigProfiles walks a top-level device and invokes a callback function for each unique MIG profile found on them
// VisitMigProfiles walks a top-level device and invokes a callback function for each unique MIG profile found on them.
func (d *devicelib) VisitMigProfiles(visit func(MigProfile) error) error {
visited := make(map[string]bool)
err := d.VisitDevices(func(i int, dev Device) error {
Expand Down Expand Up @@ -414,7 +414,7 @@ func (d *devicelib) VisitMigProfiles(visit func(MigProfile) error) error {
return nil
}

// GetDevices gets the set of all top-level devices
// GetDevices gets the set of all top-level devices.
func (d *devicelib) GetDevices() ([]Device, error) {
var devs []Device
err := d.VisitDevices(func(i int, dev Device) error {
Expand All @@ -427,7 +427,7 @@ func (d *devicelib) GetDevices() ([]Device, error) {
return devs, nil
}

// GetMigDevices gets the set of MIG devices across all top-level devices
// GetMigDevices gets the set of MIG devices across all top-level devices.
func (d *devicelib) GetMigDevices() ([]MigDevice, error) {
var migs []MigDevice
err := d.VisitMigDevices(func(i int, dev Device, j int, m MigDevice) error {
Expand All @@ -440,7 +440,7 @@ func (d *devicelib) GetMigDevices() ([]MigDevice, error) {
return migs, nil
}

// GetMigProfiles gets the set of unique MIG profiles across all top-level devices
// GetMigProfiles gets the set of unique MIG profiles across all top-level devices.
func (d *devicelib) GetMigProfiles() ([]MigProfile, error) {
// Return the cached list if available
if d.migProfiles != nil {
Expand All @@ -457,7 +457,7 @@ func (d *devicelib) GetMigProfiles() ([]MigProfile, error) {
return nil, err
}

// And cache it before returning
// And cache it before returning.
d.migProfiles = profiles
return profiles, nil
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/nvlib/device/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import (
// This includes a device index or UUID.
type Identifier string

// IsGpuIndex checks if an identifier is a full GPU index
// IsGpuIndex checks if an identifier is a full GPU index.
func (i Identifier) IsGpuIndex() bool {
if _, err := strconv.ParseUint(string(i), 10, 0); err != nil {
return false
}
return true
}

// IsMigIndex checks if an identifier is a MIG index
// IsMigIndex checks if an identifier is a MIG index.
func (i Identifier) IsMigIndex() bool {
split := strings.Split(string(i), ":")
if len(split) != 2 {
Expand All @@ -49,13 +49,13 @@ func (i Identifier) IsMigIndex() bool {
return true
}

// IsUUID checks if an identifier is a UUID
// IsUUID checks if an identifier is a UUID.
func (i Identifier) IsUUID() bool {
return i.IsGpuUUID() || i.IsMigUUID()
}

// IsGpuUUID checks if an identifier is a GPU UUID
// A GPU UUID must be of the form GPU-b1028956-cfa2-0990-bf4a-5da9abb51763
// IsGpuUUID checks if an identifier is a GPU UUID.
// A GPU UUID must be of the form GPU-b1028956-cfa2-0990-bf4a-5da9abb51763.
func (i Identifier) IsGpuUUID() bool {
if !strings.HasPrefix(string(i), "GPU-") {
return false
Expand All @@ -64,7 +64,7 @@ func (i Identifier) IsGpuUUID() bool {
return err == nil
}

// IsMigUUID checks if an identifier is a MIG UUID
// IsMigUUID checks if an identifier is a MIG UUID.
// A MIG UUID can be of one of two forms:
// - MIG-b1028956-cfa2-0990-bf4a-5da9abb51763
// - MIG-GPU-b1028956-cfa2-0990-bf4a-5da9abb51763/3/0
Expand Down
8 changes: 4 additions & 4 deletions pkg/nvlib/device/mig_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/NVIDIA/go-nvlib/pkg/nvml"
)

// MigDevice defines the set of extended functions associated with a MIG device
// MigDevice defines the set of extended functions associated with a MIG device.
type MigDevice interface {
nvml.Device
GetProfile() (MigProfile, error)
Expand All @@ -36,7 +36,7 @@ type migdevice struct {

var _ MigDevice = &migdevice{}

// NewMigDevice builds a new MigDevice from an nvml.Device
// NewMigDevice builds a new MigDevice from an nvml.Device.
func (d *devicelib) NewMigDevice(handle nvml.Device) (MigDevice, error) {
isMig, ret := handle.IsMigDeviceHandle()
if ret != nvml.SUCCESS {
Expand All @@ -48,7 +48,7 @@ func (d *devicelib) NewMigDevice(handle nvml.Device) (MigDevice, error) {
return &migdevice{handle, d, nil}, nil
}

// NewMigDeviceByUUID builds a new MigDevice from a UUID
// NewMigDeviceByUUID builds a new MigDevice from a UUID.
func (d *devicelib) NewMigDeviceByUUID(uuid string) (MigDevice, error) {
dev, ret := d.nvml.DeviceGetHandleByUUID(uuid)
if ret != nvml.SUCCESS {
Expand All @@ -57,7 +57,7 @@ func (d *devicelib) NewMigDeviceByUUID(uuid string) (MigDevice, error) {
return d.NewMigDevice(dev)
}

// GetProfile returns the MIG profile associated with a MIG device
// GetProfile returns the MIG profile associated with a MIG device.
func (m *migdevice) GetProfile() (MigProfile, error) {
if m.profile != nil {
return m.profile, nil
Expand Down
Loading

0 comments on commit 48789b7

Please sign in to comment.