Skip to content

Commit b36394b

Browse files
committed
Fix local imports, add appropriate linter
1. Modify Makefile's format target to format imports in three groups: - standard library packages; - third-party packages; - local packages (i.e. ones with the `github.com/google/cadvisor` prefix). Note goimports does the same job as gofmt, plus imports sorting. It can't do what gofmt -s does, but it was not done before (although this is checked according to .golangci.yml), so let's leave it as is. Note we don't have to use $(pkgs) and $(cmd_pkgs) in Makefile's format target, as goimports works recursively given the current directory, and it also skips vendor dir. 2. Add goimports linter with proper configuration to check imports formatting in CI. Check that the linter complains. 3. Actually implements these changes by running "make format". Check that the linter no longer complains. 4. Fix presubmit target to make sure it checks the new formatting rules. Instead of modifying build/check_gofmt.sh, let's use golangci-lint which already does this check (and more). While at it, let's remove vet target, as lint (golangci-lint) already calls it. Now, since make presumbit is already there in test job, the whole lint job is superseded by it, so let's remove it. While doing it, make sure we use the code from lint job to run make presubmit, with the following two exceptions: - "set -e" is not required, as it is set by GHA CI; - GOFLAGS="$GO_FLAGS" is not required, as it is done by Makefile. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 300242c commit b36394b

Some content is hidden

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

65 files changed

+129
-129
lines changed

.github/workflows/test.yml

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
name: Test
22
on: [push, pull_request]
33
jobs:
4-
lint:
5-
strategy:
6-
matrix:
7-
environment-variables: [build/config/plain.sh, build/config/libpfm4.sh, build/config/libipmctl.sh]
8-
runs-on: ubuntu-20.04
9-
timeout-minutes: 10
10-
steps:
11-
- name: Install Go
12-
uses: actions/setup-go@v2
13-
with:
14-
go-version: 1.17
15-
- name: Checkout code
16-
uses: actions/checkout@v2
17-
- name: Run golangci-lint
18-
run: |
19-
set -e
20-
source ${{ matrix.environment-variables }}
21-
if [[ "${BUILD_PACKAGES}" != "" ]]; then sudo apt-get update; sudo apt-get install ${BUILD_PACKAGES}; fi
22-
make -e lint
234
test:
245
strategy:
256
matrix:
@@ -36,7 +17,10 @@ jobs:
3617
- name: Checkout code
3718
uses: actions/checkout@v2
3819
- name: Run presubmit checks
39-
run: GOFLAGS="$GO_FLAGS" make presubmit
20+
run: |
21+
source ${{ matrix.environment-variables }}
22+
if [[ "${BUILD_PACKAGES}" != "" ]]; then sudo apt-get update; sudo apt-get install ${BUILD_PACKAGES}; fi
23+
make -e presubmit
4024
- name: Run tests
4125
env:
4226
GOLANG_VERSION: ${{ matrix.go-versions }}

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ run:
77
min-confidence: 0
88
gofmt:
99
simplify: true
10+
goimports:
11+
local-prefixes: github.com/google/cadvisor
1012
linters:
1113
disable-all: true
1214
enable:
@@ -22,6 +24,7 @@ linters:
2224
- typecheck
2325
- golint
2426
- gofmt
27+
- goimports
2528
issues:
2629
max-issues-per-linter: 0
2730
max-same-issues: 0

Makefile

+4-11
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,8 @@ tidy:
6262

6363
format:
6464
@echo ">> formatting code"
65-
@$(GO) fmt $(pkgs)
66-
@cd cmd && $(GO) fmt $(cmd_pkgs)
67-
68-
vet:
69-
@echo ">> vetting code"
70-
@$(GO) vet $(pkgs)
71-
@cd cmd && $(GO) vet $(cmd_pkgs)
65+
@# goimports is a superset of gofmt.
66+
@goimports -w -local github.com/google/cadvisor .
7267

7368
build: assets
7469
@echo ">> building binaries"
@@ -88,9 +83,7 @@ docker-%:
8883
docker-build:
8984
@docker run --rm -w /go/src/github.com/google/cadvisor -v ${PWD}:/go/src/github.com/google/cadvisor golang:1.17 make build
9085

91-
presubmit: vet
92-
@echo ">> checking go formatting"
93-
@./build/check_gofmt.sh
86+
presubmit: lint
9487
@echo ">> checking go mod tidy"
9588
@./build/check_gotidy.sh
9689
@echo ">> checking file boilerplate"
@@ -108,4 +101,4 @@ lint:
108101
clean:
109102
@rm -f *.test cadvisor
110103

