From 2fea8df41fe5709b871064bd310ca4a9622d0ac6 Mon Sep 17 00:00:00 2001 From: Tim Ebert Date: Wed, 1 Nov 2023 18:36:04 +0100 Subject: [PATCH] Add golangci-lint --- .golangci.yaml | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 12 ++++----- hack/tools.mk | 5 ++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 .golangci.yaml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..c7e8c7a2 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,68 @@ +run: + concurrency: 4 + deadline: 10m + + skip-files: + - "zz_generated\\..*\\.go$" + +linters: + enable: + - exportloopref + - ginkgolinter + - gocritic + - gosec + - importas + - misspell + - nilerr + - nolintlint + - prealloc + - revive + - stylecheck + - unconvert + - unparam + - whitespace + +linters-settings: + importas: + alias: + - pkg: k8s.io/api/(\w+)/(v[\w\d]+) + alias: $1$2 + - pkg: k8s.io/apimachinery/pkg/apis/(\w+)/(v[\w\d]+) + alias: $1$2 + - pkg: k8s.io/apimachinery/pkg/api/([^m]\w+) + alias: api${1} + - pkg: k8s.io/apimachinery/pkg/util/(\w+) + alias: util${1} + - pkg: k8s.io/client-go/tools/clientcmd/api/(\w+) + alias: clientcmd${1} + - pkg: k8s.io/client-go/tools/cache + alias: toolscache + - pkg: k8s.io/component-base/config/(v[\w\d]+) + alias: componentbaseconfig$1 + - pkg: k8s.io/utils/clock/testing + alias: testclock + - pkg: sigs.k8s.io/controller-runtime/pkg/client/fake + alias: fakeclient + - pkg: sigs.k8s.io/controller-runtime/pkg/log/zap + alias: logzap + - pkg: sigs.k8s.io/controller-runtime/pkg/log + alias: logf + misspell: + locale: US + nolintlint: + require-specific: true + revive: + rules: + - name: context-as-argument + - name: duplicated-imports + - name: early-return + - name: exported + - name: unreachable-code + whitespace: + multi-func: true + +issues: + exclude-rules: + - linters: + - stylecheck + text: "ST1003:.+func SetDefaults_" diff --git a/Makefile b/Makefile index 9728a340..f0ecbedb 100644 --- a/Makefile +++ b/Makefile @@ -66,12 +66,12 @@ test-kyverno: $(KYVERNO) ## Run kyverno policy tests. ##@ Verification -.PHONY: vet -vet: ## Run go vet against code. - go vet ./... +.PHONY: lint +lint: $(GOLANGCI_LINT) ## Run golangci-lint against code. + $(GOLANGCI_LINT) run ./... .PHONY: check -check: vet test test-kyverno ## Check everything (vet + test + test-kyverno). +check: lint test test-kyverno ## Check everything (lint + test + test-kyverno). .PHONY: verify-fmt verify-fmt: fmt ## Verify go code is formatted. @@ -97,11 +97,11 @@ verify: verify-fmt verify-generate verify-modules check ## Verify everything (al ##@ Build .PHONY: build -build: generate fmt vet ## Build the sharder binary. +build: ## Build the sharder binary. go build -o bin/sharder ./cmd/sharder .PHONY: run -run: generate fmt vet ## Run the sharder from your host. +run: ## Run the sharder from your host. go run ./cmd/sharder PUSH ?= false diff --git a/hack/tools.mk b/hack/tools.mk index dae623b4..191f1547 100644 --- a/hack/tools.mk +++ b/hack/tools.mk @@ -22,6 +22,11 @@ CONTROLLER_GEN_VERSION ?= v0.13.0 $(CONTROLLER_GEN): $(call tool_version_file,$(CONTROLLER_GEN),$(CONTROLLER_GEN_VERSION)) GOBIN=$(abspath $(TOOLS_BIN_DIR)) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) +GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint +GOLANGCI_LINT_VERSION ?= v1.55.1 +$(GOLANGCI_LINT): $(call tool_version_file,$(GOLANGCI_LINT),$(GOLANGCI_LINT_VERSION)) + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_BIN_DIR) $(GOLANGCI_LINT_VERSION) + KIND := $(TOOLS_BIN_DIR)/kind KIND_VERSION ?= v0.20.0 $(KIND): $(call tool_version_file,$(KIND),$(KIND_VERSION))