Skip to content

Commit 97b2c12

Browse files
committed
this commit scaffolds golangci-lint for capz
Adds - pr-golangci-lint job - exports GO_VERSION in the makefile that is used in other make targets - updates .golangci.yml with CAPI changes, and also rebases CAPZ additions to it. - update .golangci.yml - ignore package comments across CAPZ
1 parent 553cd0f commit 97b2c12

File tree

339 files changed

+1177
-827
lines changed

Some content is hidden

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

339 files changed

+1177
-827
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
- test
20+
steps:
21+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # tag=v4.1.7
22+
23+
- name: Calculate go version
24+
id: vars
25+
run: echo "go_version=$(make go-version)" >> $GITHUB_OUTPUT
26+
27+
- name: Set up Go
28+
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # tag=v5.0.2
29+
with:
30+
go-version: ${{ steps.vars.outputs.go_version }}
31+
32+
- name: Cache golangci-lint binary
33+
id: golangci-cache
34+
uses: actions/cache@v3
35+
with:
36+
# Cache location used by golangci-lint-action
37+
path: ~/.cache/golangci-lint
38+
# Change this when updating golangci-lint version
39+
key: golangci-lint-v1.60.2
40+
restore-keys: |
41+
golangci-lint-
42+
43+
- name: golangci-lint
44+
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # tag=v6.1.0
45+
with:
46+
# update the version when updating golangci-lint
47+
version: v1.60.2
48+
args: --out-format=colored-line-number
49+
working-directory: ${{matrix.working-directory}}

.golangci.yml

Lines changed: 230 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,98 @@
11
run:
2-
deadline: 10m
3-
skip-files:
4-
- 'zz_generated\.(\w*)\.go$'
2+
timeout: 10m
53
build-tags:
64
- e2e
5+
allow-parallel-runners: true
6+
77
linters:
88
disable-all: true
99
enable:
10-
- asasalint
11-
- asciicheck
12-
- bidichk
13-
- bodyclose
14-
- containedctx
15-
- decorder
16-
- dogsled
17-
- dupword
18-
- durationcheck
19-
- errcheck
20-
- errchkjson
21-
- errorlint
22-
- execinquery
23-
- exportloopref
24-
- gci
25-
- ginkgolinter
26-
- goconst
27-
- gocritic
28-
- gocyclo
29-
- godot
30-
- gofmt
31-
- goimports
32-
- goprintffuncname
33-
- gosec
34-
- gosimple
35-
- govet
36-
- importas
37-
- ineffassign
38-
- loggercheck
39-
- misspell
40-
- nakedret
41-
- nilerr
42-
- noctx
43-
- nolintlint
44-
- nosprintfhostport
45-
- predeclared
46-
- reassign
47-
- revive
48-
- rowserrcheck
49-
- staticcheck
50-
- stylecheck
51-
- thelper
52-
- typecheck
53-
- unconvert
54-
- unparam
55-
- unused
56-
- usestdlibvars
57-
- whitespace
58-
# Run with --fast=false for more extensive checks
59-
fast: true
10+
- asasalint # warns about passing []any to func(...any) without expanding it
11+
- asciicheck # non ascii symbols
12+
- bidichk # dangerous unicode sequences
13+
- bodyclose # unclosed http bodies
14+
- containedctx # context.Context nested in a struct
15+
- copyloopvar # copying loop variables
16+
- dogsled # too many blank identifiers in assignments
17+
- dupword # duplicate words
18+
- durationcheck # multiplying two durations
19+
- errcheck # unchecked errors
20+
- errchkjson # invalid types passed to json encoder
21+
- gci # ensures imports are organized
22+
- ginkgolinter # ginkgo and gomega
23+
- goconst # strings that can be replaced by constants
24+
- gocritic # bugs, performance, style (we could add custom ones to this one)
25+
- godot # checks that comments end in a period
26+
- gofmt # warns about incorrect use of fmt functions
27+
- goimports # import formatting
28+
- goprintffuncname # printft-like functions should be named with f at the end
29+
- gosec # potential security problems
30+
- gosimple # simplify code
31+
- govet # basically 'go vet'
32+
- importas # consistent import aliases
33+
- ineffassign # ineffectual assignments
34+
- intrange # suggest using integer range in for loops
35+
- loggercheck # check for even key/value pairs in logger calls
36+
- misspell # spelling
37+
- nakedret # naked returns (named return parameters and an empty return)
38+
- nilerr # returning nil after checking err is not nil
39+
- noctx # http requests without context.Context
40+
- nolintlint # badly formatted nolint directives
41+
- nosprintfhostport # using sprintf to construct host:port in a URL
42+
- prealloc # suggest preallocating slices
43+
- predeclared # shadowing predeclared identifiers
44+
- revive # better version of golint
45+
- staticcheck # some of staticcheck's rules
46+
- stylecheck # another replacement for golint
47+
- tenv # using os.Setenv instead of t.Setenv in tests
48+
- thelper # test helpers not starting with t.Helper()
49+
- unconvert # unnecessary type conversions
50+
- unparam # unused function parameters
51+
- unused # unused constants, variables,functions, types
52+
- usestdlibvars # using variables/constants from the standard library
53+
- whitespace # unnecessary newlines
6054

