Skip to content

Commit 9857b7a

Browse files
committed
Fix incompatibility issues after upgrading compose-go
Signed-off-by: Sergei Zobov <[email protected]>
1 parent 6686cf8 commit 9857b7a

7 files changed

+116
-178
lines changed

cli/compose/convert/compose.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
composetypes "github.com/compose-spec/compose-go/v2/types"
8-
"github.com/docker/docker/api/types"
98
networktypes "github.com/docker/docker/api/types/network"
109
"github.com/docker/docker/api/types/swarm"
1110
)
@@ -52,45 +51,45 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str
5251
type networkMap map[string]composetypes.NetworkConfig
5352

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

6059
externalNetworks := []string{}
61-
result := make(map[string]network.CreateOptions)
60+
result := make(map[string]networktypes.CreateOptions)
6261
for internalName := range servicesNetworks {
6362
network := networks[internalName]
6463
if network.External {
6564
externalNetworks = append(externalNetworks, network.Name)
6665
continue
6766
}
6867

69-
createOpts := network.CreateOptions{
70-
Labels: AddStackLabel(namespace, nw.Labels),
71-
Driver: nw.Driver,
72-
Options: nw.DriverOpts,
73-
Internal: nw.Internal,
74-
Attachable: nw.Attachable,
68+
createOpts := networktypes.CreateOptions{
69+
Labels: AddStackLabel(namespace, network.Labels),
70+
Driver: network.Driver,
71+
Options: network.DriverOpts,
72+
Internal: network.Internal,
73+
Attachable: network.Attachable,
7574
}
7675

77-
if nw.Ipam.Driver != "" || len(nw.Ipam.Config) > 0 {
78-
createOpts.IPAM = &network.IPAM{}
76+
if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 {
77+
createOpts.IPAM = &networktypes.IPAM{}
7978
}
8079

81-
if nw.Ipam.Driver != "" {
82-
createOpts.IPAM.Driver = nw.Ipam.Driver
80+
if network.Ipam.Driver != "" {
81+
createOpts.IPAM.Driver = network.Ipam.Driver
8382
}
84-
for _, ipamConfig := range nw.Ipam.Config {
85-
config := network.IPAMConfig{
83+
for _, ipamConfig := range network.Ipam.Config {
84+
config := networktypes.IPAMConfig{
8685
Subnet: ipamConfig.Subnet,
8786
}
8887
createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
8988
}
9089

9190
networkName := namespace.Scope(internalName)
92-
if nw.Name != "" {
93-
networkName = nw.Name
91+
if network.Name != "" {
92+
networkName = network.Name
9493
}
9594
result[networkName] = createOpts
9695
}

cli/compose/convert/service.go

+5-21
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ func Service(
8585
return swarm.ServiceSpec{}, err
8686
}
8787

88-
resources, err := convertResources(service.Deploy.Resources)
89-
if err != nil {
90-
return swarm.ServiceSpec{}, err
91-
}
88+
resources := convertResources(service.Deploy.Resources)
9289

9390
restartPolicy, err := convertRestartPolicy(
9491
service.Restart, service.Deploy.RestartPolicy)
@@ -535,31 +532,18 @@ func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig
535532
}
536533
}
537534

538-
func convertResources(source composetypes.Resources) (*swarm.ResourceRequirements, error) {
535+
func convertResources(source composetypes.Resources) *swarm.ResourceRequirements {
539536
resources := &swarm.ResourceRequirements{}
540-
var err error
541537
if source.Limits != nil {
542-
var cpus int64
543-
if source.Limits.NanoCPUs != "" {
544-
cpus, err = opts.ParseCPUs(source.Limits.NanoCPUs)
545-
if err != nil {
546-
return nil, err
547-
}
548-
}
538+
cpus := int64(source.Limits.NanoCPUs * 1e9)
549539
resources.Limits = &swarm.Limit{
550540
NanoCPUs: cpus,
551541
MemoryBytes: int64(source.Limits.MemoryBytes),
552542
Pids: source.Limits.Pids,
553543
}
554544
}
555545
if source.Reservations != nil {
556-
var cpus int64
557-
if source.Reservations.NanoCPUs != "" {
558-
cpus, err = opts.ParseCPUs(source.Reservations.NanoCPUs)
559-
if err != nil {
560-
return nil, err
561-
}
562-
}
546+
cpus := int64(source.Reservations.NanoCPUs * 1e9)
563547

564548
var generic []swarm.GenericResource
565549
for _, res := range source.Reservations.GenericResources {
@@ -581,7 +565,7 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement
581565
GenericResources: generic,
582566
}
583567
}
584-
return resources, nil
568+
return resources
585569
}
586570

587571
func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {

cli/compose/convert/service_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,15 @@ func TestConvertExtraHosts(t *testing.T) {
8383
func TestConvertResourcesFull(t *testing.T) {
8484
source := composetypes.Resources{
8585
Limits: &composetypes.Resource{
86-
NanoCPUs: "0.003",
86+
NanoCPUs: 0.003,
8787
MemoryBytes: composetypes.UnitBytes(300000000),
8888
},
8989
Reservations: &composetypes.Resource{
90-
NanoCPUs: "0.002",
90+
NanoCPUs: 0.002,
9191
MemoryBytes: composetypes.UnitBytes(200000000),
9292
},
9393
}
94-
resources, err := convertResources(source)
95-
assert.NilError(t, err)
94+
resources := convertResources(source)
9695

9796
expected := &swarm.ResourceRequirements{
9897
Limits: &swarm.Limit{
@@ -116,8 +115,7 @@ func TestConvertResourcesOnlyMemory(t *testing.T) {
116115
MemoryBytes: composetypes.UnitBytes(200000000),
117116
},
118117
}
119-
resources, err := convertResources(source)
120-
assert.NilError(t, err)
118+
resources := convertResources(source)
121119

122120
expected := &swarm.ResourceRequirements{
123121
Limits: &swarm.Limit{

cli/compose/convert/volume_test.go

-43
Original file line numberDiff line numberDiff line change
@@ -154,49 +154,6 @@ func TestConvertVolumeToMountConflictingOptionsTmpfsInBind(t *testing.T) {
154154
assert.Error(t, err, "tmpfs options are incompatible with type bind")
155155
}
156156

157-
func TestConvertVolumeToMountConflictingOptionsClusterInVolume(t *testing.T) {
158-
namespace := NewNamespace("foo")
159-
160-
config := composetypes.ServiceVolumeConfig{
161-
Type: "volume",
162-
Target: "/target",
163-
Cluster: &composetypes.ServiceVolumeCluster{},
164-
}
165-
_, err := convertVolumeToMount(config, volumes{}, namespace)
166-
assert.Error(t, err, "cluster options are incompatible with type volume")
167-
}
168-
169-
func TestConvertVolumeToMountConflictingOptionsClusterInBind(t *testing.T) {
170-
namespace := NewNamespace("foo")
171-
172-
config := composetypes.ServiceVolumeConfig{
173-
Type: "bind",
174-
Source: "/foo",
175-
Target: "/target",
176-
Bind: &composetypes.ServiceVolumeBind{
177-
Propagation: "slave",
178-
},
179-
Cluster: &composetypes.ServiceVolumeCluster{},
180-
}
181-
_, err := convertVolumeToMount(config, volumes{}, namespace)
182-
assert.Error(t, err, "cluster options are incompatible with type bind")
183-
}
184-
185-
func TestConvertVolumeToMountConflictingOptionsClusterInTmpfs(t *testing.T) {
186-
namespace := NewNamespace("foo")
187-
188-
config := composetypes.ServiceVolumeConfig{
189-
Type: "tmpfs",
190-
Target: "/target",
191-
Tmpfs: &composetypes.ServiceVolumeTmpfs{
192-
Size: 1000,
193-
},
194-
Cluster: &composetypes.ServiceVolumeCluster{},
195-
}
196-
_, err := convertVolumeToMount(config, volumes{}, namespace)
197-
assert.Error(t, err, "cluster options are incompatible with type tmpfs")
198-
}
199-
200157
func TestConvertVolumeToMountConflictingOptionsBindInTmpfs(t *testing.T) {
201158
namespace := NewNamespace("foo")
202159

cli/compose/loader/full-struct_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ func services(workingDir, homeDir string) types.Services {
9090
},
9191
Resources: types.Resources{
9292
Limits: &types.Resource{
93-
NanoCPUs: "0.001",
93+
NanoCPUs: 0.001,
9494
MemoryBytes: 50 * 1024 * 1024,
9595
Pids: 100,
9696
},
9797
Reservations: &types.Resource{
98-
NanoCPUs: "0.0001",
98+
NanoCPUs: 0.0001,
9999
MemoryBytes: 20 * 1024 * 1024,
100100
GenericResources: []types.GenericResource{
101101
{
@@ -130,7 +130,9 @@ func services(workingDir, homeDir string) types.Services {
130130
},
131131
EndpointMode: "dnsrr",
132132
},
133-
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
133+
Devices: []types.DeviceMapping{{
134+
Source: "/dev/ttyUSB0", Target: "/dev/ttyUSB0",
135+
}},
134136
DNS: []string{"8.8.8.8", "9.9.9.9"},
135137
DNSSearch: []string{"dc1.example.com", "dc2.example.com"},
136138
DomainName: "foo.com",

0 commit comments

Comments
 (0)