Skip to content

Commit 5528d67

Browse files
authored
Merge pull request #1853 from cortexproject/update-lint
Move to golangci-lint
2 parents f01fd63 + 62d1042 commit 5528d67

File tree

14 files changed

+43
-307
lines changed

14 files changed

+43
-307
lines changed

.circleci/config.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: 2.1
33
# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
44
defaults: &defaults
55
docker:
6-
- image: cortexproject/build-image:master-d74af5958
6+
- image: quay.io/cortexproject/build-image:update-lint-ae47f740a
77
working_directory: /go/src/github.com/cortexproject/cortex
88

99
workflows:
@@ -68,6 +68,10 @@ jobs:
6868
- run:
6969
name: Lint
7070
command: make BUILD_IN_CONTAINER=false lint
71+
# fails to run everything first time - see https://github.com/golangci/golangci-lint/issues/866
72+
- run:
73+
name: Lint again
74+
command: make BUILD_IN_CONTAINER=false lint
7175
- run:
7276
name: Check vendor directory is consistent.
7377
command: make BUILD_IN_CONTAINER=false mod-check

.errcheck-exclude

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
io/ioutil.WriteFile
2+
io/ioutil.ReadFile
3+
(github.com/go-kit/kit/log.Logger).Log

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output:
2+
format: line-number
3+
4+
linters-settings:
5+
errcheck:
6+
# path to a file containing a list of functions to exclude from checking
7+
# see https://github.com/kisielk/errcheck#excluding-functions for details
8+
exclude: ./.errcheck-exclude

Makefile

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,24 @@ GO_FLAGS := -ldflags "-extldflags \"-static\" -s -w" -tags netgo
7878

7979
ifeq ($(BUILD_IN_CONTAINER),true)
8080

81+
GOVOLUMES= -v $(shell pwd)/.cache:/go/cache:delegated \
82+
-v $(shell pwd)/.pkg:/go/pkg:delegated \
83+
-v $(shell pwd):/go/src/github.com/cortexproject/cortex:delegated
84+
8185
exes $(EXES) protos $(PROTO_GOS) lint test shell mod-check check-protos web-build web-pre web-deploy: build-image/$(UPTODATE)
8286
@mkdir -p $(shell pwd)/.pkg
8387
@mkdir -p $(shell pwd)/.cache
8488
@echo
8589
@echo ">>>> Entering build container: $@"
86-
@$(SUDO) time docker run $(RM) $(TTY) -i \
87-
-v $(shell pwd)/.cache:/go/cache \
88-
-v $(shell pwd)/.pkg:/go/pkg \
89-
-v $(shell pwd):/go/src/github.com/cortexproject/cortex \
90-
$(BUILD_IMAGE) $@;
90+
@$(SUDO) time docker run $(RM) $(TTY) -i $(GOVOLUMES) $(BUILD_IMAGE) $@;
9191

9292
configs-integration-test: build-image/$(UPTODATE)
9393
@mkdir -p $(shell pwd)/.pkg
9494
@mkdir -p $(shell pwd)/.cache
9595
@DB_CONTAINER="$$(docker run -d -e 'POSTGRES_DB=configs_test' postgres:9.6)"; \
9696
echo ; \
9797
echo ">>>> Entering build container: $@"; \
98-
$(SUDO) docker run $(RM) $(TTY) -i \
99-
-v $(shell pwd)/.cache:/go/cache \
100-
-v $(shell pwd)/.pkg:/go/pkg \
101-
-v $(shell pwd):/go/src/github.com/cortexproject/cortex \
98+
$(SUDO) docker run $(RM) $(TTY) -i $(GOVOLUMES) \
10299
-v $(shell pwd)/cmd/cortex/migrations:/migrations \
103100
--workdir /go/src/github.com/cortexproject/cortex \
104101
--link "$$DB_CONTAINER":configs-db.cortex.local \
@@ -121,11 +118,8 @@ protos: $(PROTO_GOS)
121118
protoc -I $(GOPATH)/src:./vendor:./$(@D) --gogoslick_out=plugins=grpc,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,:./$(@D) ./$(patsubst %.pb.go,%.proto,$@)
122119

