From 4b76859176534d186c29c13b4c9b6d245a41ecec Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Arango Gutierrez Date: Wed, 13 Mar 2024 13:51:12 +0100 Subject: [PATCH] Add golanci-lint config file and address lints Signed-off-by: Carlos Eduardo Arango Gutierrez --- .golangci.yml | 40 +++++++++++++++++++++++++++++ api/spec/v1/helpers.go | 2 ++ api/spec/v1/spec_test.go | 3 ++- cmd/nvidia-vgpu-dm/apply/apply.go | 1 + cmd/nvidia-vgpu-dm/assert/assert.go | 2 ++ deployments/container/main.go | 15 ++++++----- pkg/types/types_test.go | 3 ++- pkg/types/vgpu_config.go | 2 +- 8 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..a0164fd1 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 10m + +linters: + enable: + - contextcheck + - errcheck + - gocritic + - gofmt + - goimports + - gosec + - gosimple + - govet + - ineffassign + - misspell + - staticcheck + - unconvert + disable: [] + +linters-settings: + goimports: + local-prefixes: github.com/NVIDIA/k8s-device-plugin + +issues: + exclude-rules: + # We use math/rand instead of crypto/rand for unique names in e2e tests. + - path: tests/e2e/ + linters: + - gosec + text: "G404" + # We disable the memory aliasing checks in tests + - path: ".*_test.go" + linters: + - gosec + text: "G601: Implicit memory aliasing in for loop" + # We create world-readable files in tests. + - path: ".*_test.go" + linters: + - gosec + text: "G306: Expect WriteFile permissions to be 0600 or less" \ No newline at end of file diff --git a/api/spec/v1/helpers.go b/api/spec/v1/helpers.go index c8d9d038..89879ae4 100644 --- a/api/spec/v1/helpers.go +++ b/api/spec/v1/helpers.go @@ -48,6 +48,7 @@ func (vs *VGPUConfigSpec) MatchesDeviceFilter(deviceID types.DeviceID) bool { // MatchesAllDevices checks a 'VGPUConfigSpec' to see if it matches on 'all' devices. func (vs *VGPUConfigSpec) MatchesAllDevices() bool { + // nolint: gocritic switch devices := vs.Devices.(type) { case string: return devices == "all" @@ -57,6 +58,7 @@ func (vs *VGPUConfigSpec) MatchesAllDevices() bool { // MatchesDevices checks a 'VGPUConfigSpec' to see if it matches on a device at the specified 'index'. func (vs *VGPUConfigSpec) MatchesDevices(index int) bool { + // nolint: gocritic switch devices := vs.Devices.(type) { case []int: for _, d := range devices { diff --git a/api/spec/v1/spec_test.go b/api/spec/v1/spec_test.go index c6070ef8..1fbbd7de 100644 --- a/api/spec/v1/spec_test.go +++ b/api/spec/v1/spec_test.go @@ -17,9 +17,10 @@ package v1 import ( + "testing" + "github.com/stretchr/testify/require" "sigs.k8s.io/yaml" - "testing" ) func TestSpec(t *testing.T) { diff --git a/cmd/nvidia-vgpu-dm/apply/apply.go b/cmd/nvidia-vgpu-dm/apply/apply.go index 81d2567c..98bf3b56 100644 --- a/cmd/nvidia-vgpu-dm/apply/apply.go +++ b/cmd/nvidia-vgpu-dm/apply/apply.go @@ -94,6 +94,7 @@ func (c *Context) ApplyVGPUConfig() error { func applyWrapper(c *cli.Context, f *Flags) error { err := CheckFlags(f) if err != nil { + //nolint:errcheck cli.ShowSubcommandHelp(c) return err } diff --git a/cmd/nvidia-vgpu-dm/assert/assert.go b/cmd/nvidia-vgpu-dm/assert/assert.go index 686e51bc..83fa741b 100644 --- a/cmd/nvidia-vgpu-dm/assert/assert.go +++ b/cmd/nvidia-vgpu-dm/assert/assert.go @@ -93,6 +93,7 @@ func BuildCommand() *cli.Command { func assertWrapper(c *cli.Context, f *Flags) error { err := CheckFlags(f) if err != nil { + //nolint:errcheck cli.ShowSubcommandHelp(c) return err } @@ -217,6 +218,7 @@ func WalkSelectedVGPUConfigForEachGPU(vgpuConfig v1.VGPUConfigSpecSlice, f func( log.Debugf(" GPU %v: %v", i, deviceID) + // nolint: gosec err = f(&vc, i, deviceID) if err != nil { return err diff --git a/deployments/container/main.go b/deployments/container/main.go index 143ed0a1..e59deb81 100644 --- a/deployments/container/main.go +++ b/deployments/container/main.go @@ -18,22 +18,24 @@ package main import ( "fmt" + "os" + "os/exec" + log "github.com/sirupsen/logrus" cli "github.com/urfave/cli/v2" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" - "os" - "os/exec" "context" + "sync" + "time" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/util/wait" - "sync" - "time" ) const ( @@ -217,7 +219,7 @@ func start(c *cli.Context) error { log.Errorf("ERROR: %v", err) continue } - log.Infof("Successfuly updated to vGPU config: %s", value) + log.Infof("Successfully updated to vGPU config: %s", value) } } @@ -251,13 +253,14 @@ func continuouslySyncVGPUConfigChanges(clientset *kubernetes.Clientset, vGPUConf } func updateConfig(clientset *kubernetes.Clientset, selectedConfig string) error { + //nolint:errcheck defer setVGPUConfigStateLabel(clientset) vGPUConfigState = "failed" log.Info("Asserting that the requested configuration is present in the configuration file") err := assertValidConfig(selectedConfig) if err != nil { - return fmt.Errorf("Unable to validate the selected vGPU configuration") + return fmt.Errorf("unable to validate the selected vGPU configuration") } log.Info("Checking if the selected vGPU device configuration is currently applied or not") diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index bdace122..1c492598 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -17,8 +17,9 @@ package types import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestParseVGPUType(t *testing.T) { diff --git a/pkg/types/vgpu_config.go b/pkg/types/vgpu_config.go index 28c38bb2..67ba3589 100644 --- a/pkg/types/vgpu_config.go +++ b/pkg/types/vgpu_config.go @@ -46,7 +46,7 @@ func (v VGPUConfig) AssertValid() error { return fmt.Errorf("cannot mix time-sliced and MIG-backed vGPU devices on the same GPU") } migBacked = true - } else { + } else if vgpuType.G <= 0 { if idx > 0 && migBacked { return fmt.Errorf("cannot mix time-sliced and MIG-backed vGPU devices on the same GPU") }