Skip to content

Commit 9c60fe6

Browse files
gloursndeloof
authored andcommitted
revert commits link to mount API over bind changes
Signed-off-by: Guillaume Lours <[email protected]>
1 parent c16df17 commit 9c60fe6

File tree

4 files changed

+75
-91
lines changed

4 files changed

+75
-91
lines changed

pkg/compose/compose.go

-17
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,6 @@ func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) {
321321

322322
}
323323

324-
var windowsContainer = struct {
325-
once sync.Once
326-
val bool
327-
err error
328-
}{}
329-
330-
func (s *composeService) isWindowsContainer(ctx context.Context) (bool, error) {
331-
windowsContainer.once.Do(func() {
332-
info, err := s.apiClient().Info(ctx)
333-
if err != nil {
334-
windowsContainer.err = err
335-
}
336-
windowsContainer.val = info.OSType == "windows"
337-
})
338-
return windowsContainer.val, windowsContainer.err
339-
}
340-
341324
func (s *composeService) isDesktopIntegrationActive() bool {
342325
return s.desktopCli != nil
343326
}

pkg/compose/convergence_test.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
containerType "github.com/docker/docker/api/types/container"
2929
"github.com/docker/docker/api/types/filters"
3030
"github.com/docker/docker/api/types/network"
31+
"github.com/docker/go-connections/nat"
3132
"go.uber.org/mock/gomock"
3233
"gotest.tools/v3/assert"
3334

@@ -300,10 +301,17 @@ func TestCreateMobyContainer(t *testing.T) {
300301
},
301302
}
302303

