Skip to content

Commit e016874

Browse files
committed
when using bind API, use compose-go to (re)build volume string
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent a32fdff commit e016874

File tree

4 files changed

+7
-31
lines changed

4 files changed

+7
-31
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.2
77
github.com/buger/goterm v1.0.4
88
github.com/cnabio/cnab-to-oci v0.3.1-beta1
9-
github.com/compose-spec/compose-go v1.2.2
9+
github.com/compose-spec/compose-go v1.2.3
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.6.2
1212
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
302302
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
303303
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
304304
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
305-
github.com/compose-spec/compose-go v1.2.2 h1:y1dwl3KUTBnWPVur6EZno9zUIum6Q87/F5keljnGQB4=
306-
github.com/compose-spec/compose-go v1.2.2/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
305+
github.com/compose-spec/compose-go v1.2.3 h1:r9zZfxQKG2A3fTy/MobMecUR1cd3uf7DlQQeB/NKVJ0=
306+
github.com/compose-spec/compose-go v1.2.3/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
307307
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
308308
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
309309
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=

pkg/compose/create.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,13 @@ func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Proj
713713
MOUNTS:
714714
for _, m := range mountOptions {
715715
volumeMounts[m.Target] = struct{}{}
716-
// `Bind` API is used when host path need to be created if missing, `Mount` is preferred otherwise
717716
if m.Type == mount.TypeBind || m.Type == mount.TypeNamedPipe {
717+
// `Mount` is preferred but does not offer option to created host path if missing
718+
// so `Bind` API is used here with raw volume string
719+
// see https://github.com/moby/moby/issues/43483
718720
for _, v := range service.Volumes {
719721
if v.Target == m.Target && v.Bind != nil && v.Bind.CreateHostPath {
720-
binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, getBindMode(v.Bind, m.ReadOnly)))
722+
binds = append(binds, v.String())
721723
continue MOUNTS
722724
}
723725
}
@@ -727,23 +729,6 @@ MOUNTS:
727729
return volumeMounts, binds, mounts, nil
728730
}
729731

730-
func getBindMode(bind *types.ServiceVolumeBind, readOnly bool) string {
731-
mode := "rw"
732-
733-
if readOnly {
734-
mode = "ro"
735-
}
736-
737-
switch bind.SELinux {
738-
case types.SELinuxShared:
739-
mode += ",z"
740-
case types.SELinuxPrivate:
741-
mode += ",Z"
742-
}
743-
744-
return mode
745-
}
746-
747732
func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
748733
var mounts = map[string]mount.Mount{}
749734
if inherit != nil {

pkg/compose/create_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,6 @@ func TestBuildContainerMountOptions(t *testing.T) {
143143
assert.Equal(t, mounts[1].Target, "/var/myvolume2")
144144
}
145145

146-
func TestGetBindMode(t *testing.T) {
147-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, false), "rw")
148-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, true), "ro")
149-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, false), "rw,z")
150-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, false), "rw,Z")
151-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, true), "ro,z")
152-
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, true), "ro,Z")
153-
}
154-
155146
func TestGetDefaultNetworkMode(t *testing.T) {
156147
t.Run("returns the network with the highest priority when service has multiple networks", func(t *testing.T) {
157148
service := composetypes.ServiceConfig{

0 commit comments

Comments
 (0)