111-
.PHONY: all build docker format release test test-integration vet lint presubmit tidy
104+
.PHONY: all build docker format release test test-integration lint presubmit tidy

accelerators/nvidia_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
package accelerators
1515

1616
import (
17-
"github.com/google/cadvisor/stats"
1817
"io/ioutil"
1918
"os"
2019
"path/filepath"
2120
"testing"
2221

22+
"github.com/google/cadvisor/stats"
23+
2324
"github.com/mindprince/gonvml"
2425
"github.com/stretchr/testify/assert"
2526
)

build/check_gofmt.sh

-27
This file was deleted.

client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"path"
2929
"strings"
3030

31-
"github.com/google/cadvisor/info/v1"
31+
v1 "github.com/google/cadvisor/info/v1"
3232

3333
"k8s.io/klog/v2"
3434
)

client/v2/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"strings"
2828

2929
v1 "github.com/google/cadvisor/info/v1"
30-
"github.com/google/cadvisor/info/v2"
30+
v2 "github.com/google/cadvisor/info/v2"
3131
)
3232

3333
// Client represents the base URL for a cAdvisor client.

client/v2/client_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/google/cadvisor/info/v1"
28-
"github.com/google/cadvisor/info/v2"
2927
"github.com/stretchr/testify/assert"
28+
29+
v1 "github.com/google/cadvisor/info/v1"
30+
v2 "github.com/google/cadvisor/info/v2"
3031
)
3132

3233
func cadvisorTestClient(path string, expectedPostObj *v1.ContainerInfoRequest, replyObj interface{}, t *testing.T) (*Client, *httptest.Server, error) {

cmd/cadvisor_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
"flag"
1919
"testing"
2020

21-
"github.com/google/cadvisor/container"
2221
"github.com/stretchr/testify/assert"
22+
23+
"github.com/google/cadvisor/container"
2324
)
2425