123120
lint:
124-
./tools/lint -notestpackage -novet -ignorespelling queriers -ignorespelling Queriers .
125-
126-
# -stdmethods=false disables checks for non-standard signatures for methods with familiar names.
127-
# This is needed because the Prometheus storage interface requires a non-standard Seek() method.
128-
go vet -stdmethods=false ./pkg/...
121+
misspell -error docs
122+
golangci-lint run --new-from-rev ed7c302fd968 --build-tags netgo --timeout=5m --enable golint --enable misspell --enable gofmt
129123

130124
test:
131125
./tools/test -netgo

build-image/Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ RUN apt-get update && apt-get install -y curl python-requests python-yaml file j
44
RUN curl -sL https://deb.nodesource.com/setup_6.x | sh -
55
RUN apt-get install -y nodejs npm && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
66
RUN npm install -g postcss-cli autoprefixer
7-
RUN go clean -i net && \
8-
go install -tags netgo std && \
9-
go install -race -tags netgo std
107
ENV HUGO_VERSION=v0.59.1
118
RUN git clone https://github.com/gohugoio/hugo.git --branch ${HUGO_VERSION} --depth 1 && \
129
cd hugo && go install --tags extended && cd ../ && \
@@ -15,17 +12,13 @@ RUN curl -fsSLo shfmt https://github.com/mvdan/sh/releases/download/v1.3.0/shfmt
1512
echo "b1925c2c405458811f0c227266402cf1868b4de529f114722c2e3a5af4ac7bb2 shfmt" | sha256sum -c && \
1613
chmod +x shfmt && \
1714
mv shfmt /usr/bin
15+
RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /usr/bin v1.21.0
1816
RUN GO111MODULE=on go get -tags netgo \
19-
github.com/fzipp/gocyclo \
20-
golang.org/x/lint/golint \
21-
github.com/kisielk/[email protected] \
2217
github.com/client9/misspell/cmd/[email protected] \
2318
github.com/golang/protobuf/[email protected] \
2419
github.com/gogo/protobuf/[email protected] \
2520
github.com/gogo/protobuf/[email protected] && \
2621
rm -rf /go/pkg /go/src
27-
RUN curl -Ls https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o $GOPATH/bin/dep && \
28-
chmod +x $GOPATH/bin/dep
2922

3023
ENV NODE_PATH=/usr/lib/node_modules
3124
COPY build.sh /

