Skip to content

Move to golangci-lint #1853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
defaults: &defaults
docker:
- image: cortexproject/build-image:master-d74af5958
- image: quay.io/cortexproject/build-image:update-lint-ae47f740a
working_directory: /go/src/github.com/cortexproject/cortex

workflows:
Expand Down Expand Up @@ -68,6 +68,10 @@ jobs:
- run:
name: Lint
command: make BUILD_IN_CONTAINER=false lint
# fails to run everything first time - see https://github.com/golangci/golangci-lint/issues/866
- run:
name: Lint again
command: make BUILD_IN_CONTAINER=false lint
- run:
name: Check vendor directory is consistent.
command: make BUILD_IN_CONTAINER=false mod-check
Expand Down
3 changes: 3 additions & 0 deletions .errcheck-exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
io/ioutil.WriteFile
io/ioutil.ReadFile
(github.com/go-kit/kit/log.Logger).Log
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output:
format: line-number

linters-settings:
errcheck:
# path to a file containing a list of functions to exclude from checking
# see https://github.com/kisielk/errcheck#excluding-functions for details
exclude: ./.errcheck-exclude
22 changes: 8 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,24 @@ GO_FLAGS := -ldflags "-extldflags \"-static\" -s -w" -tags netgo

ifeq ($(BUILD_IN_CONTAINER),true)

GOVOLUMES= -v $(shell pwd)/.cache:/go/cache:delegated \
-v $(shell pwd)/.pkg:/go/pkg:delegated \
-v $(shell pwd):/go/src/github.com/cortexproject/cortex:delegated

exes $(EXES) protos $(PROTO_GOS) lint test shell mod-check check-protos web-build web-pre web-deploy: build-image/$(UPTODATE)
@mkdir -p $(shell pwd)/.pkg
@mkdir -p $(shell pwd)/.cache
@echo
@echo ">>>> Entering build container: $@"
@$(SUDO) time docker run $(RM) $(TTY) -i \
-v $(shell pwd)/.cache:/go/cache \
-v $(shell pwd)/.pkg:/go/pkg \
-v $(shell pwd):/go/src/github.com/cortexproject/cortex \
$(BUILD_IMAGE) $@;
@$(SUDO) time docker run $(RM) $(TTY) -i $(GOVOLUMES) $(BUILD_IMAGE) $@;

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

lint:
./tools/lint -notestpackage -novet -ignorespelling queriers -ignorespelling Queriers .

# -stdmethods=false disables checks for non-standard signatures for methods with familiar names.
# This is needed because the Prometheus storage interface requires a non-standard Seek() method.
go vet -stdmethods=false ./pkg/...
misspell -error docs
golangci-lint run --new-from-rev ed7c302fd968 --build-tags netgo --timeout=5m --enable golint --enable misspell --enable gofmt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is particularly slow when running on OSX (with Docker for Mac) due to the shared volumes performances. Do you see any problem adding :delegated to the 3 related volumes, switching

	@$(SUDO) time docker run $(RM) $(TTY) -i \
		-v $(shell pwd)/.cache:/go/cache \
		-v $(shell pwd)/.pkg:/go/pkg \
		-v $(shell pwd):/go/src/github.com/cortexproject/cortex \
		$(BUILD_IMAGE) $@;

to

	@$(SUDO) time docker run $(RM) $(TTY) -i \
		-v $(shell pwd)/.cache:/go/cache:delegated \
		-v $(shell pwd)/.pkg:/go/pkg:delegated \
		-v $(shell pwd):/go/src/github.com/cortexproject/cortex:delegated \
		$(BUILD_IMAGE) $@;

In my case, the execution time difference is:

  • Current: 8m8.369s
  • With delegated: 1m38.258s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does :delegated do?
Would we want it on all similar lines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does :delegated do?

The :delegated tells Docker for Mac that the container’s view is authoritative (permit delays before updates on the container appear in the host). Details here: https://docs.docker.com/docker-for-mac/osxfs-caching/

I don't have a Linux box right now to play with, but it should just be ignored on Linux.

Would we want it on all similar lines?

I would say yes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered adding :delegated to volume mounts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have done this now.


test:
./tools/test -netgo
Expand Down
9 changes: 1 addition & 8 deletions build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ RUN apt-get update && apt-get install -y curl python-requests python-yaml file j
RUN curl -sL https://deb.nodesource.com/setup_6.x | sh -
RUN apt-get install -y nodejs npm && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN npm install -g postcss-cli autoprefixer
RUN go clean -i net && \
go install -tags netgo std && \
go install -race -tags netgo std
ENV HUGO_VERSION=v0.59.1
RUN git clone https://github.com/gohugoio/hugo.git --branch ${HUGO_VERSION} --depth 1 && \
cd hugo && go install --tags extended && cd ../ && \
Expand All @@ -15,17 +12,13 @@ RUN curl -fsSLo shfmt https://github.com/mvdan/sh/releases/download/v1.3.0/shfmt
echo "b1925c2c405458811f0c227266402cf1868b4de529f114722c2e3a5af4ac7bb2 shfmt" | sha256sum -c && \
chmod +x shfmt && \
mv shfmt /usr/bin
RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /usr/bin v1.21.0
RUN GO111MODULE=on go get -tags netgo \
github.com/fzipp/gocyclo \
golang.org/x/lint/golint \
github.com/kisielk/[email protected] \
github.com/client9/misspell/cmd/[email protected] \
github.com/golang/protobuf/[email protected] \
github.com/gogo/protobuf/[email protected] \
github.com/gogo/protobuf/[email protected] && \
rm -rf /go/pkg /go/src
RUN curl -Ls https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o $GOPATH/bin/dep && \
chmod +x $GOPATH/bin/dep