6155
linters-settings:
56+
gosec:
57+
excludes:
58+
- G307 # Deferring unsafe method "Close" on type "\*os.File"
59+
- G108 # Profiling endpoint is automatically exposed on /debug/pprof
60+
- G115 # integer overflow conversion int -> int32
61+
gci:
62+
sections:
63+
- standard # Standard section: captures all standard packages.
64+
- default # Default section: contains all imports that could not be matched to another section type.
65+
- prefix(sigs.k8s.io/cluster-api-provider-azure) # Custom section: groups all imports with the specified Prefix.
66+
custom-order: true
67+
ginkgolinter:
68+
forbid-focus-container: true
69+
goconst:
70+
ignore-tests: true
71+
godot:
72+
# declarations - for top level declaration comments (default);
73+
# toplevel - for top level comments;
74+
# all - for all comments.
75+
scope: toplevel
76+
exclude:
77+
- '^ \+.*'
78+
- '^ ANCHOR.*'
79+
gocritic:
80+
enabled-tags:
81+
- "experimental"
82+
disabled-checks:
83+
- appendAssign
84+
- dupImport # https://github.com/go-critic/go-critic/issues/845
85+
- evalOrder
86+
- ifElseChain
87+
- octalLiteral
88+
- regexpSimplify
89+
- sloppyReassign
90+
- truncateCmp
91+
- typeDefFirst
92+
- unnamedResult
93+
- unnecessaryDefer
94+
- whyNoLint
95+
- wrapperFunc
6296
importas:
6397
no-unaliased: true
6498
alias:
@@ -102,34 +136,49 @@ linters-settings:
102136
# Deprecated
103137
- pkg: github.com/Azure/go-autorest/autorest/to
104138
alias: deprecated-use-k8s.io-utils-pointer
105-
gocritic:
106-
enabled-tags:
107-
- "experimental"
108-
godot:
109-
# declarations - for top level declaration comments (default);
110-
# toplevel - for top level comments;
111-
# all - for all comments.
112-
scope: toplevel
113-
exclude:
114-
- '^ \+.*'
115-
- '^ ANCHOR.*'
116-
gosec:
117-
excludes:
118-
- G307 # Deferring unsafe method "Close" on type "\*os.File"
119-
- G108 # Profiling endpoint is automatically exposed on /debug/pprof
139+
nolintlint:
140+
allow-unused: false
141+
require-specific: true
120142
revive:
121143
rules:
144+
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
145+
- name: blank-imports
146+
- name: context-as-argument
147+
- name: context-keys-type
148+
- name: dot-imports
149+
- name: error-return
150+
- name: error-strings
151+
- name: error-naming
152+
- name: if-return
153+
- name: increment-decrement
154+
- name: var-naming
155+
- name: var-declaration
156+
- name: package-comments
157+
- name: range
158+
- name: receiver-naming
159+
- name: time-naming
160+
- name: unexported-return
161+
- name: indent-error-flow
162+
- name: errorf
163+
- name: empty-block
164+
- name: superfluous-else
165+
- name: unused-parameter
166+
- name: unreachable-code
167+
- name: redefines-builtin-id
168+
#
169+
# Rules in addition to the recommended configuration above.
170+
#
171+
- name: bool-literal-in-expr
172+
- name: constant-logical-expr
122173
- name: exported
123174
arguments:
124175
- disableStutteringCheck
125-
staticcheck:
126-
go: "1.22"
127-
stylecheck:
128-
go: "1.22"
129176
unused:
130177
go: "1.22"
131178