303-
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
304-
v := x.(*containerType.HostConfig)
305-
return v.NetworkMode == "b-moby-name"
306-
}), gomock.Eq(
304+
var falseBool bool
305+
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Eq(
306+
&containerType.HostConfig{
307+
PortBindings: nat.PortMap{},
308+
ExtraHosts: []string{},
309+
Tmpfs: map[string]string{},
310+
Resources: containerType.Resources{
311+
OomKillDisable: &falseBool,
312+
},
313+
NetworkMode: "b-moby-name",
314+
}), gomock.Eq(
307315
&network.NetworkingConfig{
308316
EndpointsConfig: map[string]*network.EndpointSettings{
309317
"b-moby-name": {
@@ -382,10 +390,17 @@ func TestCreateMobyContainer(t *testing.T) {
382390
},
383391
}
384392

385-
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
386-
v := x.(*containerType.HostConfig)
387-
return v.NetworkMode == "b-moby-name"
388-
}), gomock.Eq(
393+
var falseBool bool
394+
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Eq(
395+
&containerType.HostConfig{
396+
PortBindings: nat.PortMap{},
397+
ExtraHosts: []string{},
398+
Tmpfs: map[string]string{},
399+
Resources: containerType.Resources{
400+
OomKillDisable: &falseBool,
401+
},
402+
NetworkMode: "b-moby-name",
403+
}), gomock.Eq(
389404
&network.NetworkingConfig{
390405
EndpointsConfig: map[string]*network.EndpointSettings{
391406
"a-moby-name": {

pkg/compose/create.go

+46-56
Original file line numberDiff line numberDiff line change
@@ -801,28 +801,28 @@ func (s *composeService) buildContainerVolumes(
801801
return nil, nil, err
802802
}
803803

804-
mountOptions, err := s.buildContainerMountOptions(ctx, p, service, imgInspect, inherit)
804+
mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
805805
if err != nil {
806806
return nil, nil, err
807807
}
808808

809-
version, err := s.RuntimeVersion(ctx)
810-
if err != nil {
811-
return nil, nil, err
812-
}
813-
if versions.GreaterThan(version, "1.42") {
814-
// We can fully leverage `Mount` API as a replacement for legacy `Bind`
815-
return nil, mountOptions, nil
816-
}
817-
818809
MOUNTS:
819810
for _, m := range mountOptions {
811+
if m.Type == mount.TypeNamedPipe {
812+
mounts = append(mounts, m)
813+
continue
814+
}
820815
if m.Type == mount.TypeBind {
821-
// `Mount` does not offer option to created host path if missing
816+
// `Mount` is preferred but does not offer option to created host path if missing
822817
// so `Bind` API is used here with raw volume string
818+
// see https://github.com/moby/moby/issues/43483
823819
for _, v := range service.Volumes {
824820
if v.Target == m.Target {
825-
if v.Bind != nil && v.Bind.CreateHostPath {
821+
switch {
822+
case string(m.Type) != v.Type:
823+
v.Source = m.Source
824+
fallthrough
825+
case v.Bind != nil && v.Bind.CreateHostPath:
826826
binds = append(binds, v.String())
827827
continue MOUNTS
828828
}
@@ -834,7 +834,7 @@ MOUNTS:
834834
return binds, mounts, nil
835835
}
836836

837-
func (s *composeService) buildContainerMountOptions(ctx context.Context, p types.Project, service types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
837+
func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
838838
var mounts = map[string]mount.Mount{}
839839
if inherit != nil {
840840
for _, m := range inherit.Mounts {
@@ -859,7 +859,7 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
859859
}
860860
}
861861
volumes := []types.ServiceVolumeConfig{}
862-
for _, v := range service.Volumes {
862+
for _, v := range s.Volumes {
863863
if v.Target != m.Destination || v.Source != "" {
864864
volumes = append(volumes, v)
865865
continue
@@ -872,11 +872,11 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
872872
ReadOnly: !m.RW,
873873
}
874874
}
875-
service.Volumes = volumes
875+
s.Volumes = volumes
876876
}
877877
}
878878

879-
mounts, err := s.fillBindMounts(ctx, p, service, mounts)
879+
mounts, err := fillBindMounts(p, s, mounts)
880880
if err != nil {
881881
return nil, err
882882
}
@@ -888,27 +888,27 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
888888
return values, nil
889889
}
890890

891-
func (s *composeService) fillBindMounts(ctx context.Context, p types.Project, service types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
892-
for _, v := range service.Volumes {
893-
bindMount, err := s.buildMount(ctx, p, v)
891+
func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
892+
for _, v := range s.Volumes {
893+
bindMount, err := buildMount(p, v)
894894
if err != nil {
895895
return nil, err
896896
}
897897
m[bindMount.Target] = bindMount
898898
}
899899

900-
secrets, err := s.buildContainerSecretMounts(ctx, p, service)
900+
secrets, err := buildContainerSecretMounts(p, s)
901901
if err != nil {
902902
return nil, err
903903
}
904-
for _, secret := range secrets {
905-
if _, found := m[secret.Target]; found {
904+
for _, s := range secrets {
905+
if _, found := m[s.Target]; found {
906906
continue
907907
}
908-
m[secret.Target] = secret
908+
m[s.Target] = s
909909
}
910910

911-
configs, err := s.buildContainerConfigMounts(ctx, p, service)
911+
configs, err := buildContainerConfigMounts(p, s)
912912
if err != nil {
913913
return nil, err
914914
}
@@ -921,11 +921,11 @@ func (s *composeService) fillBindMounts(ctx context.Context, p types.Project, se
921921
return m, nil
922922
}
923923

924-
func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types.Project, service types.ServiceConfig) ([]mount.Mount, error) {
924+
func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
925925
var mounts = map[string]mount.Mount{}
926926

927927
configsBaseDir := "/"
928-
for _, config := range service.Configs {
928+
for _, config := range s.Configs {
929929
target := config.Target
930930
if config.Target == "" {
931931
target = configsBaseDir + config.Source
@@ -953,7 +953,7 @@ func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types
953953
continue
954954
}
955955

956-
bindMount, err := s.buildMount(ctx, p, types.ServiceVolumeConfig{
956+
bindMount, err := buildMount(p, types.ServiceVolumeConfig{
957957
Type: types.VolumeTypeBind,
958958
Source: definedConfig.File,
959959
Target: target,
@@ -971,11 +971,11 @@ func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types
971971
return values, nil
972972
}
973973

974-
func (s *composeService) buildContainerSecretMounts(ctx context.Context, p types.Project, service types.ServiceConfig) ([]mount.Mount, error) {
974+
func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
975975
var mounts = map[string]mount.Mount{}
976976

977977
secretsDir := "/run/secrets/"
978-
for _, secret := range service.Secrets {
978+
for _, secret := range s.Secrets {
979979
target := secret.Target
980980
if secret.Target == "" {
981981
target = secretsDir + secret.Source
@@ -1003,7 +1003,7 @@ func (s *composeService) buildContainerSecretMounts(ctx context.Context, p types
10031003
continue
10041004
}
10051005

1006-
mnt, err := s.buildMount(ctx, p, types.ServiceVolumeConfig{
1006+
mnt, err := buildMount(p, types.ServiceVolumeConfig{
10071007
Type: types.VolumeTypeBind,
10081008
Source: definedSecret.File,
10091009
Target: target,
@@ -1039,7 +1039,7 @@ func isWindowsAbs(p string) bool {
10391039
return false
10401040
}
10411041

1042-
func (s *composeService) buildMount(ctx context.Context, project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
1042+
func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
10431043
source := volume.Source
10441044
// on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
10451045
// do not replace these with filepath.Abs(source) that will include a default drive.
@@ -1060,10 +1060,7 @@ func (s *composeService) buildMount(ctx context.Context, project types.Project,
10601060
}
10611061
}
10621062

1063-
bind, vol, tmpfs, err := s.buildMountOptions(ctx, volume)
1064-
if err != nil {
1065-
return mount.Mount{}, err
1066-
}
1063+
bind, vol, tmpfs := buildMountOptions(project, volume)
10671064

10681065
volume.Target = path.Clean(volume.Target)
10691066

@@ -1083,7 +1080,7 @@ func (s *composeService) buildMount(ctx context.Context, project types.Project,
10831080
}, nil
10841081
}
10851082

1086-
func (s *composeService) buildMountOptions(ctx context.Context, volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions, error) {
1083+
func buildMountOptions(project types.Project, volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
10871084
switch volume.Type {
10881085
case "bind":
10891086
if volume.Volume != nil {
@@ -1092,47 +1089,40 @@ func (s *composeService) buildMountOptions(ctx context.Context, volume types.Ser
10921089
if volume.Tmpfs != nil {
10931090
logrus.Warnf("mount of type `bind` should not define `tmpfs` option")
10941091
}
1095-
option, err := s.buildBindOption(ctx, volume.Bind)
1096-
return option, nil, nil, err
1092+
return buildBindOption(volume.Bind), nil, nil
10971093
case "volume":
10981094
if volume.Bind != nil {
10991095
logrus.Warnf("mount of type `volume` should not define `bind` option")
11001096
}
11011097
if volume.Tmpfs != nil {
11021098
logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
11031099
}
1104-
return nil, buildVolumeOptions(volume.Volume), nil, nil
1100+
if v, ok := project.Volumes[volume.Source]; ok && v.DriverOpts["o"] == types.VolumeTypeBind {
1101+
return buildBindOption(&types.ServiceVolumeBind{
1102+
CreateHostPath: true,
1103+
}), nil, nil
1104+
}
1105+
return nil, buildVolumeOptions(volume.Volume), nil
11051106
case "tmpfs":
11061107
if volume.Bind != nil {
11071108
logrus.Warnf("mount of type `tmpfs` should not define `bind` option")
11081109
}
11091110
if volume.Volume != nil {
11101111
logrus.Warnf("mount of type `tmpfs` should not define `volume` option")
11111112
}
1112-
return nil, nil, buildTmpfsOptions(volume.Tmpfs), nil
1113+
return nil, nil, buildTmpfsOptions(volume.Tmpfs)
11131114
}
1114-
return nil, nil, nil, nil
1115+
return nil, nil, nil
11151116
}
11161117

1117-
func (s *composeService) buildBindOption(ctx context.Context, bind *types.ServiceVolumeBind) (*mount.BindOptions, error) {
1118+
func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
11181119
if bind == nil {
1119-
return nil, nil
1120-
}
1121-
1122-
propagation := bind.Propagation
1123-
isWindowsContainer, err := s.isWindowsContainer(ctx)
1124-
if err != nil {
1125-
return nil, err
1126-
}
1127-
if propagation == "" && !isWindowsContainer {
1128-
propagation = types.PropagationRPrivate
1120+
return nil
11291121
}
1130-
11311122
return &mount.BindOptions{
1132-
Propagation: mount.Propagation(propagation),
1133-
CreateMountpoint: bind.CreateHostPath,
1123+
Propagation: mount.Propagation(bind.Propagation),
11341124
// NonRecursive: false, FIXME missing from model ?
1135-
}, nil
1125+
}
11361126
}
11371127

11381128
func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {

pkg/compose/create_test.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compose
1818

1919
import (
20-
"context"
2120
"os"
2221
"path/filepath"
2322
"sort"
@@ -42,8 +41,7 @@ func TestBuildBindMount(t *testing.T) {
4241
Source: "",
4342
Target: "/data",
4443
}
45-
s := composeService{}
46-
mount, err := s.buildMount(context.TODO(), project, volume)
44+
mount, err := buildMount(project, volume)
4745
assert.NilError(t, err)
4846
assert.Assert(t, filepath.IsAbs(mount.Source))
4947
_, err = os.Stat(mount.Source)
@@ -58,8 +56,7 @@ func TestBuildNamedPipeMount(t *testing.T) {
5856
Source: "\\\\.\\pipe\\docker_engine_windows",
5957
Target: "\\\\.\\pipe\\docker_engine",
6058
}
61-
s := composeService{}
62-
mount, err := s.buildMount(context.TODO(), project, volume)
59+
mount, err := buildMount(project, volume)
6360
assert.NilError(t, err)
6461
assert.Equal(t, mount.Type, mountTypes.TypeNamedPipe)
6562
}
@@ -78,8 +75,7 @@ func TestBuildVolumeMount(t *testing.T) {
7875
Source: "myVolume",
7976
Target: "/data",
8077
}
81-
s := composeService{}
82-
mount, err := s.buildMount(context.TODO(), project, volume)
78+
mount, err := buildMount(project, volume)
8379
assert.NilError(t, err)
8480
assert.Equal(t, mount.Source, "myProject_myVolume")
8581
assert.Equal(t, mount.Type, mountTypes.TypeVolume)
@@ -156,8 +152,8 @@ func TestBuildContainerMountOptions(t *testing.T) {
156152
},
157153
},
158154
}
159-
s := composeService{}
160-
mounts, err := s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], moby.ImageInspect{}, inherit)
155+
156+
mounts, err := buildContainerMountOptions(project, project.Services["myService"], moby.ImageInspect{}, inherit)
161157
sort.Slice(mounts, func(i, j int) bool {
162158
return mounts[i].Target < mounts[j].Target
163159
})
@@ -169,7 +165,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
169165
assert.Equal(t, mounts[2].VolumeOptions.Subpath, "etc")
170166
assert.Equal(t, mounts[3].Target, "\\\\.\\pipe\\docker_engine")
171167

172-
mounts, err = s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], moby.ImageInspect{}, inherit)
168+
mounts, err = buildContainerMountOptions(project, project.Services["myService"], moby.ImageInspect{}, inherit)
173169
sort.Slice(mounts, func(i, j int) bool {
174170
return mounts[i].Target < mounts[j].Target
175171
})

0 commit comments

Comments
 (0)