2526
func TestTcpMetricsAreDisabledByDefault(t *testing.T) {

cmd/internal/container/mesos/client.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ package mesos
1616

1717
import (
1818
"fmt"
19+
"net/url"
20+
"sync"
21+
1922
"github.com/Rican7/retry"
2023
"github.com/Rican7/retry/strategy"
2124
mesos "github.com/mesos/mesos-go/api/v1/lib"
@@ -24,8 +27,6 @@ import (
2427
mclient "github.com/mesos/mesos-go/api/v1/lib/client"
2528
"github.com/mesos/mesos-go/api/v1/lib/encoding/codecs"
2629
"github.com/mesos/mesos-go/api/v1/lib/httpcli"
27-
"net/url"
28-
"sync"
2930
)
3031

3132
const (

cmd/internal/container/mesos/factory.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ import (
2222
"strings"
2323
"time"
2424

25+
"k8s.io/klog/v2"
26+
2527
"github.com/google/cadvisor/container"
2628
"github.com/google/cadvisor/container/libcontainer"
2729
"github.com/google/cadvisor/fs"
2830
info "github.com/google/cadvisor/info/v1"
2931
"github.com/google/cadvisor/watcher"
30-
"k8s.io/klog/v2"
3132
)
3233

3334
var MesosAgentAddress = flag.String("mesos_agent", "127.0.0.1:5051", "Mesos agent address")

cmd/internal/container/mesos/factory_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
package mesos
1616

1717
import (
18-
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
18+
"testing"
19+
1920
mesos "github.com/mesos/mesos-go/api/v1/lib"
2021
"github.com/stretchr/testify/assert"
21-
"testing"
22+
23+
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
2224
)
2325

2426
func TestIsContainerName(t *testing.T) {

cmd/internal/container/mesos/handler_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import (
1818
"fmt"
1919
"testing"
2020

21+
mesos "github.com/mesos/mesos-go/api/v1/lib"
22+
"github.com/stretchr/testify/assert"
23+
2124
"github.com/google/cadvisor/container"
2225
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
2326
"github.com/google/cadvisor/fs"
2427
info "github.com/google/cadvisor/info/v1"
25-
mesos "github.com/mesos/mesos-go/api/v1/lib"
26-
"github.com/stretchr/testify/assert"
2728
)
2829

2930
func PopulateContainer() *mContainer {

cmd/internal/container/mesos/install/install.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
package install
1717

1818
import (
19+
"k8s.io/klog/v2"
20+
1921
"github.com/google/cadvisor/cmd/internal/container/mesos"
2022
"github.com/google/cadvisor/container"
21-
"k8s.io/klog/v2"
2223
)
2324

2425
func init() {

cmd/internal/container/mesos/mesos_agent.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package mesos
1616

1717
import (
1818
"fmt"
19+
1920
mesos "github.com/mesos/mesos-go/api/v1/lib"
2021
"github.com/mesos/mesos-go/api/v1/lib/agent"
2122
)

cmd/internal/container/mesos/mesos_agent_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ package mesos
1616

1717
import (
1818
"fmt"
19+
"testing"
20+
1921
mesos "github.com/mesos/mesos-go/api/v1/lib"
2022
"github.com/mesos/mesos-go/api/v1/lib/agent"
2123
"github.com/stretchr/testify/assert"
22-
"testing"
2324
)
2425

2526
func PopulateFrameworks(fwID string) *agent.Response_GetFrameworks {

cmd/internal/storage/bigquery/client/example/example.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/SeanDolphin/bqschema"
23+
2324
"github.com/google/cadvisor/cmd/internal/storage/bigquery/client"
2425
)
2526

collector/collector_manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"strings"
2020
"time"
2121

22-
"github.com/google/cadvisor/info/v1"
22+
v1 "github.com/google/cadvisor/info/v1"
2323
)
2424

2525
const metricLabelPrefix = "io.cadvisor.metric."

collector/collector_manager_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"testing"
1919
"time"
2020

21-
"github.com/google/cadvisor/info/v1"
22-
2321
"github.com/stretchr/testify/assert"
22+
23+
v1 "github.com/google/cadvisor/info/v1"
2424
)
2525

2626
type fakeCollector struct {

collector/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"time"
1919

2020
"encoding/json"
21-
"github.com/google/cadvisor/info/v1"
21+
22+
v1 "github.com/google/cadvisor/info/v1"
2223
)
2324

2425
type Config struct {

collector/fakes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package collector
1717
import (
1818
"time"
1919

20-
"github.com/google/cadvisor/info/v1"
20+
v1 "github.com/google/cadvisor/info/v1"
2121
)
2222

2323
type FakeCollectorManager struct {

collector/generic_collector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/google/cadvisor/container"
28-
"github.com/google/cadvisor/info/v1"
28+
v1 "github.com/google/cadvisor/info/v1"
2929
)
3030

3131
type GenericCollector struct {

collector/generic_collector_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"os"
2323
"testing"
2424

25-
"github.com/google/cadvisor/info/v1"
25+
"github.com/stretchr/testify/assert"
2626

2727
containertest "github.com/google/cadvisor/container/testing"
28-
"github.com/stretchr/testify/assert"
28+
v1 "github.com/google/cadvisor/info/v1"
2929
)
3030

3131
func TestEmptyConfig(t *testing.T) {

collector/prometheus_collector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/prometheus/common/model"
2929

3030
"github.com/google/cadvisor/container"
31-
"github.com/google/cadvisor/info/v1"
31+
v1 "github.com/google/cadvisor/info/v1"
3232
)
3333

3434
type PrometheusCollector struct {

collector/prometheus_collector_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"net/http/httptest"
2222
"testing"
2323

24-
"github.com/google/cadvisor/info/v1"
25-
26-
containertest "github.com/google/cadvisor/container/testing"
2724
"github.com/stretchr/testify/assert"
2825
"github.com/stretchr/testify/require"
26+
27+
containertest "github.com/google/cadvisor/container/testing"
28+
v1 "github.com/google/cadvisor/info/v1"
2929
)
3030

3131
func TestPrometheus(t *testing.T) {

collector/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package collector
1717
import (
1818
"time"
1919

20-
"github.com/google/cadvisor/info/v1"
20+
v1 "github.com/google/cadvisor/info/v1"
2121
)
2222

2323
// TODO(vmarmol): Export to a custom metrics type when that is available.

container/common/helpers.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
"strings"
2525
"time"
2626

27-
"github.com/google/cadvisor/container"
28-
info "github.com/google/cadvisor/info/v1"
29-
"github.com/google/cadvisor/utils"
3027
"github.com/karrick/godirwalk"
3128
"github.com/opencontainers/runc/libcontainer/cgroups"
3229
"github.com/pkg/errors"
3330
"golang.org/x/sys/unix"
3431

32+
"github.com/google/cadvisor/container"
33+
info "github.com/google/cadvisor/info/v1"
34+
"github.com/google/cadvisor/utils"
35+
3536
"k8s.io/klog/v2"
3637
)
3738

0 commit comments

Comments
 (0)