Merge pull request #923 from gtardif/lint_local

Lint local backend
This commit is contained in:
Guillaume Tardif 2020-11-18 17:33:51 +01:00 committed by GitHub
commit fbf5b0a7f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 46 additions and 45 deletions

3
.github/labeler.yml vendored
View File

@ -4,6 +4,9 @@ aci:
ecs: ecs:
- ecs/**/* - ecs/**/*
local:
- local/**/*
cli: cli:
- cli/**/* - cli/**/*

View File

@ -43,11 +43,13 @@ FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS lint-base
FROM base AS lint FROM base AS lint
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
ARG BUILD_TAGS
ARG GIT_TAG ARG GIT_TAG
RUN --mount=target=. \ RUN --mount=target=. \
--mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/.cache/golangci-lint \ --mount=type=cache,target=/root/.cache/golangci-lint \
BUILD_TAGS=${BUILD_TAGS} \
GIT_TAG=${GIT_TAG} \ GIT_TAG=${GIT_TAG} \
make -f builder.Makefile lint make -f builder.Makefile lint

View File

@ -72,6 +72,7 @@ cache-clear: ## Clear the builder cache
lint: ## run linter(s) lint: ## run linter(s)
@docker build . \ @docker build . \
--build-arg BUILD_TAGS=example,local,e2e \
--build-arg GIT_TAG=$(GIT_TAG) \ --build-arg GIT_TAG=$(GIT_TAG) \
--target lint --target lint

View File

@ -39,6 +39,7 @@ WORK_DIR:=$(shell mktemp -d)
TAGS:= TAGS:=
ifdef BUILD_TAGS ifdef BUILD_TAGS
TAGS=-tags $(BUILD_TAGS) TAGS=-tags $(BUILD_TAGS)
LINT_TAGS=--build-tags $(BUILD_TAGS)
endif endif
TAR_TRANSFORM:=--transform s/packaging/docker/ --transform s/bin/docker/ --transform s/docker-linux-amd64/docker/ --transform s/docker-darwin-amd64/docker/ TAR_TRANSFORM:=--transform s/packaging/docker/ --transform s/bin/docker/ --transform s/docker-linux-amd64/docker/ --transform s/docker-darwin-amd64/docker/
@ -68,7 +69,7 @@ test:
.PHONY: lint .PHONY: lint
lint: lint:
golangci-lint run --timeout 10m0s ./... golangci-lint run $(LINT_TAGS) --timeout 10m0s ./...
.PHONY: import-restrictions .PHONY: import-restrictions
import-restrictions: import-restrictions:

View File

