adopt compose-go/v2

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-11-08 10:19:24 +01:00 committed by Nicolas De loof
parent 85a1aec123
commit cda04f288e
62 changed files with 187 additions and 144 deletions

View file

@ -22,7 +22,7 @@ import (
"strings"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
)

View file

@ -19,7 +19,7 @@ package api
import (
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"gotest.tools/v3/assert"
)

View file

@ -19,7 +19,7 @@ package api
import (
"context"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
)
var _ Service = &ServiceProxy{}

View file

@ -23,7 +23,7 @@ import (
"io"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types"
containerType "github.com/docker/docker/api/types/container"

View file

@ -26,7 +26,7 @@ import (
"github.com/moby/buildkit/util/progress/progressui"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/build"
"github.com/docker/buildx/builder"

View file

@ -31,7 +31,7 @@ import (
"github.com/docker/docker/api/types/registry"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command/image/build"
dockertypes "github.com/docker/docker/api/types"

View file

@ -29,7 +29,7 @@ import (
"github.com/docker/docker/api/types/volume"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/distribution/reference"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config/configfile"
@ -177,23 +177,25 @@ func (s *composeService) Config(ctx context.Context, project *types.Project, opt
// projectFromName builds a types.Project based on actual resources with compose labels set
func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
project := &types.Project{
Name: projectName,
Name: projectName,
Services: types.Services{},
}
if len(containers) == 0 {
return project, fmt.Errorf("no container found for project %q: %w", projectName, api.ErrNotFound)
}
set := map[string]*types.ServiceConfig{}
set := map[string]types.ServiceConfig{}
for _, c := range containers {
serviceLabel := c.Labels[api.ServiceLabel]
_, ok := set[serviceLabel]
service, ok := set[serviceLabel]
if !ok {
set[serviceLabel] = &types.ServiceConfig{
service = types.ServiceConfig{
Name: serviceLabel,
Image: c.Image,
Labels: c.Labels,
}
set[serviceLabel] = service
}
set[serviceLabel].Scale++
service.Scale = increment(service.Scale)
}
for _, service := range set {
dependencies := service.Labels[api.DependenciesLabel]
@ -217,7 +219,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart, Required: required}
}
}
project.Services = append(project.Services, *service)
project.Services = append(project.Services, service)
}
SERVICES:
for _, qs := range services {
@ -236,6 +238,14 @@ SERVICES:
return project, nil
}
func increment(scale *int) *int {
i := 1
if scale != nil {
i = *scale + 1
}
return &i
}
func (s *composeService) actualVolumes(ctx context.Context, projectName string) (types.Volumes, error) {
opts := volume.ListOptions{
Filters: filters.NewArgs(projectFilter(projectName)),

View file

@ -22,7 +22,7 @@ import (
"sort"
"strconv"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/utils"
moby "github.com/docker/docker/api/types"

View file

@ -29,7 +29,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/containerd/containerd/platforms"
"github.com/docker/compose/v2/internal/tracing"
moby "github.com/docker/docker/api/types"
@ -428,7 +428,7 @@ func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceD
}
}
return false, err
} else if service.Scale == 0 {
} else if service.Scale != nil && *service.Scale == 0 {
// don't wait for the dependency which configured to have 0 containers running
return false, nil
}
@ -457,8 +457,11 @@ func nextContainerNumber(containers []moby.Container) int {
func getScale(config types.ServiceConfig) (int, error) {
scale := 1
if config.Deploy != nil && config.Deploy.Replicas != nil {
scale = int(*config.Deploy.Replicas)
if config.Scale != nil {
scale = *config.Scale
} else if config.Deploy != nil && config.Deploy.Replicas != nil {
// this should not be required as compose-go enforce consistency between scale anr replicas
scale = *config.Deploy.Replicas
}
if scale > 1 && config.ContainerName != "" {
return 0, fmt.Errorf(doubledContainerNameWarning,

View file

@ -22,7 +22,7 @@ import (
"strings"
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
containerType "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
@ -37,31 +37,33 @@ func TestContainerName(t *testing.T) {
s := types.ServiceConfig{
Name: "testservicename",
ContainerName: "testcontainername",
Scale: 1,
Scale: intPtr(1),
Deploy: &types.DeployConfig{},
}
ret, err := getScale(s)
assert.NilError(t, err)
assert.Equal(t, ret, s.Scale)
assert.Equal(t, ret, *s.Scale)
var zero uint64 // = 0
s.Deploy.Replicas = &zero
s.Scale = intPtr(0)
ret, err = getScale(s)
assert.NilError(t, err)
assert.Equal(t, ret, int(*s.Deploy.Replicas))
assert.Equal(t, ret, *s.Scale)
var two uint64 = 2
s.Deploy.Replicas = &two
s.Scale = intPtr(2)
_, err = getScale(s)
assert.Error(t, err, fmt.Sprintf(doubledContainerNameWarning, s.Name, s.ContainerName))
}
func intPtr(i int) *int {
return &i
}
func TestServiceLinks(t *testing.T) {
const dbContainerName = "/" + testProject + "-db-1"
const webContainerName = "/" + testProject + "-web-1"
s := types.ServiceConfig{
Name: "web",
Scale: 1,
Scale: intPtr(1),
}
containerListOptions := containerType.ListOptions{
@ -223,8 +225,8 @@ func TestWaitDependencies(t *testing.T) {
cli.EXPECT().Client().Return(apiClient).AnyTimes()
t.Run("should skip dependencies with scale 0", func(t *testing.T) {
dbService := types.ServiceConfig{Name: "db", Scale: 0}
redisService := types.ServiceConfig{Name: "redis", Scale: 0}
dbService := types.ServiceConfig{Name: "db", Scale: intPtr(0)}
redisService := types.ServiceConfig{Name: "redis", Scale: intPtr(0)}
project := types.Project{Name: strings.ToLower(testProject), Services: types.Services{dbService, redisService}}
dependencies := types.DependsOnConfig{
"db": {Condition: ServiceConditionRunningOrHealthy},
@ -233,8 +235,8 @@ func TestWaitDependencies(t *testing.T) {
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
})
t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
dbService := types.ServiceConfig{Name: "db", Scale: 1}
redisService := types.ServiceConfig{Name: "redis", Scale: 1}
dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
redisService := types.ServiceConfig{Name: "redis", Scale: intPtr(1)}
project := types.Project{Name: strings.ToLower(testProject), Services: types.Services{dbService, redisService}}
dependencies := types.DependsOnConfig{
"db": {Condition: types.ServiceConditionStarted, Required: true},

View file

@ -20,7 +20,7 @@ import (
"fmt"
"time"
compose "github.com/compose-spec/compose-go/types"
compose "github.com/compose-spec/compose-go/v2/types"
"github.com/docker/docker/api/types/container"
)

View file

@ -41,7 +41,7 @@ import (
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
@ -810,7 +810,7 @@ func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount
}
definedConfig := p.Configs[config.Source]
if definedConfig.External.External {
if definedConfig.External {
return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
}
@ -860,7 +860,7 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
}
definedSecret := p.Secrets[secret.Source]
if definedSecret.External.External {
if definedSecret.External {
return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
}
@ -1019,7 +1019,7 @@ func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
}
func (s *composeService) ensureNetwork(ctx context.Context, n *types.NetworkConfig) error {
if n.External.External {
if n.External {
return s.resolveExternalNetwork(ctx, n)
}
@ -1206,14 +1206,14 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo
if !errdefs.IsNotFound(err) {
return err
}
if volume.External.External {
if volume.External {
return fmt.Errorf("external volume %q not found", volume.Name)
}
err := s.createVolume(ctx, volume)
return err
}
if volume.External.External {
if volume.External {
return nil
}

View file

@ -26,7 +26,7 @@ import (
"github.com/docker/compose/v2/pkg/api"
composetypes "github.com/compose-spec/compose-go/types"
composetypes "github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
mountTypes "github.com/docker/docker/api/types/mount"

View file

@ -22,7 +22,7 @@ import (
"strings"
"sync"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"golang.org/x/sync/errgroup"

View file

@ -23,7 +23,7 @@ import (
"sync"
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
testify "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

View file

@ -24,7 +24,7 @@ import (
"github.com/docker/compose/v2/pkg/utils"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
containerType "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
@ -136,7 +136,7 @@ func checkSelectedServices(options api.DownOptions, project *types.Project) ([]s
func (s *composeService) ensureVolumesDown(ctx context.Context, project *types.Project, w progress.Writer) []downOp {
var ops []downOp
for _, vol := range project.Volumes {
if vol.External.External {
if vol.External {
continue
}
volumeName := vol.Name
@ -171,7 +171,7 @@ func (s *composeService) ensureImagesDown(ctx context.Context, project *types.Pr
func (s *composeService) ensureNetworksDown(ctx context.Context, project *types.Project, w progress.Writer) []downOp {
var ops []downOp
for key, n := range project.Networks {
if n.External.External {
if n.External {
continue
}
// loop capture variable for op closure

View file

@ -23,7 +23,7 @@ import (
"strings"
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types"
containerType "github.com/docker/docker/api/types/container"

View file

@ -20,7 +20,7 @@ import (
"errors"
"io/fs"
"github.com/compose-spec/compose-go/errdefs"
"github.com/compose-spec/compose-go/v2/errdefs"
)
// Error error to categorize failures and extract metrics info

View file

@ -19,7 +19,7 @@ package compose
import (
"encoding/json"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/opencontainers/go-digest"
)
@ -28,12 +28,11 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
// remove the Build config when generating the service hash
o.Build = nil
o.PullPolicy = ""
if o.Deploy == nil {
o.Deploy = &types.DeployConfig{}
o.Scale = nil
if o.Deploy != nil {
o.Deploy.Replicas = nil
}
o.Scale = 1
var one uint64 = 1
o.Deploy.Replicas = &one
bytes, err := json.Marshal(o)
if err != nil {
return "", err

View file

@ -19,7 +19,7 @@ package compose
import (
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"gotest.tools/v3/assert"
)
@ -31,9 +31,9 @@ func TestServiceHash(t *testing.T) {
assert.Equal(t, hash1, hash2)
}
func serviceConfig(replicas uint64) types.ServiceConfig {
func serviceConfig(replicas int) types.ServiceConfig {
return types.ServiceConfig{
Scale: int(replicas),
Scale: &replicas,
Deploy: &types.DeployConfig{
Replicas: &replicas,
},

View file

@ -22,7 +22,7 @@ import (
"sort"
"sync"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/distribution/reference"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"

View file

@ -23,7 +23,7 @@ import (
"sync"
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
containerType "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"

View file

@ -20,7 +20,7 @@ import (
"context"
"os"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/distribution/reference"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/compose/v2/internal/ocipush"
@ -122,12 +122,13 @@ func (s *composeService) generateImageDigestsOverride(ctx context.Context, proje
if err != nil {
return nil, err
}
override := types.Project{}
for _, service := range project.Services {
override.Services = append(override.Services, types.ServiceConfig{
Name: service.Name,
override := types.Project{
Services: types.Services{},
}
for name, service := range project.Services {
override.Services[name] = types.ServiceConfig{
Image: service.Image,
})
}
}
return override.MarshalYAML()
}

View file

@ -25,7 +25,7 @@ import (
"io"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/distribution/reference"
"github.com/docker/buildx/driver"
moby "github.com/docker/docker/api/types"

View file

@ -25,7 +25,7 @@ import (
"io"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/distribution/reference"
"github.com/docker/buildx/driver"
moby "github.com/docker/docker/api/types"

View file

@ -20,7 +20,7 @@ import (
"context"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"

View file

@ -23,7 +23,7 @@ import (
"os"
"os/signal"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli"
cmd "github.com/docker/cli/cli/command/container"
"github.com/docker/compose/v2/pkg/api"
@ -73,7 +73,8 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
if service.ContainerName == "" {
service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, stringid.TruncateID(slug), api.Separator)
}
service.Scale = 1
one := 1
service.Scale = &one
service.Restart = ""
if service.Deploy != nil {
service.Deploy.RestartPolicy = nil

View file

@ -18,7 +18,7 @@ package compose
import (
"context"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/internal/tracing"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"

View file

@ -24,7 +24,7 @@ import (
"strconv"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
)

View file

@ -30,7 +30,7 @@ import (
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"golang.org/x/sync/errgroup"

View file

@ -23,7 +23,7 @@ import (
"os/signal"
"syscall"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli"
"github.com/docker/compose/v2/internal/tracing"
"github.com/docker/compose/v2/pkg/api"

View file

@ -21,7 +21,7 @@ import (
"strconv"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
)
@ -30,13 +30,13 @@ type vizGraph map[*types.ServiceConfig][]*types.ServiceConfig
func (s *composeService) Viz(_ context.Context, project *types.Project, opts api.VizOptions) (string, error) {
graph := make(vizGraph)
for i, serviceConfig := range project.Services {
serviceConfigPtr := &project.Services[i]
graph[serviceConfigPtr] = make([]*types.ServiceConfig, 0, len(serviceConfig.DependsOn))
for dependencyName := range serviceConfig.DependsOn {
for _, service := range project.Services {
service := service
graph[&service] = make([]*types.ServiceConfig, 0, len(service.DependsOn))
for dependencyName := range service.DependsOn {
// no error should be returned since dependencyName should exist
dependency, _ := project.GetService(dependencyName)
graph[serviceConfigPtr] = append(graph[serviceConfigPtr], &dependency)
graph[&service] = append(graph[&service], &dependency)
}
}

View file

@ -21,7 +21,7 @@ import (
"strconv"
"testing"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

View file

@ -27,7 +27,7 @@ import (
"strings"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/internal/sync"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/watch"

View file

@ -20,7 +20,7 @@ import (
"testing"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/internal/sync"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/mocks"

View file

@ -40,18 +40,21 @@ func TestScaleBasicCases(t *testing.T) {
t.Log("scale up one service")
res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=2")
checkServiceContainer(t, res.Combined(), "scale-basic-tests-dbadmin", "Started", 2)
out := res.Combined()
checkServiceContainer(t, out, "scale-basic-tests-dbadmin", "Started", 2)
t.Log("scale up 2 services")
res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "front=3", "back=2")
checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Running", 2)
checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Started", 1)
checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Running", 1)
checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Started", 1)
out = res.Combined()
checkServiceContainer(t, out, "scale-basic-tests-front", "Running", 2)
checkServiceContainer(t, out, "scale-basic-tests-front", "Started", 1)
checkServiceContainer(t, out, "scale-basic-tests-back", "Running", 1)
checkServiceContainer(t, out, "scale-basic-tests-back", "Started", 1)
t.Log("scale down one service")
res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=1")
checkServiceContainer(t, res.Combined(), "scale-basic-tests-dbadmin", "Running", 1)
out = res.Combined()
checkServiceContainer(t, out, "scale-basic-tests-dbadmin", "Running", 1)
t.Log("scale to 0 a service")
res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=0")
@ -59,9 +62,10 @@ func TestScaleBasicCases(t *testing.T) {
t.Log("scale down 2 services")
res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "front=2", "back=1")
checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Running", 2)
assert.Check(t, !strings.Contains(res.Combined(), "Container scale-basic-tests-front-3 Running"), res.Combined())
checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Running", 1)
out = res.Combined()
checkServiceContainer(t, out, "scale-basic-tests-front", "Running", 2)
assert.Check(t, !strings.Contains(out, "Container scale-basic-tests-front-3 Running"), res.Combined())
checkServiceContainer(t, out, "scale-basic-tests-back", "Running", 1)
}
func TestScaleWithDepsCases(t *testing.T) {

View file

@ -8,7 +8,7 @@ import (
context "context"
reflect "reflect"
types "github.com/compose-spec/compose-go/types"
types "github.com/compose-spec/compose-go/v2/types"
api "github.com/docker/compose/v2/pkg/api"
gomock "github.com/golang/mock/gomock"
)

View file

@ -25,9 +25,9 @@ import (
"regexp"
"strconv"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/moby/buildkit/util/gitutil"
)

View file

@ -25,7 +25,7 @@ import (
"strconv"
"strings"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/distribution/reference"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/imagetools"