diff --git a/.golangci.yml b/.golangci.yml index d726406c..fcdd1677 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,6 +11,7 @@ linters: - deadcode - dogsled - errcheck + - exportloopref - gocognit - goconst - gocyclo @@ -30,7 +31,9 @@ linters: - nakedret - noctx - nolintlint + - prealloc - rowserrcheck + - scopelint - staticcheck - structcheck - stylecheck @@ -51,3 +54,11 @@ linters-settings: min-complexity: 15 gocognit: min-complexity: 31 + +issues: + exclude-rules: + - path: _test\.go + linters: + # Until the testing package allows pinning variables disable scopelint + # for tests. See https://github.com/kyoh86/scopelint/issues/4. + - scopelint diff --git a/cmd/fpga_plugin/fpga_plugin.go b/cmd/fpga_plugin/fpga_plugin.go index 61b1d39d..4505af36 100644 --- a/cmd/fpga_plugin/fpga_plugin.go +++ b/cmd/fpga_plugin/fpga_plugin.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "regexp" "strings" "time" @@ -259,7 +260,7 @@ func (dp *devicePlugin) getAFU(fpath string, devName string) (*afu, error) { if dp.ignoreAfuIDs { afuID = "unused_afu_id" } else { - data, err := ioutil.ReadFile(fpath) + data, err := ioutil.ReadFile(filepath.Clean(fpath)) if err != nil { return nil, errors.WithStack(err) } @@ -278,7 +279,7 @@ func (dp *devicePlugin) getAFU(fpath string, devName string) (*afu, error) { } func (dp *devicePlugin) getFME(interfaceIDPath string, devName string) (*region, error) { - data, err := ioutil.ReadFile(interfaceIDPath) + data, err := ioutil.ReadFile(filepath.Clean(interfaceIDPath)) if err != nil { return nil, errors.WithStack(err) } @@ -295,8 +296,6 @@ func (dp *devicePlugin) getFME(interfaceIDPath string, devName string) (*region, } func (dp *devicePlugin) scanFPGAs() (dpapi.DeviceTree, error) { - var devices []device - klog.V(4).Info("Start new FPGA scan") fpgaFiles, err := ioutil.ReadDir(dp.sysfsDir) @@ -305,6 +304,7 @@ func (dp *devicePlugin) scanFPGAs() (dpapi.DeviceTree, error) { return dp.getDevTree([]device{}), nil } + devices := make([]device, 0, len(fpgaFiles)) for _, fpgaFile := range fpgaFiles { fname := fpgaFile.Name() diff --git a/pkg/fpgacontroller/patcher/patcher.go b/pkg/fpgacontroller/patcher/patcher.go index ff88259d..ba1d0f51 100644 --- a/pkg/fpgacontroller/patcher/patcher.go +++ b/pkg/fpgacontroller/patcher/patcher.go @@ -183,8 +183,6 @@ func getRequestedResources(container corev1.Container) (map[string]int64, error) } func (p *patcher) getPatchOps(containerIdx int, container corev1.Container) ([]string, error) { - var ops []string - requestedResources, err := getRequestedResources(container) if err != nil { return nil, err @@ -197,6 +195,7 @@ func (p *patcher) getPatchOps(containerIdx int, container corev1.Container) ([]s resources := make(map[string]int64) envVars := make(map[string]string) counter := 0 + ops := make([]string, 0, 2*len(requestedResources)) for rname, quantity := range requestedResources { mode, found := p.resourceModeMap[rname] if !found { diff --git a/pkg/fpgacontroller/patcher/patchermanager.go b/pkg/fpgacontroller/patcher/patchermanager.go index 085be300..bbf86f82 100644 --- a/pkg/fpgacontroller/patcher/patchermanager.go +++ b/pkg/fpgacontroller/patcher/patchermanager.go @@ -104,7 +104,7 @@ func (pm *PatcherManager) mutate(ctx context.Context, req webhook.AdmissionReque Allowed: true, } - var ops []string + ops := []string{} for containerIdx, container := range pod.Spec.Containers { patchOps, err := patcher.getPatchOps(containerIdx, container) if err != nil {