diff --git a/.github/labeler.yml b/.github/labeler.yml index 7a24d0b3f..321f074e1 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -4,6 +4,9 @@ aci: ecs: - ecs/**/* +local: + - local/**/* + cli: - cli/**/* diff --git a/Dockerfile b/Dockerfile index fd02c25af..d174ed20c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,11 +43,13 @@ FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS lint-base FROM base AS lint ENV CGO_ENABLED=0 COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint +ARG BUILD_TAGS ARG GIT_TAG RUN --mount=target=. \ --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/root/.cache/golangci-lint \ + BUILD_TAGS=${BUILD_TAGS} \ GIT_TAG=${GIT_TAG} \ make -f builder.Makefile lint diff --git a/Makefile b/Makefile index 10273684c..6c55be9a7 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,7 @@ cache-clear: ## Clear the builder cache lint: ## run linter(s) @docker build . \ + --build-arg BUILD_TAGS=example,local,e2e \ --build-arg GIT_TAG=$(GIT_TAG) \ --target lint diff --git a/builder.Makefile b/builder.Makefile index c89b23820..179ea271f 100644 --- a/builder.Makefile +++ b/builder.Makefile @@ -39,6 +39,7 @@ WORK_DIR:=$(shell mktemp -d) TAGS:= ifdef BUILD_TAGS TAGS=-tags $(BUILD_TAGS) + LINT_TAGS=--build-tags $(BUILD_TAGS) endif 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 lint: - golangci-lint run --timeout 10m0s ./... + golangci-lint run $(LINT_TAGS) --timeout 10m0s ./... .PHONY: import-restrictions import-restrictions: diff --git a/local/backend.go b/local/backend.go index 78d1195cb..35c90198f 100644 --- a/local/backend.go +++ b/local/backend.go @@ -20,6 +20,7 @@ package local import ( "context" + "github.com/docker/docker/client" "github.com/docker/compose-cli/api/compose" @@ -71,6 +72,3 @@ func (s *local) VolumeService() volumes.Service { func (s *local) ResourceService() resources.Service { return nil } - - - diff --git a/local/compose.go b/local/compose.go index 14dc36852..fc8218777 100644 --- a/local/compose.go +++ b/local/compose.go @@ -22,13 +22,14 @@ import ( "context" "encoding/json" "fmt" - "golang.org/x/sync/errgroup" "io" "path/filepath" "strconv" "strings" "sync" + "golang.org/x/sync/errgroup" + "github.com/compose-spec/compose-go/types" "github.com/docker/compose-cli/api/compose" "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) for _, c := range list { service := c.Labels[serviceLabel] - containerId := c.ID + containerID := c.ID go func() { - s.containerService.Logs(ctx,containerId, containers.LogsRequest{ + _ = s.containerService.Logs(ctx, containerID, containers.LogsRequest{ Follow: true, - Writer: consumer.GetWriter(service, containerId), + Writer: consumer.GetWriter(service, containerID), }) wg.Done() }() @@ -260,7 +261,6 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt return status, nil } - func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) { _, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true}) if err != nil { @@ -288,9 +288,9 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i return nil, nil, nil, err } labels := map[string]string{ - projectLabel: p.Name, - serviceLabel: s.Name, - configHashLabel: hash, + projectLabel: p.Name, + serviceLabel: s.Name, + configHashLabel: hash, containerNumberLabel: strconv.Itoa(number), } @@ -340,15 +340,8 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i // StopTimeout: s.StopGracePeriod FIXME conversion } - mountOptions, err := buildContainerMountOptions(p, s, inherit) - if err != nil { - return nil, nil, nil, err - } - - bindings, err := buildContainerBindingOptions(s) - if err != nil { - return nil, nil, nil, err - } + mountOptions := buildContainerMountOptions(p, s, inherit) + bindings := buildContainerBindingOptions(s) networkMode := getNetworkMode(p, s) hostConfig := container.HostConfig{ @@ -376,7 +369,7 @@ func buildContainerPorts(s types.ServiceConfig) nat.PortSet { return ports } -func buildContainerBindingOptions(s types.ServiceConfig) (nat.PortMap, error) { +func buildContainerBindingOptions(s types.ServiceConfig) nat.PortMap { bindings := nat.PortMap{} for _, port := range s.Ports { 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) 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{} var inherited []string if inherit != nil { @@ -434,7 +427,7 @@ func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit TmpfsOptions: buildTmpfsOptions(v.Tmpfs), }) } - return mounts, nil + return mounts } func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions { @@ -561,9 +554,8 @@ func (s *local) ensureNetwork(ctx context.Context, n types.NetworkConfig) error Done: true, }) return nil - } else { - return err } + return err } return nil } diff --git a/local/convergence.go b/local/convergence.go index cca0f9668..b4c468185 100644 --- a/local/convergence.go +++ b/local/convergence.go @@ -21,6 +21,8 @@ package local import ( "context" "fmt" + "strconv" + "github.com/compose-spec/compose-go/types" "github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/progress" @@ -28,7 +30,6 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/network" "golang.org/x/sync/errgroup" - "strconv" ) 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 } - func (s *local) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error { err := s.containerService.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{ Aliases: []string{service}, diff --git a/local/dependencies.go b/local/dependencies.go index d26d1c3c1..50d7a4c47 100644 --- a/local/dependencies.go +++ b/local/dependencies.go @@ -20,15 +20,16 @@ package local import ( "context" + "github.com/compose-spec/compose-go/types" "golang.org/x/sync/errgroup" ) func inDependencyOrder(ctx context.Context, project *types.Project, fn func(types.ServiceConfig) error) error { - eg, ctx := errgroup.WithContext(ctx) + eg, _ := errgroup.WithContext(ctx) var ( scheduled []string - ready []string + ready []string ) results := make(chan string) for len(ready) < len(project.Services) { diff --git a/local/dependencies_test.go b/local/dependencies_test.go index 035eda4ee..b0b6cfe37 100644 --- a/local/dependencies_test.go +++ b/local/dependencies_test.go @@ -20,9 +20,10 @@ package local import ( "context" - "gotest.tools/v3/assert" "testing" + "gotest.tools/v3/assert" + "github.com/compose-spec/compose-go/types" ) @@ -31,27 +32,28 @@ func TestInDependencyOrder(t *testing.T) { project := types.Project{ Services: []types.ServiceConfig{ { - Name: "test1", + Name: "test1", DependsOn: map[string]types.ServiceDependency{ "test2": {}, }, }, { - Name: "test2", + Name: "test2", DependsOn: map[string]types.ServiceDependency{ "test3": {}, }, }, { - Name: "test3", + Name: "test3", }, }, } + //nolint:errcheck, unparam go inDependencyOrder(context.TODO(), &project, func(config types.ServiceConfig) error { order <- config.Name return nil }) - assert.Equal(t, <- order, "test3") - assert.Equal(t, <- order, "test2") - assert.Equal(t, <- order, "test1") -} \ No newline at end of file + assert.Equal(t, <-order, "test3") + assert.Equal(t, <-order, "test2") + assert.Equal(t, <-order, "test1") +} diff --git a/local/labels.go b/local/labels.go index 29966b4d0..300e890e0 100644 --- a/local/labels.go +++ b/local/labels.go @@ -20,16 +20,17 @@ package local import ( "fmt" + "github.com/docker/docker/api/types/filters" ) const ( - projectLabel = "com.docker.compose.project" - serviceLabel = "com.docker.compose.service" - configHashLabel = "com.docker.compose.config-hash" + projectLabel = "com.docker.compose.project" + serviceLabel = "com.docker.compose.service" + configHashLabel = "com.docker.compose.config-hash" containerNumberLabel = "com.docker.compose.container-number" ) func projectFilter(projectName string) filters.KeyValuePair { return filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, projectName)) -} \ No newline at end of file +} diff --git a/local/util.go b/local/util.go index cabd257c4..adc1b2e2c 100644 --- a/local/util.go +++ b/local/util.go @@ -20,13 +20,14 @@ package local import ( "encoding/json" + "github.com/opencontainers/go-digest" ) func jsonHash(o interface{}) (string, error) { bytes, err := json.Marshal(o) if err != nil { - return "", nil + return "", err } return digest.SHA256.FromBytes(bytes).String(), nil } diff --git a/metrics/conn_e2e.go b/metrics/conn_e2e.go index 86d837151..53e87c9e2 100644 --- a/metrics/conn_e2e.go +++ b/metrics/conn_e2e.go @@ -22,7 +22,6 @@ import ( "os" ) - func init() { testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET") if defined {