Skip to content

Commit

Permalink
Fix local imports, add appropriate linter
Browse files Browse the repository at this point in the history
Format all imports to have three groups:
1. Standard library packages.
2. Third-party packages.
3. Local packages (i.e. ones with github.com/google/cadvisor prefix).

Modify Makefile to do the same for format target. Note goimports does
the same thing as gofmt, plus imports sorting. It can't do what gofmt -s
does, but it was not done before (although checked according to
.golangci.yml), so it's left as is.

Finally, add goimports linter with proper configuration to check imports
formatting in CI.

NOTE we can't use $(pkgs) and $(cmd_pkgs) in Makefile's format target,
as we have to skip some auto-generated files (*.pb.go), so use find|grep
instead.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Oct 18, 2021
1 parent a70f60e commit 4670a26
Show file tree
Hide file tree
Showing 65 changed files with 131 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ run:
min-confidence: 0
gofmt:
simplify: true
goimports:
local-prefixes: github.com/google/cadvisor
linters:
disable-all: true
enable:
Expand All @@ -22,6 +24,7 @@ linters:
- typecheck
- golint
- gofmt
- goimports
issues:
max-issues-per-linter: 0
max-same-issues: 0
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

GO := go
GOFMT := goimports -w -local github.com/google/cadvisor
pkgs = $(shell $(GO) list ./... | grep -v vendor)
cmd_pkgs = $(shell cd cmd && $(GO) list ./... | grep -v vendor)
arch ?= $(shell go env GOARCH)
Expand Down Expand Up @@ -62,8 +63,10 @@ tidy:

format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)
@cd cmd && $(GO) fmt $(cmd_pkgs)
@# Can't use $(pkgs) and $(cmd_pkgs) here as we need
@# to skip auto-generated files.
@find -name '*.go' | grep -Ev '^vendor/|.pb.go$$' | \
xargs goimports -w -local github.com/google/cadvisor

vet:
@echo ">> vetting code"
Expand Down
3 changes: 2 additions & 1 deletion accelerators/nvidia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
package accelerators

import (
"github.com/google/cadvisor/stats"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/google/cadvisor/stats"

"github.com/mindprince/gonvml"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"path"
"strings"

"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"

"k8s.io/klog/v2"
)
Expand Down
2 changes: 1 addition & 1 deletion client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"

v1 "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/info/v2"
v2 "github.com/google/cadvisor/info/v2"
)

// Client represents the base URL for a cAdvisor client.
Expand Down
5 changes: 3 additions & 2 deletions client/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"testing"
"time"

"github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/info/v2"
"github.com/stretchr/testify/assert"

v1 "github.com/google/cadvisor/info/v1"
v2 "github.com/google/cadvisor/info/v2"
)

func cadvisorTestClient(path string, expectedPostObj *v1.ContainerInfoRequest, replyObj interface{}, t *testing.T) (*Client, *httptest.Server, error) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/cadvisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"flag"
"testing"

"github.com/google/cadvisor/container"
"github.com/stretchr/testify/assert"

"github.com/google/cadvisor/container"
)

