Skip to content

Commit

Permalink
Fix incompatibility issues after upgrading compose-go
Browse files Browse the repository at this point in the history
  • Loading branch information
szobov committed Nov 28, 2024
1 parent b891dbc commit b6def2b
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 178 deletions.
33 changes: 16 additions & 17 deletions cli/compose/convert/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

composetypes "github.com/compose-spec/compose-go/v2/types"
"github.com/docker/docker/api/types"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
)
Expand Down Expand Up @@ -52,45 +51,45 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str
type networkMap map[string]composetypes.NetworkConfig

// Networks from the compose-file type to the engine API type
func Networks(namespace Namespace, networks composetypes.Networks, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) {
func Networks(namespace Namespace, networks composetypes.Networks, servicesNetworks map[string]struct{}) (map[string]networktypes.CreateOptions, []string) {
if networks == nil {
networks = make(map[string]composetypes.NetworkConfig)
}

externalNetworks := []string{}
result := make(map[string]network.CreateOptions)
result := make(map[string]networktypes.CreateOptions)
for internalName := range servicesNetworks {
network := networks[internalName]
if network.External {
externalNetworks = append(externalNetworks, network.Name)
continue
}

createOpts := network.CreateOptions{
Labels: AddStackLabel(namespace, nw.Labels),
Driver: nw.Driver,
Options: nw.DriverOpts,
Internal: nw.Internal,
Attachable: nw.Attachable,
createOpts := networktypes.CreateOptions{
Labels: AddStackLabel(namespace, network.Labels),
Driver: network.Driver,
Options: network.DriverOpts,
Internal: network.Internal,
Attachable: network.Attachable,
}

if nw.Ipam.Driver != "" || len(nw.Ipam.Config) > 0 {
createOpts.IPAM = &network.IPAM{}
if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 {
createOpts.IPAM = &networktypes.IPAM{}
}

if nw.Ipam.Driver != "" {
createOpts.IPAM.Driver = nw.Ipam.Driver
if network.Ipam.Driver != "" {
createOpts.IPAM.Driver = network.Ipam.Driver
}
for _, ipamConfig := range nw.Ipam.Config {
config := network.IPAMConfig{
for _, ipamConfig := range network.Ipam.Config {
config := networktypes.IPAMConfig{
Subnet: ipamConfig.Subnet,
}
createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
}

networkName := namespace.Scope(internalName)
if nw.Name != "" {
networkName = nw.Name
if network.Name != "" {
networkName = network.Name
}
result[networkName] = createOpts
}
Expand Down
26 changes: 5 additions & 21 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ func Service(
return swarm.ServiceSpec{}, err
}

resources, err := convertResources(service.Deploy.Resources)
if err != nil {
return swarm.ServiceSpec{}, err
}
resources := convertResources(service.Deploy.Resources)

restartPolicy, err := convertRestartPolicy(
service.Restart, service.Deploy.RestartPolicy)
Expand Down Expand Up @@ -535,31 +532,18 @@ func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig
}
}

func convertResources(source composetypes.Resources) (*swarm.ResourceRequirements, error) {
func convertResources(source composetypes.Resources) *swarm.ResourceRequirements {
resources := &swarm.ResourceRequirements{}
var err error
if source.Limits != nil {
var cpus int64
if source.Limits.NanoCPUs != "" {
cpus, err = opts.ParseCPUs(source.Limits.NanoCPUs)
if err != nil {
return nil, err
}
}
cpus := int64(source.Limits.NanoCPUs * 1e9)
resources.Limits = &swarm.Limit{
NanoCPUs: cpus,
MemoryBytes: int64(source.Limits.MemoryBytes),
Pids: source.Limits.Pids,
}
}
if source.Reservations != nil {
var cpus int64
if source.Reservations.NanoCPUs != "" {
cpus, err = opts.ParseCPUs(source.Reservations.NanoCPUs)
if err != nil {
return nil, err
}
}
cpus := int64(source.Reservations.NanoCPUs * 1e9)

var generic []swarm.GenericResource
for _, res := range source.Reservations.GenericResources {
Expand All @@ -581,7 +565,7 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement
GenericResources: generic,
}
}
return resources, nil
return resources
}

func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
Expand Down
10 changes: 4 additions & 6 deletions cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,15 @@ func TestConvertExtraHosts(t *testing.T) {
func TestConvertResourcesFull(t *testing.T) {
source := composetypes.Resources{
Limits: &composetypes.Resource{
NanoCPUs: "0.003",
NanoCPUs: 0.003,
MemoryBytes: composetypes.UnitBytes(300000000),
},
Reservations: &composetypes.Resource{
NanoCPUs: "0.002",
NanoCPUs: 0.002,
MemoryBytes: composetypes.UnitBytes(200000000),
},
}
resources, err := convertResources(source)
assert.NilError(t, err)
resources := convertResources(source)

expected := &swarm.ResourceRequirements{
Limits: &swarm.Limit{
Expand All @@ -116,8 +115,7 @@ func TestConvertResourcesOnlyMemory(t *testing.T) {
MemoryBytes: composetypes.UnitBytes(200000000),
},
}
resources, err := convertResources(source)
assert.NilError(t, err)
resources := convertResources(source)

expected := &swarm.ResourceRequirements{
Limits: &swarm.Limit{
Expand Down
43 changes: 0 additions & 43 deletions cli/compose/convert/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,49 +154,6 @@ func TestConvertVolumeToMountConflictingOptionsTmpfsInBind(t *testing.T) {
assert.Error(t, err, "tmpfs options are incompatible with type bind")
}

func TestConvertVolumeToMountConflictingOptionsClusterInVolume(t *testing.T) {
namespace := NewNamespace("foo")

config := composetypes.ServiceVolumeConfig{
Type: "volume",
Target: "/target",
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type volume")
}

func TestConvertVolumeToMountConflictingOptionsClusterInBind(t *testing.T) {
namespace := NewNamespace("foo")

config := composetypes.ServiceVolumeConfig{
Type: "bind",
Source: "/foo",
Target: "/target",
Bind: &composetypes.ServiceVolumeBind{
Propagation: "slave",
},
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type bind")
}

func TestConvertVolumeToMountConflictingOptionsClusterInTmpfs(t *testing.T) {
namespace := NewNamespace("foo")

config := composetypes.ServiceVolumeConfig{
Type: "tmpfs",
Target: "/target",
Tmpfs: &composetypes.ServiceVolumeTmpfs{
Size: 1000,
},
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type tmpfs")
}

func TestConvertVolumeToMountConflictingOptionsBindInTmpfs(t *testing.T) {
namespace := NewNamespace("foo")

Expand Down
8 changes: 5 additions & 3 deletions cli/compose/loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func services(workingDir, homeDir string) types.Services {
},
Resources: types.Resources{
Limits: &types.Resource{
NanoCPUs: "0.001",
NanoCPUs: 0.001,
MemoryBytes: 50 * 1024 * 1024,
Pids: 100,
},
Reservations: &types.Resource{
NanoCPUs: "0.0001",
NanoCPUs: 0.0001,
MemoryBytes: 20 * 1024 * 1024,
GenericResources: []types.GenericResource{
{
Expand Down Expand Up @@ -130,7 +130,9 @@ func services(workingDir, homeDir string) types.Services {
},
EndpointMode: "dnsrr",
},
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
Devices: []types.DeviceMapping{{
Source: "/dev/ttyUSB0", Target: "/dev/ttyUSB0",
}},
DNS: []string{"8.8.8.8", "9.9.9.9"},
DNSSearch: []string{"dc1.example.com", "dc2.example.com"},
DomainName: "foo.com",
Expand Down
Loading

0 comments on commit b6def2b

Please sign in to comment.