@ -20,6 +20,7 @@ package local
import ( import (
"context" "context"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
@ -71,6 +72,3 @@ func (s *local) VolumeService() volumes.Service {
func (s *local) ResourceService() resources.Service { func (s *local) ResourceService() resources.Service {
return nil return nil
} }

View File

@ -22,13 +22,14 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"golang.org/x/sync/errgroup"
"io" "io"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"golang.org/x/sync/errgroup"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/api/containers"
@ -222,11 +223,11 @@ func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error
consumer := formatter.NewLogConsumer(w) consumer := formatter.NewLogConsumer(w)
for _, c := range list { for _, c := range list {
service := c.Labels[serviceLabel] service := c.Labels[serviceLabel]
containerId := c.ID containerID := c.ID
go func() { go func() {
s.containerService.Logs(ctx,containerId, containers.LogsRequest{ _ = s.containerService.Logs(ctx, containerID, containers.LogsRequest{
Follow: true, Follow: true,
Writer: consumer.GetWriter(service, containerId), Writer: consumer.GetWriter(service, containerID),
}) })
wg.Done() wg.Done()
}() }()
@ -260,7 +261,6 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt
return status, nil return status, nil
} }
func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) { func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
_, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true}) _, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true})
if err != nil { if err != nil {
@ -340,15 +340,8 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
// StopTimeout: s.StopGracePeriod FIXME conversion // StopTimeout: s.StopGracePeriod FIXME conversion
} }
mountOptions, err := buildContainerMountOptions(p, s, inherit) mountOptions := buildContainerMountOptions(p, s, inherit)
if err != nil { bindings := buildContainerBindingOptions(s)
return nil, nil, nil, err
}
bindings, err := buildContainerBindingOptions(s)
if err != nil {
return nil, nil, nil, err
}
networkMode := getNetworkMode(p, s) networkMode := getNetworkMode(p, s)
hostConfig := container.HostConfig{ hostConfig := container.HostConfig{
@ -376,7 +369,7 @@ func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
return ports return ports
} }
func buildContainerBindingOptions(s types.ServiceConfig) (nat.PortMap, error) { func buildContainerBindingOptions(s types.ServiceConfig) nat.PortMap {
bindings := nat.PortMap{} bindings := nat.PortMap{}
for _, port := range s.Ports { for _, port := range s.Ports {
p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol)) p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
@ -388,10 +381,10 @@ func buildContainerBindingOptions(s types.ServiceConfig) (nat.PortMap, error) {
bind = append(bind, binding) bind = append(bind, binding)
bindings[p] = bind bindings[p] = bind
} }
return bindings, nil return bindings
} }
func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) { func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit *moby.Container) []mount.Mount {
mounts := []mount.Mount{} mounts := []mount.Mount{}
var inherited []string var inherited []string
if inherit != nil { if inherit != nil {
@ -434,7 +427,7 @@ func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit
TmpfsOptions: buildTmpfsOptions(v.Tmpfs), TmpfsOptions: buildTmpfsOptions(v.Tmpfs),
}) })
} }
return mounts, nil return mounts
} }
func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions { func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
@ -561,9 +554,8 @@ func (s *local) ensureNetwork(ctx context.Context, n types.NetworkConfig) error
Done: true, Done: true,
}) })
return nil return nil
} else {
return err
} }
return err
} }
return nil return nil
} }

View File

@ -21,6 +21,8 @@ package local
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/api/containers"
"github.com/docker/compose-cli/progress" "github.com/docker/compose-cli/progress"
@ -28,7 +30,6 @@ import (
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"strconv"
) )
func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error { func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
@ -229,7 +230,6 @@ func (s *local) runContainer(ctx context.Context, project *types.Project, servic
return nil return nil
} }
func (s *local) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error { func (s *local) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error {
err := s.containerService.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{ err := s.containerService.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{
Aliases: []string{service}, Aliases: []string{service},

View File

@ -20,12 +20,13 @@ package local
import ( import (
"context" "context"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func inDependencyOrder(ctx context.Context, project *types.Project, fn func(types.ServiceConfig) error) error { func inDependencyOrder(ctx context.Context, project *types.Project, fn func(types.ServiceConfig) error) error {
eg, ctx := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
var ( var (
scheduled []string scheduled []string
ready []string ready []string

View File

@ -20,9 +20,10 @@ package local
import ( import (
"context" "context"
"gotest.tools/v3/assert"
"testing" "testing"
"gotest.tools/v3/assert"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
) )
@ -47,11 +48,12 @@ func TestInDependencyOrder(t *testing.T) {
}, },
}, },
} }
//nolint:errcheck, unparam
go inDependencyOrder(context.TODO(), &project, func(config types.ServiceConfig) error { go inDependencyOrder(context.TODO(), &project, func(config types.ServiceConfig) error {
order <- config.Name order <- config.Name
return nil return nil
}) })
assert.Equal(t, <- order, "test3") assert.Equal(t, <-order, "test3")
assert.Equal(t, <- order, "test2") assert.Equal(t, <-order, "test2")
assert.Equal(t, <- order, "test1") assert.Equal(t, <-order, "test1")
} }

View File

@ -20,6 +20,7 @@ package local
import ( import (
"fmt" "fmt"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
) )

View File

@ -20,13 +20,14 @@ package local
import ( import (
"encoding/json" "encoding/json"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
) )
func jsonHash(o interface{}) (string, error) { func jsonHash(o interface{}) (string, error) {
bytes, err := json.Marshal(o) bytes, err := json.Marshal(o)
if err != nil { if err != nil {
return "", nil return "", err
} }
return digest.SHA256.FromBytes(bytes).String(), nil return digest.SHA256.FromBytes(bytes).String(), nil
} }

View File

@ -22,7 +22,6 @@ import (
"os" "os"
) )
func init() { func init() {
testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET") testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET")
if defined { if defined {