Skip to content

Commit dac6705

Browse files
committed
🌱 Align golangci-lint with upstream Cluster API
Signed-off-by: Vince Prignano <[email protected]>
1 parent 9b6fe34 commit dac6705

File tree

207 files changed

+730
-407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+730
-407
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: PR golangci-lint
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, synchronize, reopened]
6+
7+
# Remove all permissions from GITHUB_TOKEN except metadata.
8+
permissions: {}
9+
10+
jobs:
11+
golangci:
12+
name: lint
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
working-directory:
18+
- ""
19+
steps:
20+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # tag=v4.1.1
21+
- name: Calculate go version
22+
id: vars
23+
run: echo "go_version=$(make go-version)" >> $GITHUB_OUTPUT
24+
- name: Set up Go
25+
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # tag=v5.0.0
26+
with:
27+
go-version: ${{ steps.vars.outputs.go_version }}
28+
- name: golangci-lint
29+
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # tag=v4.0.0
30+
with:
31+
version: v1.56.1
32+
args: --out-format=colored-line-number
33+
working-directory: ${{matrix.working-directory}}

.golangci.yml

+130-68
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,126 @@
11
linters:
2-
enable-all: true
3-
disable:
4-
- bidichk
5-
- contextcheck
6-
- cyclop
7-
- dupl
8-
- durationcheck
9-
- errname
10-
- errorlint
11-
- exhaustive
12-
- exhaustivestruct
13-
- exhaustruct
14-
- forcetypeassert
15-
- forbidigo
16-
- funlen
17-
- gochecknoglobals
18-
- gochecknoinits
19-
- gocognit
20-
- godox
21-
- goerr113
22-
- gofumpt
23-
- golint
24-
- gomnd
25-
- gomoddirectives
26-
- gomodguard
27-
- interfacer
28-
- ireturn
29-
- lll
30-
- makezero
31-
- maligned
32-
- musttag
33-
- nestif
34-
- nilnil
35-
- nlreturn
36-
- nonamedreturns
37-
- nosnakecase
38-
- paralleltest
39-
- promlinter
40-
- scopelint
41-
- sqlclosecheck
42-
- tagliatelle
43-
- tenv
44-
- testpackage
45-
- tparallel
46-
- varnamelen
47-
- wastedassign
48-
- wrapcheck
49-
- wsl
50-
- deadcode
51-
- ifshort
52-
- structcheck
53-
- varcheck
54-
- interfacebloat
2+
disable-all: true
3+
enable:
4+
- asasalint
5+
- asciicheck
6+
- bidichk
7+
- bodyclose
8+
- containedctx
9+
- dogsled
10+
- dupword
11+
- durationcheck
12+
- errcheck
13+
- errchkjson
14+
- exportloopref
15+
- gci
16+
- ginkgolinter
17+
- goconst
18+
- gocritic
19+
- godot
20+
- gofmt
21+
- goimports
22+
- goprintffuncname
23+
- gosec
24+
- gosimple
25+
- govet
26+
- importas
27+
- ineffassign
28+
- loggercheck
29+
- misspell
30+
- nakedret
31+
- nilerr
32+
- noctx
33+
- nolintlint
34+
- nosprintfhostport
35+
- prealloc
36+
- predeclared
37+
- revive
38+
- rowserrcheck
39+
- staticcheck
40+
- stylecheck
41+
- thelper
42+
- typecheck
43+
- unconvert
44+
- unparam
45+
- unused
46+
- usestdlibvars
47+
- whitespace
5548