pkg/configs/configs.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,8 @@ func (c RulesConfig) parseV2Formatted() (map[string]rulefmt.RuleGroups, error) {
203203

204204
for fn, content := range c.Files {
205205
rgs, errs := rulefmt.Parse([]byte(content))
206-
if errs != nil {
207-
for _, err := range errs {
208-
return nil, err
209-
}
206+
for _, err := range errs { // return just the first error, if any
207+
return nil, err
210208
}
211209
ruleMap[fn] = *rgs
212210

pkg/cortex/modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (t *Cortex) initQueryFrontend(cfg *Config) (err error) {
344344
func (t *Cortex) stopQueryFrontend() (err error) {
345345
t.frontend.Close()
346346
if t.cache != nil {
347-
t.cache.Stop()
347+
_ = t.cache.Stop()
348348
t.cache = nil
349349
}
350350
return

pkg/ingester/ingester_v2.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,14 @@ func (i *Ingester) getOrCreateTSDB(userID string, force bool) (*tsdb.DB, error)
400400
i.done.Add(1)
401401
go func() {
402402
defer i.done.Done()
403-
runutil.Repeat(i.cfg.TSDBConfig.ShipInterval, i.quit, func() error {
403+
if err := runutil.Repeat(i.cfg.TSDBConfig.ShipInterval, i.quit, func() error {
404404
if uploaded, err := s.Sync(context.Background()); err != nil {
405405
level.Warn(util.Logger).Log("err", err, "uploaded", uploaded)
406406
}
407407
return nil
408-
})
408+
}); err != nil {
409+
level.Warn(util.Logger).Log("err", err)
410+
}
409411
}()
410412

411413
i.TSDBState.dbs[userID] = db

pkg/ring/lifecycler_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func TestCheckReady(t *testing.T) {
340340
}
341341

342342
type noopFlushTransferer struct {
343-
lifecycler *Lifecycler
344343
}
345344

346345
func (f *noopFlushTransferer) StopIncomingRequests() {}

pkg/ring/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (i *IngesterDesc) IsHealthy(op Operation, heartbeatTimeout time.Duration) b
171171
healthy = true
172172
}
173173

174-
return healthy && time.Now().Sub(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
174+
return healthy && time.Since(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
175175
}
176176

177177
// Merge merges other ring into this one. Returns sub-ring that represents the change,

pkg/ruler/rules/store_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func Test_ConfigRuleStoreError(t *testing.T) {
3333
}
3434

3535
store := NewConfigRuleStore(mock)
36-
_, err := store.ListAllRuleGroups(nil)
36+
_, err := store.ListAllRuleGroups(context.Background())
3737

3838
assert.Equal(t, mock.err, err, "Unexpected error returned")
3939
}
@@ -52,7 +52,7 @@ func Test_ConfigRuleStoreReturn(t *testing.T) {
5252
}
5353

5454
store := NewConfigRuleStore(mock)
55-
rules, _ := store.ListAllRuleGroups(nil)
55+
rules, _ := store.ListAllRuleGroups(context.Background())
5656

5757
assert.Equal(t, 1, len(rules["user"]))
5858
assert.Equal(t, id, store.since)
@@ -71,15 +71,15 @@ func Test_ConfigRuleStoreDelete(t *testing.T) {
7171
}
7272

7373
store := NewConfigRuleStore(mock)
74-
store.ListAllRuleGroups(nil)
74+
_, _ = store.ListAllRuleGroups(context.Background())
7575

7676
mock.cfgs["user"] = configs.VersionedRulesConfig{
7777
ID: 1,
7878
Config: configs.RulesConfig{},
7979
DeletedAt: time.Unix(0, 1),
8080
}
8181

82-
rules, _ := store.ListAllRuleGroups(nil)
82+
rules, _ := store.ListAllRuleGroups(context.Background())
8383

8484
assert.Equal(t, 0, len(rules["user"]))
8585
}
@@ -97,7 +97,7 @@ func Test_ConfigRuleStoreAppend(t *testing.T) {
9797
}
9898

9999
store := NewConfigRuleStore(mock)
100-
store.ListAllRuleGroups(nil)
100+
_, _ = store.ListAllRuleGroups(context.Background())
101101

102102
delete(mock.cfgs, "user")
103103
mock.cfgs["user2"] = configs.VersionedRulesConfig{
@@ -106,7 +106,7 @@ func Test_ConfigRuleStoreAppend(t *testing.T) {
106106
DeletedAt: zeroTime,
107107
}
108108

109-
rules, _ := store.ListAllRuleGroups(nil)
109+
rules, _ := store.ListAllRuleGroups(context.Background())
110110

111111
assert.Equal(t, 2, len(rules))
112112
}
@@ -134,7 +134,7 @@ func Test_ConfigRuleStoreSinceSet(t *testing.T) {
134134
}
135135

136136
store := NewConfigRuleStore(mock)
137-
store.ListAllRuleGroups(nil)
137+
_, _ = store.ListAllRuleGroups(context.Background())
138138
assert.Equal(t, configs.ID(100), store.since)
139139

140140
delete(mock.cfgs, "user")
@@ -145,7 +145,7 @@ func Test_ConfigRuleStoreSinceSet(t *testing.T) {
145145
DeletedAt: zeroTime,
146146
}
147147

148-
store.ListAllRuleGroups(nil)
148+
_, _ = store.ListAllRuleGroups(context.Background())
149149
assert.Equal(t, configs.ID(100), store.since)
150150

151151
mock.cfgs["user2"] = configs.VersionedRulesConfig{
@@ -154,7 +154,7 @@ func Test_ConfigRuleStoreSinceSet(t *testing.T) {
154154
DeletedAt: zeroTime,
155155
}
156156

157-
store.ListAllRuleGroups(nil)
157+
_, _ = store.ListAllRuleGroups(context.Background())
158158
assert.Equal(t, configs.ID(101), store.since)
159159
}
160160

pkg/util/limiter/rate_limiter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (l *RateLimiter) recheckTenantLimiter(now time.Time, tenantID string) *rate
9999
l.tenantsLock.Lock()
100100
defer l.tenantsLock.Unlock()
101101

102-
entry, _ := l.tenants[tenantID]
102+
entry := l.tenants[tenantID]
103103

104104
// We check again if the recheck period elapsed, cause it may
105105
// have already been rechecked in the meanwhile.

0 commit comments

Comments
 (0)