Skip to content

Commit 8a6af60

Browse files
authored
Add golangci github action and replace the deprecated golint (#10187)
* Add golangci github action and replace the deprecated golint Signed-off-by: z1cheng <[email protected]> * Install if golangci-lint not exists Signed-off-by: z1cheng <[email protected]> * Use -z operator Signed-off-by: z1cheng <[email protected]> * Fix json tag for DatadogSampleRate field in config.go Signed-off-by: z1cheng <[email protected]> * Add golangci linters Signed-off-by: z1cheng <[email protected]> * Revert DatadogSampleRate fix Signed-off-by: z1cheng <[email protected]> * Fix comments Signed-off-by: z1cheng <[email protected]> * Add a new line Signed-off-by: z1cheng <[email protected]> * fixup! Add a new line Signed-off-by: z1cheng <[email protected]> * Add trigger condition Signed-off-by: z1cheng <[email protected]> * Add golint-check entry in makefile Signed-off-by: Chen Chen <[email protected]> * Run golint-check in a container Signed-off-by: Chen Chen <[email protected]> --------- Signed-off-by: z1cheng <[email protected]> Signed-off-by: Chen Chen <[email protected]>
1 parent c9ca168 commit 8a6af60

File tree

5 files changed

+289
-33
lines changed

5 files changed

+289
-33
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,6 @@ jobs:
7878
# G307 TODO: Deferring unsafe method "Close"
7979
args: -exclude=G109,G601,G104,G204,G304,G306,G307 -tests=false -exclude-dir=test -exclude-dir=images/ -exclude-dir=docs/ ./...
8080

81-
lint:
82-
runs-on: ubuntu-latest
83-
needs: changes
84-
if: |
85-
(needs.changes.outputs.go == 'true')
86-
steps:
87-
- name: Checkout
88-
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
89-
90-
- name: Set up Go
91-
id: go
92-
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
93-
with:
94-
go-version: '1.20'
95-
check-latest: true
96-
97-
- name: Run Lint
98-
run: ./hack/verify-golint.sh
99-
10081
gofmt:
10182
runs-on: ubuntu-latest
10283
needs: changes
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: golangci-lint
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
paths-ignore:
9+
- 'docs/**'
10+
- 'deploy/**'
11+
- '**.md'
12+
13+
permissions:
14+
contents: read
15+
16+
jobs:
17+
golangci:
18+
name: lint
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
23+
24+
- name: Set up Go
25+
id: go
26+
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
27+
with:
28+
go-version: '1.20'
29+
check-latest: true
30+
31+
- name: golangci-lint
32+
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0
33+
with:
34+
version: v1.53

.golangci.yml

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
run:
2+
timeout: 10m
3+
allow-parallel-runners: true
4+
5+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
6+
max-issues-per-linter: 0
7+
8+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
9+
max-same-issues: 0
10+
linters:
11+
disable-all: true
12+
enable:
13+
- asasalint
14+
- asciicheck
15+
- bidichk
16+
- bodyclose
17+
- contextcheck
18+
- decorder
19+
- dogsled
20+
- dupl
21+
- durationcheck
22+
- errcheck
23+
- errchkjson
24+
- errname
25+
- execinquery
26+
- ginkgolinter
27+
- gocheckcompilerdirectives
28+
- goconst
29+
- gocritic
30+
- gocyclo
31+
- godox
32+
- gofmt
33+
- gofumpt
34+
- goheader
35+
- goimports
36+
- gomoddirectives
37+
- gomodguard
38+
- goprintffuncname
39+
- gosec
40+
- gosimple
41+
- govet
42+
- grouper
43+
- importas
44+
- ineffassign
45+
- loggercheck
46+
- makezero
47+
- misspell
48+
- musttag
49+
- nakedret
50+
- nolintlint
51+
- nosprintfhostport
52+
- prealloc
53+
- predeclared
54+
- promlinter
55+
- reassign
56+
- revive
57+
- rowserrcheck
58+
- sqlclosecheck
59+
- staticcheck
60+
- stylecheck
61+
- tenv
62+
- testableexamples
63+
- typecheck
64+
- unconvert
65+
- unparam
66+
- unused
67+
- usestdlibvars
68+
- whitespace
69+
# - containedctx
70+
# - cyclop
71+
# - dupword
72+
# - errorlint
73+
# - exhaustive
74+
# - exhaustruct
75+
# - exportloopref
76+
# - forbidigo
77+
# - forcetypeassert
78+
# - funlen
79+
# - gci
80+
# - gochecknoglobals
81+
# - gochecknoinits
82+
# - gocognit
83+
# - godot
84+
# - goerr113
85+
# - gomnd
86+
# - interfacebloat
87+
# - ireturn
88+
# - lll
89+
# - maintidx
90+
# - nestif
91+
# - nilerr
92+
# - nilnil
93+
# - nlreturn
94+
# - noctx
95+
# - nonamedreturns
96+
# - paralleltest
97+
# - tagliatelle
98+
# - testpackage
99+
# - thelper
100+
# - tparallel
101+
# - varnamelen
102+
# - wastedassign
103+
# - wrapcheck
104+
# - wsl
105+
linters-settings:
106+
gocyclo:
107+
min-complexity: 40
108+
godox:
109+
keywords:
110+
- BUG
111+
- FIXME
112+
- HACK
113+
errcheck:
114+
check-type-assertions: true
115+
check-blank: true
116+
gocritic:
117+
enabled-checks:
118+
# Diagnostic
119+
- appendAssign
120+
- argOrder
121+
- badCall
122+
- badCond
123+
- badLock
124+
- badRegexp
125+
- badSorting
126+
- builtinShadowDecl
127+
- caseOrder
128+
- codegenComment
129+
- commentedOutCode
130+
- deferInLoop
131+
- deprecatedComment
132+
- dupArg
133+
- dupBranchBody
134+
- dupCase
135+
- dupSubExpr
136+
- dynamicFmtString
137+
- emptyDecl
138+
- evalOrder
139+
- exitAfterDefer
140+
- externalErrorReassign
141+
- filepathJoin
142+
- flagDeref
143+
- flagName
144+
- mapKey
145+
- nilValReturn
146+
- offBy1
147+
- regexpPattern
148+
- returnAfterHttpError
149+
- sloppyReassign
150+
- sloppyTypeAssert
151+
- sortSlice
152+
- sprintfQuotedString
153+
- sqlQuery
154+
- syncMapLoadAndDelete
155+
- truncateCmp
156+
- unnecessaryDefer
157+
- weakCond
158+
159+
# Performance
160+
- appendCombine
161+
- equalFold
162+
- hugeParam
163+
- indexAlloc
164+
- preferDecodeRune
165+
- preferFprint
166+
- preferStringWriter
167+
- preferWriteByte
168+
- rangeExprCopy
169+
- rangeValCopy
170+
- sliceClear
171+
- stringXbytes
172+
173+
# Style
174+
- assignOp
175+
- boolExprSimplify
176+
- captLocal
177+
- commentFormatting
178+
- commentedOutImport
179+
- defaultCaseOrder
180+
- deferUnlambda
181+
- docStub
182+
- dupImport
183+
- elseif
184+
- emptyFallthrough
185+
- emptyStringTest
186+
- exposedSyncMutex
187+
- hexLiteral
188+
- httpNoBody
189+
- ifElseChain
190+
- methodExprCall
191+
- newDeref
192+
- octalLiteral
193+
- preferFilepathJoin
194+
- redundantSprint
195+
- regexpMust
196+
- regexpSimplify
197+
- ruleguard
198+
- singleCaseSwitch
199+
- sloppyLen
200+
- stringConcatSimplify
201+
- stringsCompare
202+
- switchTrue
203+
- timeCmpSimplify
204+
- timeExprSimplify
205+
- todoCommentWithoutDetail
206+
- tooManyResultsChecker
207+
- typeAssertChain
208+
- typeDefFirst
209+
- typeSwitchVar
210+
- underef
211+
- unlabelStmt
212+
- unlambda
213+
- unslice
214+
- valSwap
215+
- whyNoLint
216+
- wrapperFunc
217+
- yodaStyleExpr
218+
219+
# Opinionated
220+
- builtinShadow
221+
- importShadow
222+
- initClause
223+
- nestingReduce
224+
- paramTypeCombine
225+
- ptrToRefParam
226+
- typeUnparen
227+
- unnamedResult
228+
- unnecessaryBlock
229+
nolintlint:
230+
# Enable to ensure that nolint directives are all used. Default is true.
231+
allow-unused: false
232+
# Disable to ensure that nolint directives don't have a leading space. Default is true.
233+
# TODO(lint): Enforce machine-readable `nolint` directives
234+
allow-leading-space: true
235+
# Exclude following linters from requiring an explanation. Default is [].
236+
allow-no-explanation: []
237+
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
238+
# TODO(lint): Enforce explanations for `nolint` directives
239+
require-explanation: false
240+
# Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
241+
require-specific: true

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ static-check: ## Run verification script for boilerplate, codegen, gofmt, golint
128128
MAC_OS=$(MAC_OS) \
129129
hack/verify-all.sh
130130

131+
.PHONY: golint-check
132+
golint-check:
133+
@build/run-in-docker.sh \
134+
MAC_OS=$(MAC_OS) \
135+
hack/verify-golint.sh
136+
131137
###############################
132138
# Tests for ingress-nginx
133139
###############################

hack/verify-golint.sh

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2222

2323
cd "${KUBE_ROOT}"
2424

25-
GOLINT=${GOLINT:-"golint"}
26-
PACKAGES=($(go list ./internal/... | grep -v /vendor/))
27-
bad_files=()
28-
for package in "${PACKAGES[@]}"; do
29-
out=$("${GOLINT}" -min_confidence=0.9 "${package}" | grep -v -E '(should not use dot imports|internal/file/bindata.go)' || :)
30-
if [[ -n "${out}" ]]; then
31-
bad_files+=("${out}")
32-
fi
33-
done
34-
if [[ "${#bad_files[@]}" -ne 0 ]]; then
35-
echo "!!! '$GOLINT' problems: "
36-
echo "${bad_files[@]}"
37-
exit 1
25+
LINT=${LINT:-golangci-lint}
26+
27+
if [[ -z "$(command -v ${LINT})" ]]; then
28+
echo "${LINT} is missing. Installing it now."
29+
# See: https://golangci-lint.run/usage/install/#local-installation
30+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3
31+
LINT=$(go env GOPATH)/bin/golangci-lint
3832
fi
3933

40-
# ex: ts=2 sw=2 et filetype=sh
34+
${LINT} run

0 commit comments

Comments
 (0)