ENV NODE_PATH=/usr/lib/node_modules
COPY build.sh /
Expand Down
6 changes: 2 additions & 4 deletions pkg/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ func (c RulesConfig) parseV2Formatted() (map[string]rulefmt.RuleGroups, error) {

for fn, content := range c.Files {
rgs, errs := rulefmt.Parse([]byte(content))
if errs != nil {
for _, err := range errs {
return nil, err
}
for _, err := range errs { // return just the first error, if any
return nil, err
}
ruleMap[fn] = *rgs

Expand Down
2 changes: 1 addition & 1 deletion pkg/cortex/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (t *Cortex) initQueryFrontend(cfg *Config) (err error) {
func (t *Cortex) stopQueryFrontend() (err error) {
t.frontend.Close()
if t.cache != nil {
t.cache.Stop()
_ = t.cache.Stop()
t.cache = nil
}
return
Expand Down
6 changes: 4 additions & 2 deletions pkg/ingester/ingester_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,14 @@ func (i *Ingester) getOrCreateTSDB(userID string, force bool) (*tsdb.DB, error)
i.done.Add(1)
go func() {
defer i.done.Done()
runutil.Repeat(i.cfg.TSDBConfig.ShipInterval, i.quit, func() error {
if err := runutil.Repeat(i.cfg.TSDBConfig.ShipInterval, i.quit, func() error {
if uploaded, err := s.Sync(context.Background()); err != nil {
level.Warn(util.Logger).Log("err", err, "uploaded", uploaded)
}
return nil
})
}); err != nil {
level.Warn(util.Logger).Log("err", err)
}
}()

i.TSDBState.dbs[userID] = db
Expand Down
1 change: 0 additions & 1 deletion pkg/ring/lifecycler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ func TestCheckReady(t *testing.T) {
}

type noopFlushTransferer struct {
lifecycler *Lifecycler
}

func (f *noopFlushTransferer) StopIncomingRequests() {}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ring/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (i *IngesterDesc) IsHealthy(op Operation, heartbeatTimeout time.Duration) b
healthy = true
}

return healthy && time.Now().Sub(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
return healthy && time.Since(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
}

// Merge merges other ring into this one. Returns sub-ring that represents the change,
Expand Down
18 changes: 9 additions & 9 deletions pkg/ruler/rules/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_ConfigRuleStoreError(t *testing.T) {
}

store := NewConfigRuleStore(mock)
_, err := store.ListAllRuleGroups(nil)
_, err := store.ListAllRuleGroups(context.Background())

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

store := NewConfigRuleStore(mock)
rules, _ := store.ListAllRuleGroups(nil)
rules, _ := store.ListAllRuleGroups(context.Background())

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

store := NewConfigRuleStore(mock)
store.ListAllRuleGroups(nil)
_, _ = store.ListAllRuleGroups(context.Background())

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

rules, _ := store.ListAllRuleGroups(nil)
rules, _ := store.ListAllRuleGroups(context.Background())

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

store := NewConfigRuleStore(mock)
store.ListAllRuleGroups(nil)
_, _ = store.ListAllRuleGroups(context.Background())

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

rules, _ := store.ListAllRuleGroups(nil)
rules, _ := store.ListAllRuleGroups(context.Background())

assert.Equal(t, 2, len(rules))
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func Test_ConfigRuleStoreSinceSet(t *testing.T) {
}

store := NewConfigRuleStore(mock)
store.ListAllRuleGroups(nil)
_, _ = store.ListAllRuleGroups(context.Background())
assert.Equal(t, configs.ID(100), store.since)

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

store.ListAllRuleGroups(nil)
_, _ = store.ListAllRuleGroups(context.Background())
assert.Equal(t, configs.ID(100), store.since)

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

store.ListAllRuleGroups(nil)
_, _ = store.ListAllRuleGroups(context.Background())
assert.Equal(t, configs.ID(101), store.since)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/limiter/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (l *RateLimiter) recheckTenantLimiter(now time.Time, tenantID string) *rate
l.tenantsLock.Lock()
defer l.tenantsLock.Unlock()

entry, _ := l.tenants[tenantID]
entry := l.tenants[tenantID]

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