diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ca3c1ecc92..4088d56d09 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -42,3 +42,14 @@ jobs: - name: test controllers on opensfhit run: CLUSTER_TYPE=openshift make test-controllers + golangci: + name: Golangci-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. + version: v1.37 + args: --timeout 3m0s diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000000..22a892db39 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,120 @@ +# Tested with golangci-lint ver. 1.37 +run: + timeout: 3m +linters-settings: + depguard: + list-type: blacklist + packages: + # logging is allowed only by logutils.Log, logrus + # is allowed to use only in logutils package + - github.com/sirupsen/logrus + packages-with-error-message: + - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" + dupl: + threshold: 100 + funlen: + lines: 100 + statements: 50 + goconst: + min-len: 2 + min-occurrences: 2 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - dupImport # https://github.com/go-critic/go-critic/issues/845 + - ifElseChain + - octalLiteral + - whyNoLint + - wrapperFunc + - unnamedResult + settings: + hugeParam: + sizeThreshold: 512 + rangeValCopy: + sizeThreshold: 512 + gocyclo: + min-complexity: 15 + goimports: + local-prefixes: github.com/k8snetworkplumbingwg/sriov-network-device-plugin + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: argument,case,condition,return + lll: + line-length: 140 + misspell: + locale: US + prealloc: + # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. + # True by default. + simple: true + range-loops: true # Report preallocation suggestions on range loops, true by default + for-loops: false # Report preallocation suggestions on for loops, false by default + +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - exportloopref + - exhaustive + - funlen + #- gochecknoinits + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - gomnd + - goprintffuncname + - gosec + - gosimple + #- govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - rowserrcheck + #- scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd + - gosec + - dupl + - lll + - stylecheck + - goconst + +run: + skip-dirs: + - vendor/ + - .github/ + - deployments/ + - docs/ + - images/ diff --git a/Makefile b/Makefile index 286d6173cc..26dc3e1c03 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,15 @@ else GOBIN=$(shell go env GOBIN) endif +GOLANGCI_LINT = $(GOBIN)/golangci-lint +# golangci-lint version should be updated periodically +# we keep it fixed to avoid it from unexpectedly failing on the project +# in case of a version bump +GOLANGCI_LINT_VER = v1.37.0 +TIMEOUT = 15 +Q = $(if $(filter 1,$V),,@) + + .PHONY: all build clean gendeepcopy test test-e2e test-e2e-k8s run image fmt sync-manifests test-e2e-conformance manifests update-codegen all: generate vet build plugins @@ -229,3 +238,14 @@ undeploy-k8s: undeploy deps-update: go mod tidy && \ go mod vendor + +$(GOLANGCI_LINT): $(info building golangci-lint...) + $Q curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCI_LINT_VER) + +.PHONY: lint +lint: | $(GOLANGCI_LINT) ; $(info running golangci-lint...) @ ## Run golangci-lint + $Q mkdir -p $(CURPATH)/test-lint + $Q cd $(CURPATH) && ret=0 && \ + test -z "$$($(GOLANGCI_LINT) run | tee $(CURPATH)/test-lint/lint.out)" || ret=1 ; \ + cat $(CURPATH)/test-lint/lint.out ; rm -rf $(CURPATH)/test-lint ; \ + exit $$ret