Skip to content

Commit 14f8b85

Browse files
authored
[improvement] bump golangci-lint, address linting comments and remove endpoint pkg (#227)
<!-- If this is your first PR, welcome! Please make sure you read the [contributing guidelines](../CONTRIBUTING.md). --> <!-- Ensure your PR title complies with the following guidelines 1. All PRs titles should start with one of the following prefixes - `[fix]` for PRs related to bug fixes and patches - `[feat]` for PRs related to new features - `[improvement]` for PRs related to improvements of existing features - `[test]` for PRs related to tests - `[CI]` for PRs related to repo CI improvements - `[docs]` for PRs related to documentation updates - `[deps]` for PRs related to dependency updates 2. if a PR introduces a breaking change it should include `[breaking]` in the title 3. if a PR introduces a deprecation it should include `[deprecation]` in the title --> **What this PR does / why we need it**: **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **TODOs**: <!-- Put an "X" character inside the brackets of each completed task. Some may be optional depending on the PR. --> - [ ] squashed commits - [ ] includes documentation - [ ] adds unit tests - [ ] adds or updates e2e tests
1 parent 0ca27fd commit 14f8b85

File tree

25 files changed

+309
-602
lines changed

25 files changed

+309
-602
lines changed

.golangci.yml

Lines changed: 167 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,185 @@
1+
version: "2"
12
run:
2-
timeout: 5m
3-
tests: true
4-
build-tags:
5-
- integration
6-
7-
issues:
8-
max-issues-per-linter: 0
9-
max-same-issues: 0
10-
uniq-by-line: false
11-
3+
issues-exit-code: 1
124
output:
13-
sort-results: true
14-
5+
formats:
6+
text:
7+
path: stdout
158
linters:
169
enable:
10+
- asasalint
11+
- asciicheck
12+
- bidichk
13+
- bodyclose
1714
- containedctx
1815
- contextcheck
16+
- copyloopvar
17+
- cyclop
18+
- decorder
19+
- depguard
1920
- dogsled
20-
- errcheck
21+
- dupword
22+
- durationcheck
23+
- errchkjson
24+
- errname
2125
- errorlint
22-
- gci
23-
- gosec
26+
- exhaustive
27+
- forbidigo
28+
- forcetypeassert
2429
- gocheckcompilerdirectives
30+
- gochecksumtype
2531
- gocognit
2632
- goconst
27-
- gocritic
28-
- gocyclo
29-
- godot
30-
- gofumpt
31-
- gosimple
32-
- govet
33-
- ineffassign
33+
# TODO: enable in gocritic in it's own PR
34+
# - gocritic
35+
- goprintffuncname
36+
- gosec
37+
- gosmopolitan
38+
- loggercheck
39+
- maintidx
40+
- makezero
3441
- misspell
3542
- mnd
43+
- musttag
44+
- nestif
3645
- nilerr
46+
- nilnesserr
3747
- nilnil
48+
- noctx
49+
- nolintlint
3850
- paralleltest
51+
- prealloc
52+
- predeclared
3953
- protogetter
40-
- revive
41-
- sloglint
42-
- staticcheck
54+
- reassign
55+
- recvcheck
56+
- rowserrcheck
57+
- spancheck
58+
- sqlclosecheck
59+
- testifylint
60+
- thelper
61+
- unconvert
62+
- unparam
63+
- usestdlibvars
64+
- usetesting
65+
- varnamelen
66+
- whitespace
67+
- zerologlint
68+
- govet
69+
- ineffassign
4370
- unused
44-
- wsl
45-
46-
linters-settings:
47-
paralleltest:
48-
ignore-missing: false
49-
50-
sloglint:
51-
kv-only: true
52-
key-naming-case: snake
53-
54-
gci:
55-
sections:
56-
- standard
57-
- default
58-
- blank
59-
- dot
60-
- prefix(github.com/linode)
71+
settings:
72+
exhaustive:
73+
default-signifies-exhaustive: true
74+
cyclop:
75+
max-complexity: 15
76+
depguard:
77+
rules:
78+
main:
79+
files:
80+
- $all
81+
- '!$test'
82+
deny:
83+
- pkg: reflect
84+
desc: Reflection is never clear.
85+
- pkg: gob
86+
desc: Please convert types manually
87+
dupl:
88+
threshold: 100
89+
errcheck:
90+
check-type-assertions: true
91+
check-blank: true
92+
goconst:
93+
min-len: 3
94+
min-occurrences: 5
95+
gocritic:
96+
enabled-tags:
97+
- diagnostic
98+
- experimental
99+
- opinionated
100+
- performance
101+
- style
102+
settings:
103+
captLocal:
104+
paramsOnly: true
105+
rangeValCopy:
106+
sizeThreshold: 32
107+
lll:
108+
tab-width: 1
109+
nolintlint:
110+
require-explanation: true
111+
require-specific: true
112+
prealloc:
113+
simple: true
114+
range-loops: true
115+
for-loops: false
116+
unparam:
117+
check-exported: false
118+
varnamelen:
119+
min-name-length: 2
120+
exclusions:
121+
generated: lax
122+
rules:
123+
- linters:
124+
- copyloopvar
125+
- dupl
126+
- errcheck
127+
- gocyclo
128+
- gosec
129+
- maintidx
130+
- unparam
131+
- containedctx
132+
path: _test\.go
133+
- linters:
134+
- dupl
135+
path: _conversion\.go
136+
- linters:
137+
- gocritic
138+
path: _test\.go
139+
text: (unnamedResult|exitAfterDefer)
140+
- linters:
141+
- gocritic
142+
text: '(hugeParam|rangeValCopy):'
143+
- linters:
144+
- staticcheck
145+
text: 'SA3000:'
146+
- linters:
147+
- gosec
148+
text: 'G101:'
149+
- linters:
150+
- gosec
151+
text: 'G104:'
152+
paths:
153+
- cmd/main.go
154+
- third_party$
155+
- builtin$
156+
- examples$
157+
issues:
158+
max-issues-per-linter: 0
159+
max-same-issues: 0
160+
new: false
161+
formatters:
162+
enable:
163+
- gci
164+
- gofmt
165+
- goimports
166+
settings:
167+
gci:
168+
sections:
169+
- standard
170+
- default
171+
- blank
172+
- dot
173+
- prefix(github.com/linode/linode-cosi-driver)
174+
gofmt:
175+
simplify: true
176+
goimports:
177+
local-prefixes:
178+
- github.com/linode/linode-cosi-driver
179+
exclusions:
180+
generated: lax
181+
paths:
182+
- cmd/main.go
183+
- third_party$
184+
- builtin$
185+
- examples$

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ TILT ?= $(LOCALBIN)/tilt
221221
## Tool Versions
222222
CHAINSAW_VERSION ?= v0.2.12
223223
CTLPTL_VERSION ?= v0.8.39
224-
GOLANGCI_LINT_VERSION ?= v1.64.5
224+
GOLANGCI_LINT_VERSION ?= v2.7.1
225225
HELM_VERSION ?= v3.17.1
226226
HELM_DOCS_VERSION ?= v1.14.2
227227
HELM_VALUES_SCHEMA_JSON_VERSION ?= v1.7.0
@@ -242,7 +242,7 @@ $(CTLPTL)-$(CTLPTL_VERSION): $(LOCALBIN)
242242
.PHONY: golangci-lint
243243
golangci-lint: $(GOLANGCI_LINT)-$(GOLANGCI_LINT_VERSION) ## Download golangci-lint locally if necessary.
244244
$(GOLANGCI_LINT)-$(GOLANGCI_LINT_VERSION): $(LOCALBIN)
245-
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
245+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
246246

247247
.PHONY: helm
248248
helm: $(HELM)-$(HELM_VERSION) ## Download helm locally if necessary.

cmd/linode-cosi-driver/main.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"log/slog"
22+
"net"
2223
"net/url"
2324
"os"
2425
"os/signal"
@@ -32,7 +33,6 @@ import (
3233
"google.golang.org/grpc"
3334
cosi "sigs.k8s.io/container-object-storage-interface-spec"
3435

35-
"github.com/linode/linode-cosi-driver/pkg/endpoint"
3636
"github.com/linode/linode-cosi-driver/pkg/envflag"
3737
grpchandlers "github.com/linode/linode-cosi-driver/pkg/grpc"
3838
"github.com/linode/linode-cosi-driver/pkg/linodeclient"
@@ -89,6 +89,7 @@ type mainOptions struct {
8989
s3SecretKey string
9090
}
9191

92+
//nolint:cyclop //TODO: reduce complexity
9293
func run(ctx context.Context, log *slog.Logger, opts mainOptions) error {
9394
_, err := maxprocs.Set(maxprocs.Logger(logutils.ForMaxprocs(log.Handler())))
9495
if err != nil {
@@ -171,15 +172,19 @@ func run(ctx context.Context, log *slog.Logger, opts mainOptions) error {
171172
return fmt.Errorf("unable to parse COSI endpoint: %w", err)
172173
}
173174

174-
// create the endpoint handler
175-
ep := endpoint.New(endpointURL)
176-
defer ep.Close()
177-
178-
lis, err := ep.Listener(ctx)
175+
// create the unix socket listener directly
176+
if endpointURL.Scheme != "unix" {
177+
return fmt.Errorf("only unix scheme is supported for endpoint")
178+
}
179+
listenConfig := net.ListenConfig{}
180+
lis, err := listenConfig.Listen(ctx, "unix", endpointURL.Path)
179181
if err != nil {
180-
return fmt.Errorf("unable to create new listener: %w", err)
182+
return fmt.Errorf("unable to create unix listener: %w", err)
181183
}
182-
defer lis.Close()
184+
defer func() {
185+
lis.Close() //nolint:errcheck //ignore close error
186+
os.Remove(endpointURL.Path) //nolint:errcheck //ignore close error
187+
}()
183188

184189
// create the grpcServer
185190
srv, err := grpcServer(ctx, log, idSrv, prvSrv)
@@ -232,7 +237,7 @@ func grpcServer(ctx context.Context,
232237
// shutdown handles shutdown with grace period consideration.
233238
func shutdown(ctx context.Context,
234239
wg *sync.WaitGroup,
235-
g *grpc.Server,
240+
server *grpc.Server,
236241
) {
237242
<-ctx.Done()
238243
defer wg.Done()
@@ -243,22 +248,22 @@ func shutdown(ctx context.Context,
243248
dctx, stop := context.WithTimeout(context.Background(), gracePeriod)
244249
defer stop()
245250

246-
c := make(chan struct{})
251+
channel := make(chan struct{})
247252

248-
if g != nil {
253+
if server != nil {
249254
go func() {
250-
g.GracefulStop()
251-
c <- struct{}{}
255+
server.GracefulStop()
256+
channel <- struct{}{}
252257
}()
253258

254259
for {
255260
select {
256261
case <-dctx.Done():
257262
slog.Warn("Forcing shutdown")
258-
g.Stop()
263+
server.Stop()
259264

260265
return
261-
case <-c:
266+
case <-channel:
262267
return
263268
}
264269
}

cmd/linode-cosi-driver/main_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ import (
1919
"errors"
2020
"io"
2121
"log/slog"
22-
"os"
2322
"testing"
2423
"time"
25-
26-
"github.com/linode/linode-cosi-driver/pkg/testutils"
2724
)
2825

2926
func TestRun(t *testing.T) {
30-
t.Parallel()
27+
t.Setenv("LINODE_TOKEN", "test")
3128

3229
for _, tc := range []struct {
3330
testName string // required
@@ -46,8 +43,6 @@ func TestRun(t *testing.T) {
4643
t.Run(tc.testName, func(t *testing.T) {
4744
t.Parallel()
4845

49-
os.Setenv("LINODE_TOKEN", "test")
50-
5146
noopLog := slog.New(slog.NewTextHandler(
5247
io.Discard,
5348
&slog.HandlerOptions{Level: slog.LevelError},
@@ -67,8 +62,7 @@ func TestRun(t *testing.T) {
6762
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
6863
defer cancel()
6964

70-
tmp := testutils.MustMkdirTemp()
71-
defer os.RemoveAll(tmp)
65+
tmp := t.TempDir()
7266

7367
defaultOpts.cosiEndpoint = "unix://" + tmp + defaultOpts.cosiEndpoint
7468

0 commit comments

Comments
 (0)