5649
linters-settings:
57-
# Restrict revive to exported.
58-
revive:
59-
# see https://github.com/mgechev/revive#available-rules for details.
60-
ignore-generated-header: true
61-
severity: warning
62-
rules:
63-
- name: exported
64-
severity: warning
6550
gci:
6651
sections:
6752
- standard
6853
- default
6954
- prefix(sigs.k8s.io/cluster-api)
7055
ginkgolinter:
71-
# Suppress the wrong length assertion warning.
72-
suppress-len-assertion: true
73-
# Suppress the wrong nil assertion warning.
74-
suppress-nil-assertion: false
75-
# Suppress the wrong error assertion warning.
76-
suppress-err-assertion: true
56+
forbid-focus-container: true
57+
suppress-len-assertion: true # Suppress the wrong length assertion warning.
58+
suppress-nil-assertion: false # Suppress the wrong nil assertion warning.
59+
suppress-err-assertion: true # Suppress the wrong error assertion warning.
60+
gocritic:
61+
enabled-tags:
62+
- diagnostic
63+
- experimental
64+
- performance
65+
disabled-checks:
66+
- appendAssign
67+
- dupImport # https://github.com/go-critic/go-critic/issues/845
68+
- evalOrder
69+
- ifElseChain
70+
- octalLiteral
71+
- regexpSimplify
72+
- sloppyReassign
73+
- truncateCmp
74+
- typeDefFirst
75+
- unnamedResult
76+
- unnecessaryDefer
77+
- whyNoLint
78+
- wrapperFunc
79+
- rangeValCopy
80+
- hugeParam
81+
- filepathJoin
82+
- emptyStringTest
83+
godot:
84+
# declarations - for top level declaration comments (default);
85+
# toplevel - for top level comments;
86+
# all - for all comments.
87+
scope: toplevel
88+
exclude:
89+
- '^ \+.*'
90+
- '^ ANCHOR.*'
91+
revive:
92+
rules:
93+
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
94+
- name: blank-imports
95+
- name: context-as-argument
96+
- name: context-keys-type
97+
- name: dot-imports
98+
- name: error-return
99+
- name: error-strings
100+
- name: error-naming
101+
- name: exported
102+
- name: if-return
103+
- name: increment-decrement
104+
- name: var-naming
105+
- name: var-declaration
106+
- name: package-comments
107+
- name: range
108+
- name: receiver-naming
109+
- name: time-naming
110+
- name: unexported-return
111+
- name: indent-error-flow
112+
- name: errorf
113+
- name: empty-block
114+
- name: superfluous-else
115+
- name: unreachable-code
116+
- name: redefines-builtin-id
117+
#
118+
# Rules in addition to the recommended configuration above.
119+
#
120+
- name: bool-literal-in-expr
121+
- name: constant-logical-expr
122+
goconst:
123+
ignore-tests: true
77124
gosec:
78125
excludes:
79126
- G307 # Deferring unsafe method "Close" on type "\*os.File"
@@ -159,6 +206,10 @@ linters-settings:
159206
alias: apimachinerytypes
160207
- pkg: "sigs.k8s.io/cluster-api/exp/api/v1beta1"
161208
alias: expclusterv1
209+
nolintlint:
210+
allow-unused: false
211+
allow-leading-space: false
212+
require-specific: true
162213
staticcheck:
163214
go: "1.21"
164215
stylecheck:
@@ -178,7 +229,6 @@ issues:
178229
# List of regexps of issue texts to exclude, empty list by default.
179230
exclude:
180231
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
181-
- "exported: exported (const|function|method|type|var) (.+) should have comment or be unexported"
182232
- "exported: (func|type) name will be used as (.+) by other packages, and that stutters; consider calling this (.+)"
183233
- (G104|G107|G404|G505|ST1000)
184234
- "G108: Profiling endpoint is automatically exposed on /debug/pprof"
@@ -188,6 +238,13 @@ issues:
188238
- "net/http.Get must not be called"
189239
exclude-rules:
190240
# Exclude revive's exported for certain packages and code, e.g. tests and fake.
241+
- linters:
242+
- revive
243+
text: "exported: exported method .*\\.(Reconcile|SetupWithManager|SetupWebhookWithManager) should have comment or be unexported"
244+
- linters:
245+
- errcheck
246+
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
247+
# Exclude some packages or code to require comments, for example test code, or fake clients.
191248
- linters:
192249
- revive
193250
text: exported (method|function|type|const) (.+) should have comment or be unexported
@@ -229,6 +286,11 @@ issues:
229286
- revive
230287
text: "var-naming: don't use underscores in Go names; func (.+) should be (.+)"
231288
path: .*/defaults.go
289+
# These directives allow the mock and gc packages to be imported with an underscore everywhere.
290+
- linters:
291+
- revive
292+
text: "var-naming: don't use an underscore in package name"
293+
path: .*/.*(mock|gc_).*/.+\.go
232294
# Disable unparam "always receives" which might not be really
233295
# useful when building libraries.
234296
- linters:

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ KUBETEST_CONF_PATH ?= $(abspath $(E2E_DATA_DIR)/kubetest/conformance.yaml)
4848
EXP_DIR := exp
4949