func TestTcpMetricsAreDisabledByDefault(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/internal/container/mesos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package mesos

import (
"fmt"
"net/url"
"sync"

"github.com/Rican7/retry"
"github.com/Rican7/retry/strategy"
mesos "github.com/mesos/mesos-go/api/v1/lib"
Expand All @@ -24,8 +27,6 @@ import (
mclient "github.com/mesos/mesos-go/api/v1/lib/client"
"github.com/mesos/mesos-go/api/v1/lib/encoding/codecs"
"github.com/mesos/mesos-go/api/v1/lib/httpcli"
"net/url"
"sync"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion cmd/internal/container/mesos/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"strings"
"time"

"k8s.io/klog/v2"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog/v2"
)

var MesosAgentAddress = flag.String("mesos_agent", "127.0.0.1:5051", "Mesos agent address")
Expand Down
6 changes: 4 additions & 2 deletions cmd/internal/container/mesos/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package mesos

import (
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
"testing"

mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/stretchr/testify/assert"
"testing"

containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
)

func TestIsContainerName(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/internal/container/mesos/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
"fmt"
"testing"

mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/stretchr/testify/assert"

"github.com/google/cadvisor/container"
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/stretchr/testify/assert"
)

func PopulateContainer() *mContainer {
Expand Down
3 changes: 2 additions & 1 deletion cmd/internal/container/mesos/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package install

import (
"k8s.io/klog/v2"

"github.com/google/cadvisor/cmd/internal/container/mesos"
"github.com/google/cadvisor/container"
"k8s.io/klog/v2"
)

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/internal/container/mesos/mesos_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mesos

import (
"fmt"

mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib/agent"
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/internal/container/mesos/mesos_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package mesos

import (
"fmt"
"testing"

mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib/agent"
"github.com/stretchr/testify/assert"
"testing"
)

func PopulateFrameworks(fwID string) *agent.Response_GetFrameworks {
Expand Down
1 change: 1 addition & 0 deletions cmd/internal/storage/bigquery/client/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/SeanDolphin/bqschema"

"github.com/google/cadvisor/cmd/internal/storage/bigquery/client"
)

Expand Down
2 changes: 1 addition & 1 deletion collector/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
"time"

"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
)

const metricLabelPrefix = "io.cadvisor.metric."
Expand Down
4 changes: 2 additions & 2 deletions collector/collector_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"testing"
"time"

"github.com/google/cadvisor/info/v1"

"github.com/stretchr/testify/assert"

v1 "github.com/google/cadvisor/info/v1"
)

type fakeCollector struct {
Expand Down
3 changes: 2 additions & 1 deletion collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"time"

"encoding/json"
"github.com/google/cadvisor/info/v1"

v1 "github.com/google/cadvisor/info/v1"
)

type Config struct {
Expand Down
2 changes: 1 addition & 1 deletion collector/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package collector
import (
"time"

"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
)

type FakeCollectorManager struct {
Expand Down
2 changes: 1 addition & 1 deletion collector/generic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
)

type GenericCollector struct {
Expand Down
4 changes: 2 additions & 2 deletions collector/generic_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"os"
"testing"

"github.com/google/cadvisor/info/v1"
"github.com/stretchr/testify/assert"

containertest "github.com/google/cadvisor/container/testing"
"github.com/stretchr/testify/assert"
v1 "github.com/google/cadvisor/info/v1"
)

func TestEmptyConfig(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion collector/prometheus_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/prometheus/common/model"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
)

type PrometheusCollector struct {
Expand Down
6 changes: 3 additions & 3 deletions collector/prometheus_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"net/http/httptest"
"testing"

"github.com/google/cadvisor/info/v1"

containertest "github.com/google/cadvisor/container/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

containertest "github.com/google/cadvisor/container/testing"
v1 "github.com/google/cadvisor/info/v1"
)

func TestPrometheus(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion collector/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package collector
import (
"time"

"github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
)

// TODO(vmarmol): Export to a custom metrics type when that is available.
Expand Down
7 changes: 4 additions & 3 deletions container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
"strings"
"time"

"github.com/google/cadvisor/container"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/utils"
"github.com/karrick/godirwalk"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/pkg/errors"
"golang.org/x/sys/unix"

"github.com/google/cadvisor/container"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/utils"

"k8s.io/klog/v2"
)

Expand Down
3 changes: 2 additions & 1 deletion container/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

info "github.com/google/cadvisor/info/v1"
v2 "github.com/google/cadvisor/info/v2"
"github.com/stretchr/testify/assert"
)

func BenchmarkListDirectories(b *testing.B) {
Expand Down
3 changes: 2 additions & 1 deletion container/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/pkg/dialer"
ptypes "github.com/gogo/protobuf/types"
criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"

criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"
)

type client struct {
Expand Down
1 change: 1 addition & 0 deletions container/containerd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

types "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/containers"

criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"
)

Expand Down
6 changes: 4 additions & 2 deletions container/containerd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import (
"time"

"github.com/containerd/containerd/errdefs"
criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"
"golang.org/x/net/context"
"k8s.io/klog/v2"

criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"

specs "github.com/opencontainers/runtime-spec/specs-go"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/common"
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

type fsUsageProvider struct {
Expand Down
5 changes: 3 additions & 2 deletions container/containerd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
types "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/containers"
"github.com/containerd/typeurl"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"

"github.com/google/cadvisor/container"
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
criapi "github.com/google/cadvisor/cri-api/pkg/apis/runtime/v1alpha2"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
v1 "github.com/google/cadvisor/info/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion container/containerd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package install

import (
"k8s.io/klog/v2"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/containerd"
"k8s.io/klog/v2"
)

func init() {
Expand Down
Loading

0 comments on commit 4670a26

Please sign in to comment.