132179
issues:
180+
exclude-files:
181+
- 'zz_generated\.(\w*)\.go$'
133182
exclude-rules:
134183
- path: '(\w*)conversion.go'
135184
text: "use underscores in Go names|receiver name (.+) should be consistent|methods on the same type should have the same receiver name"
@@ -145,8 +194,114 @@ issues:
145194
text: exported (.+) should have comment( \(or a comment on this block\))? or be unexported
146195
- source: \"github.com/onsi/(ginkgo/v2|gomega)\"
147196
text: "should not use dot imports"
148-
include:
149-
- EXC0012 # revive: check for comments
150-
- EXC0014 # revive: check for comments
197+
# Exclude some packages or code to require comments, for example test code, or fake clients.
198+
- linters:
199+
- revive
200+
text: exported (method|function|type|const) (.+) should have comment or be unexported
201+
source: (func|type).*Fake.*
202+
- linters:
203+
- revive
204+
text: exported (method|function|type|const) (.+) should have comment or be unexported
205+
path: fake_\.go
206+
# Dot imports for gomega and ginkgo are allowed
207+
# within test files and test utils.
208+
- linters:
209+
- revive
210+
- stylecheck
211+
path: _test\.go
212+
text: should not use dot imports
213+
- linters:
214+
- revive
215+
# Checking if an error is nil to just after return the error or nil is redundant
216+
text: "if-return: redundant if ...; err != nil check, just return error instead"
217+
# Ignoring stylistic checks for generated code
218+
path: .*(api|types|test)\/.*\/conversion.*\.go$
219+
- linters:
220+
- revive
221+
# Exported function and methods should have comments. This warns on undocumented exported functions and methods.
222+
text: exported (method|function|type|const) (.+) should have comment or be unexported
223+
# Ignoring stylistic checks for generated code
224+
path: .*(api|types|test)\/.*\/conversion.*\.go$
225+
- linters:
226+
- revive
227+
# By convention, receiver names in a method should reflect their identity.
228+
text: "receiver-naming: receiver name"
229+
# Ignoring stylistic checks for generated code
230+
path: .*(api|types)\/.*\/conversion.*\.go$
231+
- linters:
232+
- stylecheck
233+
text: "ST1016: methods on the same type should have the same receiver name"
234+
path: .*(api|types)\/.*\/conversion.*\.go$
235+
# We don't care about defer in for loops in test files.
236+
- linters:
237+
- gocritic
238+
text: "deferInLoop: Possible resource leak, 'defer' is called in the 'for' loop"
239+
path: _test\.go
240+
# Ignore non-constant format string in call to condition utils
241+
- linters:
242+
- govet
243+
text: "non-constant format string in call to sigs\\.k8s\\.io\\/cluster-api\\/util\\/conditions\\."
244+
# for i = x; i < y; i++ {} is already optimal; Ignore this linter message
245+
- linters:
246+
- intrange
247+
text: "for loop can be changed to use an integer range"
248+
# Ignore the return value of below functions in test files
249+
- linters:
250+
- errcheck
251+
text: Error return value of (`os.Setenv`|`fmt.Fprintf`|`resp.Body.Close`|`os.Unsetenv`|`fmt.Fprintln`|`[\w\.]+\.Close`|`[\w\.]+\.Flush`|`[\w\.]+\.RemoveAll`) is not checked
252+
path: (^test\/e2e\/.*\.go$|.*_test\.go$)
253+
# Do not validate file paths in tests
254+
- linters:
255+
- gosec
256+
text: "G304: Potential file inclusion via variable"
257+
path: ^test\/e2e\/.*\.go$
258+
# Ignore the elevated access that "others" have for dirs/files created in tests with 0755 permissions
259+
- linters:
260+
- gosec
261+
text: "G301: Expect directory permissions to be 0750 or less"
262+
path: ^test\/e2e\/.*\.go$
263+
# Ignore the elevated access that "group" and "others" have for dirs/files opened in tests with 0644 permissions
264+
- linters:
265+
- gosec
266+
text: "G302: Expect file permissions to be 0600 or less"
267+
path: ^test\/e2e\/.*\.go$
268+
# Ignore unhandled errors in test files when using os.Setenv
269+
- linters:
270+
- gosec
271+
text: "G104: Errors unhandled."
272+
path: ^test\/e2e\/.*\.go$
273+
# Ignore the unhandled errors on using os.Setenv in test files
274+
- linters:
275+
- gosec
276+
text: "G104: Errors unhandled."
277+
path: .*_test\.go$
278+
# Ignore unused parameters in test files
279+
- linters:
280+
- revive
281+
text: "^unused-parameter: parameter '.*' seems to be unused, consider removing or renaming it as _$"
282+
path: .*(_test|test)\.go$
283+
# Ignore adding package comments for all files. TODO: Ideally, we should add package comments to all the packages files.
284+
- linters:
285+
- revive
286+
text: "^package-comments: package comment should be of the form \".*\"$"
287+
path: .*\.go$
288+
# Ignore adding package comments for all files. TODO: Ideally, we should add package comments to all the packages files.
289+
- linters:
290+
- revive
291+
text: "package-comments: should have a package comment"
292+
path: .*\.go$
293+
# Ignore adding package comments for all files. TODO: Ideally, we should add package comments to all the packages files.
294+
- linters:
295+
- stylecheck
296+
text: "ST1000: at least one file in a package should have a package comment"
297+
path: .*\.go$
298+
# Ignore adding package comments for all files. TODO: Ideally, we should add package comments to all the packages files.
299+
- linters:
300+
- stylecheck
301+
text: "ST1000: package comment should be of the form \".*\"$"
302+
path: .*\.go$
303+
# We are disabling default golangci exclusions because we want to help reviewers to focus on reviewing the most relevant
304+
# changes in PRs and avoid nitpicking.
305+
exclude-use-default: false
151306
max-issues-per-linter: 0
152307
max-same-issues: 0

0 commit comments

Comments
 (0)