5050
# Binaries.
51+
GO_INSTALL := ./scripts/go_install.sh
5152
GO_APIDIFF_BIN := $(BIN_DIR)/go-apidiff
5253
GO_APIDIFF := $(TOOLS_DIR)/$(GO_APIDIFF_BIN)
5354
CLUSTERCTL := $(BIN_DIR)/clusterctl
@@ -58,7 +59,10 @@ DEFAULTER_GEN := $(TOOLS_BIN_DIR)/defaulter-gen
5859
ENVSUBST := $(TOOLS_BIN_DIR)/envsubst
5960
GH := $(TOOLS_BIN_DIR)/gh
6061
GOJQ := $(TOOLS_BIN_DIR)/gojq
61-
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
62+
GOLANGCI_LINT_BIN := golangci-lint
63+
GOLANGCI_LINT_VER := $(shell cat .github/workflows/pr-golangci-lint.yaml | grep [[:space:]]version: | sed 's/.*version: //')
64+
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER))
65+
GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
6266
KIND := $(TOOLS_BIN_DIR)/kind
6367
KUSTOMIZE := $(TOOLS_BIN_DIR)/kustomize
6468
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen
@@ -290,6 +294,9 @@ generate-go-apis: ## Alias for .build/generate-go-apis
290294

291295
.PHONY: modules
292296

297+
$(GOLANGCI_LINT): # Build golangci-lint from tools folder.
298+
GOBIN=$(abspath $(TOOLS_BIN_DIR)) $(GO_INSTALL) $(GOLANGCI_LINT_PKG) $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)
299+
293300
.PHONY: lint
294301
lint: $(GOLANGCI_LINT) ## Lint codebase
295302
$(GOLANGCI_LINT) run -v --fast=false $(GOLANGCI_LINT_EXTRA_ARGS)

api/v1beta1/awscluster_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ type AWSClusterStatus struct {
207207
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
208208
}
209209

210+
// S3Bucket defines a supporting S3 bucket for the cluster, currently can be optionally used for Ignition.
210211
type S3Bucket struct {
211212
// ControlPlaneIAMInstanceProfile is a name of the IAMInstanceProfile, which will be allowed
212213
// to read control-plane node bootstrap data from S3 Bucket.

api/v1beta1/awsclustertemplate_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func init() {
5353
SchemeBuilder.Register(&AWSClusterTemplate{}, &AWSClusterTemplateList{})
5454
}
5555

56+
// AWSClusterTemplateResource defines the desired state of AWSClusterTemplate.
5657
type AWSClusterTemplateResource struct {
5758
// Standard object's metadata.
5859
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

api/v1beta1/conversion_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ package v1beta1
1919
import (
2020
"testing"
2121

22-
. "github.com/onsi/gomega"
23-
2422
fuzz "github.com/google/gofuzz"
23+
. "github.com/onsi/gomega"
2524
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2625
"k8s.io/apimachinery/pkg/runtime"
2726
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
@@ -38,7 +37,7 @@ func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
3837

3938
func AWSMachineFuzzer(obj *AWSMachine, c fuzz.Continue) {
4039
c.FuzzNoCustom(obj)
41-
40+
4241
// AWSMachine.Spec.FailureDomain, AWSMachine.Spec.Subnet.ARN and AWSMachine.Spec.AdditionalSecurityGroups.ARN has been removed in v1beta2, so setting it to nil in order to avoid v1beta1 --> v1beta2 --> v1beta1 round trip errors.
4342
if obj.Spec.Subnet != nil {
4443
obj.Spec.Subnet.ARN = nil
@@ -54,7 +53,7 @@ func AWSMachineFuzzer(obj *AWSMachine, c fuzz.Continue) {
5453

5554
func AWSMachineTemplateFuzzer(obj *AWSMachineTemplate, c fuzz.Continue) {
5655
c.FuzzNoCustom(obj)
57-
56+
5857
// AWSMachineTemplate.Spec.Template.Spec.FailureDomain, AWSMachineTemplate.Spec.Template.Spec.Subnet.ARN and AWSMachineTemplate.Spec.Template.Spec.AdditionalSecurityGroups.ARN has been removed in v1beta2, so setting it to nil in order to avoid v1beta1 --> v1beta2 --> v1beta round trip errors.
5958
if obj.Spec.Template.Spec.Subnet != nil {
6059
obj.Spec.Template.Spec.Subnet.ARN = nil
@@ -81,16 +80,16 @@ func TestFuzzyConversion(t *testing.T) {
8180
}))
8281

8382
t.Run("for AWSMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
84-
Scheme: scheme,
85-
Hub: &v1beta2.AWSMachine{},
86-
Spoke: &AWSMachine{},
83+
Scheme: scheme,
84+
Hub: &v1beta2.AWSMachine{},
85+
Spoke: &AWSMachine{},
8786
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
8887
}))
8988

9089
t.Run("for AWSMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
91-
Scheme: scheme,
92-
Hub: &v1beta2.AWSMachineTemplate{},
93-
Spoke: &AWSMachineTemplate{},
90+
Scheme: scheme,
91+
Hub: &v1beta2.AWSMachineTemplate{},
92+
Spoke: &AWSMachineTemplate{},
9493
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
9594
}))
9695

0 commit comments

Comments
 (0)